使用Camera 2 Android录制视频时,如何在按钮点击时重新聚焦相机?

时间:2017-01-22 23:53:47

标签: android android-camera android-camera2

我使用googlesample制作了一款相机Android应用。我需要相机将重新聚焦于视频录制的开始以及每次用户在录制视频时按下对焦按钮。我在哪里可以找到如何实现它的示例?

这是我的Camera2VideoFragment及其parent

1 个答案:

答案 0 :(得分:0)

您需要添加捕捉回叫以将焦点控制添加到您的相机会话。

private boolean firstCaptureDone = false;
private CameraCaptureSession.CaptureCallback captureCallback = null;

captureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);

            boolean cameraFocussed = false;

            private void process(CaptureResult result) {
                int afState = result.get(CaptureResult.CONTROL_AF_STATE);
                if (CaptureResult.CONTROL_AF_TRIGGER_START == afState) {
                    if (cameraFocussed) {
                        if (firstCaptureDone) {
                            firstCaptureDone = true;
                            Log.d(TAG, "firstCapture Done with focus");
                        }
                    }
                }
                if (CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState) {
                    cameraFocussed = true;
                } else {
                    cameraFocussed = false;
                }
            }

        };


mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), captureCallback, mBackgroundHandler);