55

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.

Alexey Zakharov
  • 24,694
  • 42
  • 126
  • 197
  • possible duplicate of [Android Camera AutoFocus on Demand](http://stackoverflow.com/questions/5878042/android-camera-autofocus-on-demand) – swiftBoy Sep 26 '13 at 08:09

6 Answers6

122

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);
luke-trimby
  • 1,573
  • 1
  • 9
  • 14
  • 2
    Min SDK 14 for this. Is there a way for Min SDK < 14 ? – patrickjason91 Aug 07 '15 at 05:21
  • Where does this code snippet go? I am working with a heavy Unity3D app (C#) that scans a serial-number, but it seems that the Android device's camera does not auto-focus, therefore the scanning (and character recognition) is really bad. Where should I put this code? –  Jun 09 '17 at 14:34
  • `Camera.Parameters` is deprecated in SDK 21 and above. – Ashutosh Sagar May 31 '18 at 09:11
  • You should check whether the device supports the focus mode as it is done in dcoz' answer to prevent a RuntimeException in `setParameters()` – Doopy Apr 18 '19 at 13:07
63

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)

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
dcoz
  • 1,276
  • 11
  • 10
  • works to autofocus camera, but once I start recording it looses focus again – htafoya Oct 17 '17 at 08:28
  • Nevermind, I was opening the cam again so it lost the first permissions. – htafoya Oct 17 '17 at 08:55
  • I just did some testing. You might want to use `Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE` instead if you want to do text/barcode recognition or focusing on a piece of paper. – Patrick Nov 13 '18 at 22:16
  • whats your point here? Did you get better results in the stream with continuous picture? Normally the recommended mode for a stream is continuous video – Fabian Köbel Jun 08 '20 at 09:34
19

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
    }
};
  • answer of "Jens van de Mötter" is better than others,because the camera is auto focus only when we are going to capture.so i think it saves bit energy. – naseer mohammad Jan 11 '17 at 09:26
  • Why does setting the autofocus go in preview class? Seems to me like activity class would be more appropriate. – Kartik Chugh Dec 04 '17 at 05:07
7

Looks like you should continuous autofocus as is discussed here.

There is a question here that you can reference.

Community
  • 1
  • 1
Kgrover
  • 2,106
  • 2
  • 36
  • 54
1

This works perfectly for preview callback:

Camera.Parameters parameters = camera.getParameters();
if (parameters.getSupportedFocusModes().contains(
        Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) {
    parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
}
camera.setParameters(parameters);
gung - Reinstate Monica
  • 11,583
  • 7
  • 60
  • 79
0

you can try code:

ShutterCallback _pfnShutterCallback = new ShutterCallback() {

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

        }

    };

    PictureCallback _pfnRawPictureCallback = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub

        }
    };

    private AutoFocusCallback _pfnAutoFocusCallback = new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {
            // TODO Auto-generated method stub
            camera.autoFocus(null);
            camera.takePicture(_pfnShutterCallback, _pfnRawPictureCallback,
                    mPicture);

        }
    };
    private PictureCallback mPicture = new PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            new SavePhotoTask().execute(data);
            camera.startPreview();

        }
    };
class SavePhotoTask extends AsyncTask<byte[], String, String> {
// Process save file image
}

call capture : mCamera.autoFocus(autoFocusCallback);

You can refer at: http://android-er.blogspot.com/2011/01/start-camera-auto-focusing-autofocus.html

D T
  • 3,522
  • 7
  • 45
  • 89