I have to make a simple app that will messure the saturation, brightness etc. from camera preview on Android. My code is now sending image to data in:
public void onPreviewFrame(byte[] data, Camera camera){
}
...and if I'm not wrong it is in YUV420SP format. I have tried to find some information about this but unsuccessful. Can anyone tell me how to manage this format?
See Android document: http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat(int); the image format is described here: http://www.fourcc.org/yuv.php#NV21.
In the nutshell, this byte[]
contains two parts: luma and chroma. You can use the camera object to find the current parameters (don't use this in production code in every call to onPreviewFrame()
, because these calls are performance burden, but reuse the values):
int w = camera.getParameters().getPreviewSize().width;
int h = camera.getParameters().getPreviewSize().height;
byte[] luma = new byte[w*h];
byte[] chroma = new byte[w*h/2];
System.arraycopy(data, 0, luma, 0, w*h);
System.arraycopy(data, w*h, chroma, 0, w*h/2);
int Y_at_x_y = luma[x + y*w]; // or data[x + y*w]
int U_at_x_y = chroma[x/2 + y*w/2 + 1]; // or data[w*h + x/2 + y*w/2 + 1]
int V_at_x_y = chroma[x/2 + y*w/2]; // or data[w*h + x/2 + y*w/2]