hjam40 / Camera.MAUI

A CameraView Control for preview, take photos and control the camera options
MIT License
448 stars 72 forks source link

Providing access to idividual frames. #53

Open Expecho opened 1 year ago

Expecho commented 1 year ago

For now, it seems the only way to get an image using this plugin is by doing something like cameraView.GetSnapShot(Camera.MAUI.ImageFormat.JPEG); or am I wrong?

The built-in barcode/QR detection uses the FrameReader_FrameArrived event as seen here on the Windows platform (haven't looked at the other platform implementations). How about making this event public so consumers can subscribe and process the received frames by, for example, ML algorithms, other QR/Barcode libs or whatever one needs?

If it fits your purpose I am more than willing to try to come up with an implementation, although testing on an IOS device would be hard for me as I don't have one.

cropyai commented 1 year ago

@Expecho We also wanted to get individual frames (5 per second). However, the GetSnapShot method works so slowly that we can only get 1 per second. If we could access the Camera Preview Image Capturing event, it would be awesome.

@hjam40 do you think it is applicable?

hjam40 commented 1 year ago

Hi,

I understand your problem, but you have to think that this plugging is not an Android Camera Preview, it is a multiplatform plugging. This is a problem because each platform gets a different image format from the camera frame and I have to convert it to a common format (this is the reason why GetSnapShot is so slowly). By the way, right now you could try to use the auto snap properties (in memory buffers):

AutoSnapShotSeconds = 0.2; AutoSnapShotFormat = ImageFormat.JPG; AutoSnapShotAsImageSource = false;

and read from the bindable property SnapShotStream.

Hrishikesh46779 commented 1 year ago

It will be very helpful if we get access to the raw live camera feed and access each frame.

kfrancis commented 10 months ago

Agree, doing that means we can run OCR on the frame data making it more than just barcodes.

cropyai commented 10 months ago

@kfrancis @Hrishikesh46779 I can maybe help you in accessing live preview frames for Android only? I kind of changed small part of the library. I don't remember how, so in my free time I would maybe take a look at if you want.

I took a quick look. If you want to get access to frames, you can call TakeSnap function in MauiCameraView.cs for android. Making it public is enough.

public Bitmap TakeSnap()
{
    Bitmap bitmap = null;
    try
    {
        MainThread.InvokeOnMainThreadAsync(() => { bitmap = textureView.GetBitmap(null); bitmap = textureView.Bitmap; }).Wait();
        if (bitmap != null)
        {
            int oriWidth = bitmap.Width;
            int oriHeight = bitmap.Height;

            bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, textureView.GetTransform(null), false);
            float xscale = (float)oriWidth / bitmap.Width;
            float yscale = (float)oriHeight / bitmap.Height;
            //bitmap = Bitmap.CreateBitmap(bitmap, Math.Abs(bitmap.Width - (int)((float)Width*xscale)) / 2, Math.Abs(bitmap.Height - (int)((float)Height * yscale)) / 2, Width, Height);
            bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, Width, Height);
            if (textureView.ScaleX == -1)
            {
                Matrix matrix = new();
                matrix.PreScale(-1, 1);
                bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, false);
            }
        }
    }
    catch { }
    return bitmap;
}

This code would probably provide you 5-10 fps live feed.

Than, in your app, you might use this code to get the snapshot.

Android.Graphics.Bitmap bmp = null;
var androidview = (cameraView.Handler.PlatformView as Camera.MAUI.Platforms.Android.MauiCameraView);
bmp = androidview.TakeSnap();
programatix commented 10 months ago

I've made some modification by moving qr code decoding as plugin. See https://github.com/hjam40/Camera.MAUI/pull/107. You can create your own plugin to process the frame provided by Camera.MAUI. I added Google MLKit plugin as ZXing alternative as proof of concept.