Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera autofocus callback not happening

I am using the Camera API on the Android and gotten my code to work on several platforms including the Samsung Galaxies and HTC Desire. So far I'm only experiencing a problem on the HTC Desire Z, which is intermittent.

in my code I'm calling the following instructions

camera.startPreview();
camera.autoFocus(autoFocusCallback);

where I have already created the autoFocusCallback class required. I'd like to stress again that this code works on the phones, including the one I'm having problems with, so don't go scrutinizing the code. :) After the callback is called, my code then goes on to take the picture, but that part is irrelevant for now.

The intermittent problem is that for a certain random picture (happens one out of 20-100 times), the callback does not happen. I have verified with my own Log.i()'s that this is the last command performed (i.e. the code does not get to the callback). Debug also shows that no errors are reported.

Just to set your mind at ease, my callback looks something like this

AutoFocusCallback autoFocusCallback = new AutoFocusCallback() {
  @Override
  public void onAutoFocus(boolean success, Camera camera) {
    Log.i("tag","this ran"); 
    ...
    ...
  }
};

The Logcat results for a successful run looks something like this

07-12 10:17:50.564: DEBUG/QualcommCameraHardware(1223): startPreview X
07-12 10:17:50.564: DEBUG/QualcommCameraHardware(1223): autoFocus E
07-12 10:17:50.564: DEBUG/QualcommCameraHardware(1223): autoFocus X
07-12 10:17:50.564: DEBUG/QualcommCameraHardware(1223): runAutoFocus E
07-12 10:17:50.564: DEBUG/QualcommCameraHardware(1223): af start (fd 49)
07-12 10:17:51.184: DEBUG/QualcommCameraHardware(1223): native_set_afmode: ctrlCmd.status == 0
07-12 10:17:51.184: DEBUG/QualcommCameraHardware(1223): af done: 1
07-12 10:17:51.184: DEBUG/QualcommCameraHardware(1223): runAutoFocus X
07-12 10:17:51.184: DEBUG/QualcommCameraHardware(1223): takePicture(479)

But the problematic run is like this

07-12 10:17:52.194: DEBUG/QualcommCameraHardware(1223): startPreview X
07-12 10:17:52.194: DEBUG/QualcommCameraHardware(1223): autoFocus E
07-12 10:17:52.194: DEBUG/QualcommCameraHardware(1223): autoFocus X
07-12 10:17:52.194: DEBUG/QualcommCameraHardware(1223): runAutoFocus E
07-12 10:17:52.194: DEBUG/QualcommCameraHardware(1223): af start (fd 49)

and then it hangs.

I'd like to know if anyone has any ideas about this problem, or if you have experienced something similar? I've only managed to find one thread on the net with similar problems, here it is http://groups.google.com/group/android-developers/browse_thread/thread/75ecb8db0ae02bdb

like image 279
Ian Low Avatar asked Jul 12 '11 02:07

Ian Low


1 Answers

Autofocus calls do not have a timeout. I've encountered some devices where autofocus calls sporadically fail to callback (even in perfect conditions). Where the same code runs just fine on other devices.

My solution was to roll my own timeout mechanism for autofocus. I did it with a scheduled future, but the best timeout implementation may depend on your specific use case.

final ScheduledFuture<?> focusTimeoutFuture = mScheduledExecutorService.schedule(new Runnable() {
        @Override
        public void run()
        {
            takePictureAndCancelAutoFocus();
        }
    }, 3, TimeUnit.SECONDS);// add a 3 second timeout to autofocus

mCamera.autoFocus(new Camera.AutoFocusCallback() {
        @Override
        public void onAutoFocus(boolean b, Camera camera) 
        {                
            // cancel the timeout future if it didn't run already
            boolean canceledFuture = focusTimeoutFuture.cancel(false);
            if(canceledFuture)
            {
                takePictureAndCancelAutoFocus();
            }
        }
}
like image 142
Oren Avatar answered Nov 07 '22 06:11

Oren