1

What I'm trying to do is get a stream of RGB values from a picture taken from the Android camera preview.

So I've looked up a tonne of questions on Stackoverflow and tutorials online and I've gotten this far:

Set the following camera properties:

        Camera.Parameters param = camera.getParameters();

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int swidth = size.x;
        int sheight = size.y;

        param.setPreviewSize(sheight, swidth);
        camera.setParameters(param);
        param.setPreviewFormat(ImageFormat.NV21);

        camera.setPreviewDisplay(surfaceHolder);
        camera.startPreview();
        camera.setDisplayOrientation(90);

The param.setPreviewFormat(ImageFormat.NV21); is for compatibility on all devices.

Then I have:

    jpegCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {

                int[] rgbs = new int[swidth*sheight]; //from above code
                decodeYUV(rgbs, data, swidth, sheight);
                for(int i = 0; i<rgbs.length; i++)
                    System.out.println("RGB: " + rgbs[i]);

where decodeYUV() is the method given here on SO. I've tried using both answers (methods), and I get similar results. This means it must be working, I'm just doing something wrong.

Now, I'm assuming it is in the format ARGB.

I get the following stream of output from the above code:

RGB: -16757489
RGB: -16059990
RGB: -9157
RGB: -49494
RGB: -2859008
RGB: -7283401
RGB: -4288512
RGB: -3339658
RGB: -6411776
RGB: -13994240
RGB: -16750475
RGB: -16735438
RGB: -14937280
RGB: -3866455
RGB: -16762040
RGB: -16714621
RGB: -11647630
RGB: -37121
...
...

How do I extract RGB values from this, in the form R/G/B = [0..255]?

Thanks for any help!

Community
  • 1
  • 1
Greg Peckory
  • 7,700
  • 21
  • 67
  • 114

1 Answers1

1

If the format is ARGB then:

int argb = rgbs[i];
int a = ( argb >> 24 ) & 255;
int r = ( argb >> 16 ) & 255;
int g = ( argb >> 8 ) & 255;
int b = argb & 255;

The >> operator shifts the int to the right, and the && is a boolean and which masks the last eight bits for the result.

323go
  • 14,143
  • 6
  • 33
  • 41
  • 1
    This has certainly helped (assuming you meant `&` and not `&&`). I am getting RGB values but not the correct ones unfortunately. It always seems to average out around 120/120/120 give or take, even when I take a pure black picture. I also get Array Out of Bounds inside my decode function. Any ideas why this may be happening? I have been following precise steps. – Greg Peckory Jan 18 '16 at 09:56
  • Hard to tell why you get an exception in decode... you didn't post it. Thanks for pointing out the typo -- fixed. – 323go Jan 19 '16 at 13:46