Xamarin CameraX OnImageCapturedCallback

Viewed 616

I've been working with the "Getting started with CameraX" port to c# https://github.com/venetasoft/Xamarin.CameraX.

The example uses the ImageSaveCallback, but I'd like to use ImageCapturedCallback instead, since I'd like to access the image in memory.

My code is generating the error "cannot convert from 'CameraX.ImageCapturedCallback' to 'AndroidX.Camera.Core.ImageCapture.OnImageCapturedCallback'", so clearly I'm missing something obvious here.

In the ImageSaveCallback example, the code is:

imageCapture.TakePicture(outputOptions, ContextCompat.GetMainExecutor(this), new ImageSaveCallback(

            onErrorCallback: (exc) =>
            {
                var msg = $"Photo capture failed: {exc.Message}";
                Log.Error(TAG, msg, exc);
                Toast.MakeText(this.BaseContext, msg, ToastLength.Short).Show();
            },

            onImageSaveCallback: (output) =>
            {
                var savedUri = output.SavedUri;
                var msg = $"Photo capture succeeded: {savedUri}";
                Log.Debug(TAG, msg);
                Toast.MakeText(this.BaseContext, msg, ToastLength.Short).Show();
            }
        ));

and the callback is:

class ImageSaveCallback : Java.Lang.Object, IOnImageSavedCallback
{
    private const string TAG = "CameraXBasic";

    private readonly Action<ImageCaptureException> onErrorCallback;
    private readonly Action<OutputFileResults> onImageSaveCallback;

    public ImageSaveCallback(Action<OutputFileResults> onImageSaveCallback, Action<ImageCaptureException> onErrorCallback)
    {
        this.onImageSaveCallback = onImageSaveCallback;
        this.onErrorCallback = onErrorCallback;
    }

    public void OnError(ImageCaptureException exc)
    {
        this.onErrorCallback.Invoke(exc);
    }

    public void OnImageSaved(OutputFileResults photoFile)
    {
        this.onImageSaveCallback.Invoke(photoFile);
    }
}

My attempt, based on the above:

imageCapture.TakePicture(ContextCompat.GetMainExecutor(this), new ImageCapturedCallback(

            onErrorCallback: (exc) =>
            {
                // handle error
            },

            onImageCapturedCallback: (output) =>
            {
                // handle image
            }
        ));

And the callback class:

class ImageCapturedCallback : Java.Lang.Object { 

    private readonly Action<ImageCaptureException> onErrorCallback;
    private readonly Action<IImageProxy> onImageCapturedCallback;

    public ImageCapturedCallback(Action<ImageCaptureException> onErrorCallback, Action<IImageProxy> onImageCapturedCallback)
    {
        onErrorCallback = onErrorCallback;
        onImageCapturedCallback = onImageCapturedCallback;
    }

    public void OnError(ImageCaptureException exc)
    {
        this.onErrorCallback.Invoke(exc);
    }

    public void OnCaptureSuccess(IImageProxy proxyImage)
    {
        this.onImageCapturedCallback.Invoke(proxyImage);
    }



}
1 Answers

I found the answer - there's a branch of the example code https://github.com/venetasoft/Xamarin.CameraX/tree/CameraX_Updated.

Based on that code, the ImageCapturedCallback class looks like this:

class ImageCapturedCallback : OnImageCapturedCallback
{
    private readonly Action<ImageCaptureException> onErrorCallback;
    private readonly Action<Bitmap> onCapturedSuccessCallback;

    public ImageCapturedCallback(Action<Bitmap> onCapturedSuccessCallback, Action<ImageCaptureException> onErrorCallback)
    {
        this.onCapturedSuccessCallback = onCapturedSuccessCallback;
        this.onErrorCallback = onErrorCallback;
    }

    public override void OnError(ImageCaptureException exc)
    {
        this.onErrorCallback?.Invoke(exc);
    }

    public override void OnCaptureSuccess(IImageProxy image)
    {
        var data = ImageUtil.ImageToJpegByteArray(image);
        var imageBitmap = BitmapFactory.DecodeByteArray(data, 0, data.Length);

        this.onCapturedSuccessCallback?.Invoke(imageBitmap);

        base.OnCaptureSuccess(image);
        image.Close();
    }
} 
Related