4

I have a camera app that works on most phone but I got an error from a user today. The error says that the app crashes on Camera.setParameters() I have read This StackOverflow Post about the topic, but have that same solution already implemented.

Here's the code I'm using:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

        camera.setDisplayOrientation(90);
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(w, h);    
        parameters.setPreviewSize(size.width, size.height); // preview size
        camera.setParameters(parameters);
        camera.startPreview();


        Camera.Parameters parameters = camera.getParameters();
        List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();

        // You need to choose the most appropriate previewSize for your app
        Camera.Size previewSize = // .... select one of previewSizes here


    }


private Camera.Size getBestPreviewSize(int width, int height)
    {

        // Get For Photo Size
        Camera.Parameters camparams = camera.getParameters();

        // Find the Best Preview Size
        List<Size> sizes = camparams.getSupportedPreviewSizes();

        Camera.Size result=null;
        int finalHeight = 0;
        for (Size s : sizes) {

            if (s.width <= width && s.height <= height) {
                if (result == null) {
                        result = s;
                        finalHeight = s.height;
                } else {
                        int resultArea=result.width*result.height; 
                        int newArea=s.width*s.height;

                        if (newArea>resultArea) {
                      result=s;
                      finalHeight = s.height;
                     }
                }
            } 
        }

        // Just in case... 
        if (result == null) {
            finalHeight = height;
        }

        result.width = (int)(finalHeight*cameraRatio);


        return result; 

    }

My thought is to put a try / catch around the camera.setParameter(size.width, size.height) but I don't know if that will keep it from crashing upon failure?

Here is the Crash Log that was sent to me on developer.android.com:

java.lang.RuntimeException: setParameters failed at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:953) at net.feltpad.mosaic.Preview.surfaceChanged(CameraPreview.java:145) at android.view.SurfaceView.updateWindow(SurfaceView.java:557) at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.ViewGroup.drawChild(ViewGroup.java:1730) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at android.view.ViewGroup.drawChild(ViewGroup.java:1732) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459) at android.view.View.draw(View.java:6988) at android.widget.FrameLayout.draw(FrameLayout.java:357) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1961) at android.view.ViewRoot.draw(ViewRoot.java:1602) at android.view.ViewRoot.performTraversals(ViewRoot.java:1323) at android.view.ViewRoot.handleMessage(ViewRoot.java:1961) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:150) at android.app.ActivityThread.main(ActivityThread.java:4333) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:507) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) at dalvik.system.NativeStart.main(Native Method)

Community
  • 1
  • 1
Chris
  • 5,485
  • 15
  • 68
  • 130
  • does it have the same `LogCat` as in the question linked? If different, post it here. – gsb Oct 27 '12 at 01:52
  • All I have is the Crash Error that I can see in the Android Developer Console at developer.android.com - though I did see it crash in person. I will add that log to the original post. – Chris Oct 27 '12 at 01:54

2 Answers2

1

This code assumes that you can just set result.width to some dynamically-computed value. This will not work on all devices. The result must be one of the values returned by getSupportedPreviewSizes() to work reliably across all devices, and in your case, it is not necessarily one of those values.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay, I can get that... but then I am back to having a squished image. – Chris Oct 27 '12 at 14:43
  • 1
    @Chris: I am not sure what "squished" means in this context. The way the Camera app does it is to enforce the aspect ratio in the display, not in the data from the camera. See https://github.com/commonsguy/cw-omnibus/tree/master/Camera/Preview and the use of `com.android.camera.PreviewFrameLayout` as a container for the `SurfaceView`. – CommonsWare Oct 27 '12 at 16:48
0

You can have an extended class for CameraHost. Within this class you can override the adjustPreviewParameters functions which result in the wrong result size. Below is my fix for camera preview:

@Override
public Parameters adjustPreviewParameters(Parameters parameters) {
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size cs = sizes.get(0);
    parameters.setPreviewSize(cs.width, cs.height);
    return super.adjustPreviewParameters(parameters);
}
Long Thay
  • 13
  • 5