Android Camera.autoFocus() failed

eclipseWhile coding a camera test program I run into the following problem… Camera.autoFocus(autoFocusCallback) failed the whole time with the error message “autoFocus failed”. The stacktrace contains:

java.lang.RuntimeException: autoFocus failed
at android.hardware.Camera.native_autoFocus

The internet wasn’t a real help on this topic. So I had to dig deeper on my own.
It was a bit tricky to find the error since I used a filter on my application in the logcat output. So I saw only the “autoFocus failed” message. When I switched to “All messages” I saw the camera is complaining that there is no active preview. Huh? But I saw the camera preview on the phone. Yeah, but this preview was created some milliseconds later than the first autoFocus request.

I use the SurfaceHolder.Callback -> surfaceCreated now to know when the surfaceHolder is created. After this I can start the AutoFocus.

Everything runs fine now :)

8 Gedanken zu “Android Camera.autoFocus() failed

  1. My device is Sony Xperia P (android 4.0.4). , autofocus is not working in the applications. Usually my camera runs with microspur then it will focus within 1-3 seconds.in the app camera runs with microspur and doesnt focus. Any solution to this problem, PLEASE?

  2. Add this to your class defination:
    implements SurfaceHolder.Callback

    then this in onResume:

    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(this);

    when surfaceCreated(SurfaceHolder holder) the surface is created and you can go on.

  3. Pingback: Android Camera开发:扫描二维码,周期性循环自动聚焦auto focus挂掉原因分析(preview is not enabled) | riaos

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>