views:

890

answers:

2

Our application displays a camera preview and it seems to work fine on all phones except for the Motorola Droid where we get a runtime exception when we set the camera parameters:

    java.lang.RuntimeException: setParameters failed
   at android.hardware.Camera.native_setParameters(Native Method)
   at android.hardware.Camera.setParameters(Camera.java:611)
   at com.highwaynorth.andrometer.CameraPreviewSurfaceView.surfaceChanged(CameraPreviewSurfaceView.java:57)
   at android.view.SurfaceView.updateWindow(SurfaceView.java:460)
   at android.view.SurfaceView.dispatchDraw(SurfaceView.java:287)
   at android.view.ViewGroup.drawChild(ViewGroup.java:1525)

Here is the code for surfaceChanged() which is mostly taken from APIDemos

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
   // Now that the size is known, set up the camera parameters and begin
   // the preview.
   Camera.Parameters parameters = mCamera.getParameters();
   parameters.setPreviewSize(w, h);
   parameters.setPictureFormat(PixelFormat.JPEG);
   parameters.setPreviewFormat(PixelFormat.YCbCr_422_SP);
   parameters.setPreviewFrameRate(1);
   mCamera.setParameters(parameters);
   mCamera.startPreview();

}

Does anyone know what is wrong with how we are setting the parameters that would be causing the exception on the Motorola Droid?

+2  A: 

I can tell you your problem is with one of the following two lines:

parameters.setPreviewFormat(PixelFormat.YCbCr_422_SP);
parameters.setPreviewFrameRate(1);

I know this, because the rest of that code is just what I do in some camera samples in my book, and they've been tested on a DROID.

You may wish to use getSupportedPreviewFormats() and getSupportedPreviewFrameRates() on your Camera.Parameters object, to see if the device in question supports the format and frame rate you seek. Note that those methods are new to Android 2.0, so they'll work on the DROID/Milestone (and, presumably, the Nexus One), but nothing else at the time of this writing. If you are targeting older Android API versions, you'll need to use reflection or some classloading tricks to get these methods to work on Android 2.0 and be skipped on older versions.

CommonsWare
You are right. We changed it to match APIDemos exactly and removed the call to setPictureFormat(), setPreviewFormat() and setPreviewFrameRate() and now it works fine on the Droid. Thanks for your detailed answer!
Bryan Bedard
A: 

You should check what preview formats are available to make sure you can run on as many devices as possible.

It looks like DROID supports PixelFormat.YCbCr_422_I PixelFormat.YCbCr_420_SP

you can use the following method to get a list of available formats. getSupportedPreviewFormats()

Pixel Formats

Travis