Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autoFocus throwing exception

Tags:

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.access$1500(ActivityThread.java:135) 01-11 16:09:38.466: W/System.err(26546):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054) 01-11 16:09:38.466: W/System.err(26546):    at android.os.Handler.dispatchMessage(Handler.java:99) 01-11 16:09:38.466: W/System.err(26546):    at android.os.Looper.loop(Looper.java:150) 01-11 16:09:38.476: W/System.err(26546):    at android.app.ActivityThread.main(ActivityThread.java:4385) 01-11 16:09:38.476: W/System.err(26546):    at java.lang.reflect.Method.invokeNative(Native Method) 01-11 16:09:38.476: W/System.err(26546):    at java.lang.reflect.Method.invoke(Method.java:507) 01-11 16:09:38.476: W/System.err(26546):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849) 01-11 16:09:38.476: W/System.err(26546):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607) 01-11 16:09:38.476: W/System.err(26546):    at dalvik.system.NativeStart.main(Native Method) 
like image 265
MikeIsrael Avatar asked Jan 11 '12 14:01

MikeIsrael


1 Answers

Use SurfaceHolder.Callback -> surfaceCreated to know when you can start the AutoFocus. If the surfaceholder isn't created (lasted some time) autofocus will fail.

like image 85
hitzi Avatar answered Sep 30 '22 18:09

hitzi