Android Camera2焦点状态卡住了

时间:2017-06-13 16:56:29

标签: android android-camera2

我需要在我的应用程序上使用Camera2 API。 (Api21 +) 我找到了下一个样本: https://github.com/googlesamples/android-Camera2Basic

我下载了它并开始使用我的手机。当我按下“图片”按钮时,它会调用takePhoto方法。

b := bar{foo: foo{get: func() int { return 69 }}}
fmt.Println(b.double())
fmt.Println(b.toString())

这是一台国家机器。有时这台机器卡在private void takePicture() { lockFocus(); } 上。 我的设备正在等待Focus,但没有任何反应! (是的,我的设备支持自动对焦)

STATE_WAITING_LOCK

这个问题的好方法是什么? 这个程序有时会在这里崩溃:

case STATE_WAITING_LOCK: {
        Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
        if (afState == null) {
            captureStillPicture();
        } else if (CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) {
            // CONTROL_AE_STATE can be null on some devices
            Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
            if (aeState == null ||
                    aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                mState = STATE_PICTURE_TAKEN;
                captureStillPicture();
            } else {
                runPrecaptureSequence();
            }
        }
        break;
    }

为什么无法关注我的设备?

2 个答案:

答案 0 :(得分:2)

似乎您的程序有时卡在CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED中,根据文档" AF算法无法对焦。镜头不动。" link

尝试取消AF_TRIGGER并重新启动它。像这样:

 if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
          getRidOfNotFocusedLock();
}

private void getRidOfNotFocusedLock(){
        try {
            mPreviewRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
            mCaptureSession.capture(
                    captureRequestBuilder.build(), captureSessionCaptureCallback, backgroundHandler);
            mPreviewRequestBuilder.set(
                    CaptureRequest.CONTROL_AF_TRIGGER, CaptureRequest.CONTROL_AF_TRIGGER_START);

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:1)

当我找到Lautern的答案时,我不得不对其进行一些调整,因为它对我不起作用。
由于程序有时会卡在CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED中,因此您可以尝试重新启动AF_TRIGGER:

if (afState == CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED) {
    restartFocus();
}

private void restartFocus(){
        try {
            mPreviewRequestBuilder.set(
                CaptureRequest.CONTROL_AF_TRIGGER,
                CaptureRequest.CONTROL_AF_TRIGGER_CANCEL);
            mCaptureSession.capture(
                captureRequestBuilder.build(),
                captureSessionCaptureCallback,
                mBackgroundHandler);
            lockFocus();

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

private void lockFocus() {
    try {
        mPreviewRequestBuilder.set(
            CaptureRequest.CONTROL_AF_TRIGGER,
            CaptureRequest.CONTROL_AF_TRIGGER_START);
        mState = STATE_WAIT_LOCK;
        mCaptureSession.capture(
                mPreviewRequestBuilder.build(),
                mPreviewCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

随着时间的流逝,自从我实现此功能以来,我不确定为什么必须重新启动捕获会话。但是没有它,它对我不起作用。