Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Why is the camera's GetPixel(x, y) function so slow?

I'm trying to get a color sample of the camera while it's open at something around 24fps. The way I have it now chugs a bit and eventually crashes. Not sure what I'm doing wrong.

This is the code I have currently. I've tested with a variety of things here. I've tried in a thread, not in a thread, different intervals, etc. It technically works, but crashes and is unstable.

void SetupInterval() {
    float interval = 0.05f;
    Color pixelColor;

    Thread thread = new Thread(() => {
        Device.StartTimer(TimeSpan.FromSeconds(interval), () => {
            pixelColor = GetPixelFromCamera();
            return true;
        });
    });

    thread.Start();
}


public Color GetPixelFromCamera() {
    int x = liveView.Bitmap.Width / 2;
    int y = liveView.Bitmap.Height / 2;
    int color = liveView.Bitmap.GetPixel(x, y);

    int A = (color >> 24) & 0xff; // or color >>> 24
    int R = (color >> 16) & 0xff;
    int G = (color >> 8) & 0xff;
    int B = (color) & 0xff;

    Color pixelColor = new Color(color);
    capturePhotoButton.color = pixelColor;
    capturePhotoButton.Invalidate();

    return pixelColor;
}

Anything related to liveView.Bitmap makes the timer slow and the app crashes within like 10-15 seconds. What should I be doing differently?

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    You can try to use following code to get the bitmap.

        Bitmap  bitmap= ((BitmapDrawable)imageView.Drawable).Bitmap;
        bitmap.GetPixel(x,y);
    
  • Why would that be faster?

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    This result create a bitmap at a certain moment, then get the value.

Sign In or Register to comment.