I am using a simple camera app, and obtain the live camera preview to a surfaceView
. I turn the camerea on/off using a button. In the button's onClick
, after I call camera.startPreview()
, I run:
camera.setPreviewCallback(new PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, Camera camera) {
sumRED = 0;
sumGREEN = 0;
sumBLUE = 0;
int frameHeight = camera.getParameters().getPictureSize().height;
int frameWidth = camera.getParameters().getPictureSize().width;
int[] myPixels = convertYUV420_NV21toRGB8888(data, 200, 200);
Bitmap bm = Bitmap.createBitmap(myPixels, 200, 200, Bitmap.Config.ARGB_8888);
imageView.setImageBitmap(bm);
for (int i = 0; i < myPixels.length; i++) {
sumRED = sumRED + Color.red(myPixels[i]);
sumGREEN = sumGREEN + Color.green(myPixels[i]);
sumBLUE = sumBLUE + Color.blue(myPixels[i]);
}
sumRED = sumRED / myPixels.length;
sumGREEN = sumGREEN / myPixels.length;
sumBLUE = sumBLUE / myPixels.length;
String sRed = Float.toString(sumRED);
String sGreen = Float.toString(sumGREEN);
String sBlue = Float.toString(sumBLUE);
rTextView.setText("RED: " + sRed);
gTextView.setText("Green: " + sGreen);
bTextView.setText("Blue: " + sBlue);
}
}
I am using the offered code for NV21 to RGB conversion from here.
I thought that by doing Color.blue(myPixels[i]);
for the R-G-B colors I will get the color intensity of each pixel, and then obtain the average colol intensity of the picture.
But I have noticed that the TextView
shows the Red and the Green as exactly same values.
Is the code I am reusing is not right for this purpose? or is there a better, preferred, more efficieny way to obtain specific color intensity of a pixel (or live video preview?)
Thank you
update:
At first I was not touching the output format of the preview. The preview of the rebuilt bitmap was really bad. When I added cameraParam.setPreviewFormat(ImageFormat.YV12);
the rebuilt bitmap divided the rebuilt image to 4 quadrants. When I covered the camera with my finger (was shown mosly orange/red due to flash being on), the rebuilt image was purple on the top two quadrants, and greenish on the bottom two. When I changed the format to NV21
, the rebuilt image was a single image, and showed pretty much the same as the shown preview, and there was a segnificant change between all R-G-B colors. So, my question is: Why am I getting results in the range of 150-160 when most of the screen is a single color, and values in the range of 255 or so? I thought that the conversion algorithm I was using was converting to a scale of 255. What am I missing here?