0

I'm using the Android camera in my app.

MY SETUP

1) I find out the dimensions of the App Window.

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

(Step 2 and 3 are done in SurfaceCreated and SurfaceChanged)

2) I set the camera preview to the app size (in my case it is 480x800).

param.setPreviewSize(height, width);

3) I set the camera resolution (picture size) equal to that of the preview size.

param.setPictureSize(height, width);

Here are the rest of the parameters.

param.setPreviewFormat(ImageFormat.NV21);
camera.setPreviewDisplay(surfaceHolder);
camera.setDisplayOrientation(90);

4) I have the method which deals with capturing an image:

public void onPictureTaken(byte[] data, Camera camera) {
    ...

MY PROBLEM

The length of data (stream of bytes) varies depending on the picture taken (depending on the quality I'm presuming).

However my phone Window resolution is 480*800 (as I said earlier), yet the stream of bytes (data) is generally around 28000 in size.

If the resolution is 480*800 then we are talking about 384000 pixels. And if each pixel requires a Y,U and V value, assuming a byte is dedicated to each, that would mean the array of bytes (data) should be 384000*3 = 1152000 in length.

And 1152000 > 28000. By a lot!!

The reason this is bothering me is because I am using the methods given here on SO, for converting these YUV values into RGB values.

I call it like this:

int[] rgbs = new int[width*height];
decodeYUV(rgbs, data, width, height);

Where width, height are the dimensions of the App Window 800*480.

And I keep getting Array Out of Bounds exceptions, and exceptions like buffer size < minimum.

Anyone know what the problem might be? Any help would be hugely appreciated.

Community
  • 1
  • 1
Greg Peckory
  • 7,700
  • 21
  • 67
  • 114
  • " The format of the data depends on the context of the callback and Camera.Parameters settings." - Can you tell us your Parameters? – Fildor Jan 18 '16 at 11:20
  • See `MY SETUP` above. Have I provided enough information? – Greg Peckory Jan 18 '16 at 11:25
  • See new edit on calling the YUV->RGB method – Greg Peckory Jan 18 '16 at 11:29
  • OK, two things here (actually 3): 1. You should check if `int[width*height]` is appropriate - so if it meets your actual array size to avoid the "out of bounds" exception. 2. What you are getting back in your `data` byte-Array depends on the Camera.Parameters that you provide when opening the Camera. So that would be good to know, too. As far as I know, you have to provide same size in those Params as you do for your preview. At least in older Versions I can remember having to do that. And 3. (for your info): Camera-API is deprecated since Api Version 21. There is a new API for Camera. – Fildor Jan 18 '16 at 11:41
  • Thanks a lot for the answer! I'll try respond to those 3 "things". 1/2. I set the picture size to `800x480` using the code `param.setPictureSize(height, width);`. This is why I set the `rgbs` array-size to `800*480` since that is the number of pixels it will produce. Unless `setPictureSize` is not correct, is it? 3. I'm aware it is deprecated, and I suppose I probably should take a look at the new API. But if I want this app to run on older Android versions I need to use the older API. Also, there is much less help online (tutorials etc.) for using the new API. – Greg Peckory Jan 18 '16 at 11:49
  • Try reading the PictureSize after setting it for validation. I've seen devices simply taking on a default-size and not throwing an exception if you try to set an unsupported one leaving you with for example 480x768 when you think you have set 800x480. Another thing you could try is setting 480x800 instead of 800x480. Assuming the resulting Array will have the correct size is like ordering on amazon and not controlling the contents of the received package ;) Just assume Android Camera will do anything to make you freak out. So check and double check. **Do not trust it**! – Fildor Jan 18 '16 at 11:54
  • Yes, now that you say it I think that is causing an issue. Do you think the YUV->RGB conversion will work perfectly once this issue is fixed? I.e - is the rest of the code OK? – Greg Peckory Jan 18 '16 at 12:03
  • Possibly, yes. You could make a little Test-Application and run a test-set on the conversion-code. – Fildor Jan 18 '16 at 12:05
  • I'm also making the assumption that the width-height of the actual phone screen is a compatible preview size? It would be pretty crazy if it wasn't. 800x480 in my case. – Greg Peckory Jan 18 '16 at 12:19
  • Some devices (manufacturers) assume that you turn the phone by 90° when taking photos. So you'll have to check orientation, too. Cameras grew me a lot of grey hair - so glad to have left that behind :D – Fildor Jan 18 '16 at 12:26

0 Answers0