Alex Rivera | Logout

autoFocus throwing exception

Asked 2012-01-11T14:11:51.407
23

I have tried everything but I'm still unable to solve this issue.

I am implementing a camera feature in an app and everything works fine except the autofocus. When I call autoFocus(), it throws an exception, and I can't understand why. I am running the code on a Desire HD.

Code:

@Override
protected void onStart() {
     super.onStart();

     //grab seurface view and callback
     cameraView = (CameraSurfaceView) findViewById(R.id.cameraView);
     try{
        camera = Camera.open();
        cameraView.setCamera(camera);
        //release previous autofocus and assign new one
        camera.cancelAutoFocus();
        camera.autoFocus(new Camera.AutoFocusCallback() {

                public void onAutoFocus(boolean success, Camera camera) {
                // TODO Auto-generated method stub

                }});
    }
    catch (Exception e) {
        //had an issue accessing the camera prompt user
        //TODO create user prompt
        e.printStackTrace();
    }
}

Stack trace:

01-11 16:09:38.456: W/System.err(26546): java.lang.RuntimeException: autoFocus failed
01-11 16:09:38.456: W/System.err(26546):    at android.hardware.Camera.native_autoFocus(Native Method)
01-11 16:09:38.456: W/System.err(26546):    at android.hardware.Camera.autoFocus(Camera.java:680)
01-11 16:09:38.456: W/System.err(26546):    at com.myapp.MyActivity.onStart(BarcodeScannerActivity.java:57)
01-11 16:09:38.466: W/System.err(26546):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1201)
01-11 16:09:38.466: W/System.err(26546):    at android.app.Activity.performStart(Activity.java:3955)
01-11 16:09:38.466: W/System.err(26546):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1845)
01-11 16:09:38.466: W/System.err(26546):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
01-11 16:09:38.466: W/System.err(26546):    at android.app.ActivityThread.
Edit
Report

1 Answer

8

You might want to make sure that the phone supports autofocus. It is quite easy to check this:

Camera.Parameters p = mCamera.getParameters();
List<String> focusModes = p.getSupportedFocusModes();

if(focusModes != null && focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
    //Phone supports autofocus!
}
else {
    //Phone does not support autofocus!
}
answered 2012-02-03T11:21:06.677

Your Answer