Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to autofocus Android camera automatically?

I want to autofocus Android camera as soon as camera holds still. Im looking for tutorials or samples how to do it or at least small sample that shows what classes I can use to hook on such events.

like image 243
Alexey Zakharov Avatar asked Mar 25 '13 20:03

Alexey Zakharov


People also ask

How do I unlock my camera autofocus Android?

Open the Camera app and tap the Settings icon. Tap the switch next to Tracking auto-focus to turn it off.

How does mobile camera autofocus work?

Autofocus (AF) on smartphones is the process of determin- ing how to move a camera's lens such that certain scene content is in focus. The underlying algorithms used by AF systems, such as contrast detection and phase differencing, are well established.


3 Answers

For me this worked a treat:

//set camera to continually auto-focus
Camera.Parameters params = c.getParameters();
//*EDIT*//params.setFocusMode("continuous-picture");
//It is better to use defined constraints as opposed to String, thanks to AbdelHady
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
c.setParameters(params);
like image 57
lukas_wins Avatar answered Oct 19 '22 02:10

lukas_wins


Try to use Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO or Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE. See below:

Camera.Parameters params = camera.getParameters();
if (params.getSupportedFocusModes().contains(
    Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
  params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
camera.setParameters(params);

It's important to test whether the phone is supporting your chosen mode before attempting to use it, otherwise setParameters() will throw a runtime exception. (Edit code now working properly)

like image 63
dcoz Avatar answered Oct 19 '22 01:10

dcoz


Following code works for me.

Setting autofocus (preview class):

Parameters params = camera.getParameters();
params.setFocusMode(Parameters.FOCUS_MODE_AUTO);
//some more settings
camera.setParameters(params);

Call camera for shot a picture in case that autofocus is ready (activity class):

public void butClick(View v){
    preview.camera.autoFocus(new AutoFocusCallback() {
        public void onAutoFocus(boolean success, Camera camera) {
            if(success){
                camera.takePicture(shutterCallback, rawCallback, jpegCallback);
            }
        }
    });
}

Get picture (activity class):

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        //do something
    }
};
like image 19
Jens van de Mötter Avatar answered Oct 19 '22 00:10

Jens van de Mötter