Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Another thing you may want to investigate:

I am experiencing this issue for devices running Motoblur and updated to 2.3 (especially Droid2, DroidX and Atrix with Verizon).

The Camera parameters were fine, but in layout/capture.xml the background of the ViewfinderView is set to transparent:

<com.google.zxing.client.android.ViewfinderView
   android:id="@+id/viewfinder_view" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent" 
   android:background="@color/transparent"
/>

Well, it looks that transparent for Motoblur on Android 2.3 is not that transparent...

removing

android:background="@color/transparent"

from the ViewFinder solved my problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top