0

In my Android app, the focus mode is set to FOCUS_MODE_CONTINUOUS_PICTURE.. Here is some relevant code:

private Camera.AutoFocusCallback _cbAutoFocus = new Camera.AutoFocusCallback() {

    private int _count = 0;
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        if (success) {
            _count++;
            if ((_count % 500) == 0) {
                Log.d("MyCam Focus", Integer.toString(_count));
            }
            _camera.cancelAutoFocus();
        }
        _camera.autoFocus(_cbAutoFocus);
    }
};

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    try {
        if (this._camera != null) {
            this._camera.setPreviewDisplay(holder);
            this._camera.startPreview();
            this._camera.autoFocus(this._cbAutoFocus);
        }
    }catch(Exception e) {
        Log.e("Camera Surface change", e.getMessage());
    }
}

As I move the camera over some printed text, the camera seems to auto-focus properly for some time. However, after a while, it stops focusing and I don't get success as true in my onAutoFocus code. Wondering if anyone has any insights. Regards.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Peter
  • 11,260
  • 14
  • 78
  • 155

2 Answers2

0

Did you see this in the API documentation?:

Applications can call autoFocus(AutoFocusCallback) in this mode. If the autofocus is in the middle of scanning, the focus callback will return when it completes. If the autofocus is not scanning, the focus callback will immediately return with a boolean that indicates whether the focus is sharp or not. The apps can then decide if they want to take a picture immediately or to change the focus mode to auto, and run a full autofocus cycle. The focus position is locked after autoFocus call. If applications want to resume the continuous focus, cancelAutoFocus must be called. Restarting the preview will not resume the continuous autofocus. To stop continuous focus, applications should change the focus mode to other modes.

It seems that when the autoFocus method is called, the auto-focus is locked. Call cancelAutoFocus resume auto-focus.

In the onAutoFocus, at the end of the method (regardless of success), autoFocus is called, which will focus then lock the focus regardless of any further movement of the camera. Is this intentional?

CJBS
  • 15,147
  • 6
  • 86
  • 135
0

It turns out FOCUS_MODE_CONTINUOUS_PICTURE or FOCUS_MODE_CONTINUOUS_VIDEO do not always work even if supported by the device. It seems it is better to use FOCUS_MODE_AUTO and manually call autofocus() each time the focus is desired.

Peter
  • 11,260
  • 14
  • 78
  • 155