0

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?

Community
  • 1
  • 1
KingsInnerSoul
  • 1,373
  • 4
  • 20
  • 49
  • Have you checked your totals (sumRED, sumGreen) to make sure they aren't rolling over and that they are equal? I always worry about summing and dividing with ints when there is so much data. – HalR Apr 11 '13 at 02:30
  • I just checked it, Its not equal. Its in the magnitude of (E7). but the difference between Red to Blue is 3000 or so (small difference in the area of 10million... I tried screeing Red, Green, and Blue papers; Green reacts to the colors, but Red and Blue tend to be in the same ball-park. – KingsInnerSoul Apr 11 '13 at 03:23
  • Are you using longs for sumRed etc.? – HalR Apr 11 '13 at 03:34
  • I added an **update** my original question, as it was too long to be a comment. – KingsInnerSoul Apr 11 '13 at 04:11

0 Answers0