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!