Android 一段时间后相机自动对焦停止工作

Android camera auto-focus stops working after some time

在我的 Android 应用中,焦点模式设置为 FOCUS_MODE_CONTINUOUS_PICTURE.。这是一些相关代码:

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());
    }
}

当我将相机移到一些印刷文本上时,相机似乎可以正确自动对焦一段时间。但是,过了一会儿,它停止了聚焦,我的 onAutoFocus 代码也没有成功。想知道是否有人有任何见解。问候。

你在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.

好像autoFocus method is called, the auto-focus is locked. Call cancelAutoFocus恢复自动对焦的时候。

在 onAutoFocus 中,在方法结束时(无论是否成功)调用 autoFocus,无论相机是否有任何进一步移动,它都会对焦然后锁定焦点。这是故意的吗?

事实证明,即使设备支持,FOCUS_MODE_CONTINUOUS_PICTURE 或 FOCUS_MODE_CONTINUOUS_VIDEO 也不总是有效。似乎最好使用 FOCUS_MODE_AUTO 并在每次需要焦点时手动调用 autofocus()