0

I followed the Android Studio tutorial to get the CameraPreview to work (Camera API Android Developer Guide). This works fine for me and i can view the camera stream in my FrameLayout.

But I would like to get the RGB values from a specific Pixel in the Preview everytime it changes. I did not find a method which gives me the previewImage as a bitmap and was not able to understand the usage of the onPreviewFrame method

@Override public void onPreviewFrame(byte[] data, Camera camera) {}

How can I get the RGB values from a Camerapreview Pixel?

Zoe
  • 27,060
  • 21
  • 118
  • 148
J.Doe
  • 82
  • 1
  • 10

2 Answers2

0

If you are using the Camera2 API, you can implement the ImageReader.OnImageAvailableListener class in your application. After that, you override the onImageAvailable function , which gets an ImageReader as argument. Then you can access the image just recorded with imageReader.acquireNextImage().

Zelig63
  • 1,592
  • 1
  • 23
  • 40
0

With either API, you need to handle processing YUV data yourself, unfortunately.

Camera devices natively produce YUV data, not RGB, so the API doesn't spend extra resources to auto-convert the data. The main easy exception is piping data to the GPU, where the GPU driver auto-converts YUV to RGB for you within your pixel shader.

But if you're just in regular app code, you need to parse the data.

For the deprecated android.hardware.Camera API, the output is NV21 by default, and you can usually select YV12 as another option.

The wikipedia article on YUV is relatively helpful: https://en.wikipedia.org/wiki/YUV

But it does have the wrong conversion coefficients for YUV->RGB conversion; they should be:

R = Y + 1.402 (Cr-128)
G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)
B = Y + 1.772 (Cb-128)

(Cb = U, Cr = V)

You can also take a look at this stackoverflow post: Extract black and white image from android camera's NV21 format

which has code that looks to be correct for the conversion.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47