9

I want to use the camera preview for image recognition. For my purposes, I need the preview resolution to be as high as possible (and, at the same time, display the preview to the user).

I created a Preview class, extending SurfaceView and set the PreviewSize to 1280x720. I added a PreviewCallBack to get the live Image:

camera = Camera.open();

parameters = camera.getParameters();
parameters.setPreviewSize(1280,720);

camera.setParameters( parameters);
byte[] b = new byte[camera.getParameters().getPreviewSize().width *
  camera.getParameters().getPreviewSize().height *
  ImageFormat.getBitsPerPixel(camera.getParameters().getPreviewFormat()) / 8];
camera.addCallbackBuffer(b);
camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());

try {
 camera.setPreviewDisplay(this.getHolder());
 camera.startPreview()
}

My Byte Array b is 1382400 Byte and my CameraPreviewCallback.onPreviewFrame() function receives those 1382400 Bytes - but only the first 497664 Byte contain data (matching a 768x432 resolution (HTC Desire)).

I tested this on different devices, all with display resolutions of 800x480 (HTC Desire, LG Optimus 3D, Samsung Galaxy S2, Samsung Galaxy Tab, ...). The only device my Code works for is a HTC Desire HD.

Does anyone know how to receive the full 720p resolution as a Byte Array? "Something" seems to reduce the preview resolution to fit the smartphone display.

Regards

Joern

Jörn Buitink
  • 2,906
  • 2
  • 22
  • 33
  • Are you sure that your phone supports 720p? Does Logcat say anything (like "can't set resolution")? – Lennart Dec 05 '11 at 12:49
  • Yep, 720p is supported on all devices as preview size. Logcat does not say anything special - it looks like "something" reduces the preview resolution silently. – Jörn Buitink Dec 05 '11 at 13:50
  • Does getPreviewSize() return the correct size? – Lennart Dec 05 '11 at 13:53
  • Just tested this on my desire, can verify this. The preview size is supported and works, but the returned preview data is smaller as described here. Very weird. –  Dec 05 '11 at 13:54

2 Answers2

8

You can't use any random resolution for a camera preview.

Certain devices only support certain resolutions. You can query these via Camera.Parameters.getSupportedPreviewSizes()¹. If your device doesn't return 1280x720 in this list, you can't use it. Select a supported resolution that comes close to your desired one instead.

¹ You should do so in general before using any resolution, 720p or not

2

Although this is an old question, the answer is unchanged: the list of supported preview sizes for each device is not accurate. The default preview size is, to the best of my knowledge, always valid. Other sizes appear to work at most on a case-by-case basis - you can be reasonably confident that a default preview size won't look incorrect on screen, but that's all. In particular, that Byte Array won't scale to accommodate larger image formats even if they're "supported", and only some of the "supported" preview sizes will be properly rendered at-resolution and without green/pink distortion.

The only reliable way to know that the resolution is correct and the data from onPreviewFrame() is good is to test this manually.

wintermute92
  • 97
  • 1
  • 10