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
You can try to use following code to get the bitmap.
Why would that be faster?
This result create a bitmap at a certain moment, then get the value.