2

I would like to know if there is a way to know when the autofocus is done?

I have a app that doing some image processing on video. My first thing that I need to do is to turn on the flash and the autofocus, my problem is that I don't know if the autofocus is done or not (I need to know it programmatically) so I can start all the image processing only after the auto focus is done.

This is how I configure the camera settings :

@Override
public void surfaceCreated(SurfaceHolder arg0) {
 try {
    camera = Camera.open();

    parameters = camera.getParameters();
    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
    parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
    camera.setParameters(parameters);
    camera.setPreviewDisplay(holder);
    camera.startPreview();

    recorder = new MediaRecorder();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

I just want to be clear, everything is working good, I just want to know when exactly the autofocus is done.

Thanks!

user2235615
  • 1,513
  • 3
  • 17
  • 37

3 Answers3

4

You have to check it first that a camera supports autofocus or not. Check it like this way and you can also check if your phone supports flash light or not follow this link How turn on camera flash light programmatically in Android?

PackageManager pm = getPackageManager();
if(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA) && pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS)){
       // True means the camera has autofocus mode on. Do what ever you want to do 
}
Community
  • 1
  • 1
  • Thanks, but my app work only on Galaxy S3 and S4 so I know for sure that They are support both Autofocus and Flash light. – user2235615 Jan 14 '14 at 07:26
  • Yes but what about the other devices which don't support flashlight and autofocus? There are many devices dont have the feature of `Flashlight` what about these devices? –  Jan 14 '14 at 07:27
  • I accept your comment, although this is not answering my question. but good scalability thinking :) – user2235615 Jan 14 '14 at 07:33
2

Ok, I found the answer for this, I don't know why I don't think about it before. This is the new code :

@Override
public void surfaceCreated(SurfaceHolder arg0) {
 try {
    camera = Camera.open();

    parameters = camera.getParameters();
    parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
    parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
    camera.setParameters(parameters);
    camera.setPreviewDisplay(holder);
    camera.startPreview();
    camera.autoFocus(new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {

        }
    });
    recorder = new MediaRecorder();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
user2235615
  • 1,513
  • 3
  • 17
  • 37
2

The "autoFocus" method in the (now deprecated) Camera class just triggers a single auto focus event and the callback only relates to that single event. It will not be triggered again for future auto focus events.

To know when auto focus starts and stops each time you need to call the setAutoFocusMoveCallback method instead and pass in your AutoFocusMoveCallback instance. The "start" parameter in the callback is true when auto focus begins and false when auto focus completes.

camera.setAutoFocusMoveCallback(new AutoFocusMoveCallback() {
    @Override
    public void onAutoFocusMoving(boolean start, Camera camera) {
        // Do whatever you need to respond to auto focus starting and stopping
    }
});
RowanPD
  • 374
  • 4
  • 14