如何花时间直到焦点正在拍摄图像而不是拍照?camera2API

Ale*_*nko 5 java camera android android-camera

我使用相机2 API.

我使用标准谷歌样本的相机.

问题是代码允许用户拍照,即使图像不在焦点...

private final CameraCaptureSession.CaptureCallback mCaptureCallback
        = new CameraCaptureSession.CaptureCallback() {

    @Override
    public void onCaptureProgressed(@NonNull CameraCaptureSession session,
                                    @NonNull CaptureRequest request,
                                    @NonNull CaptureResult partialResult) {
    }

    @Override
    public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                   @NonNull CaptureRequest request,
                                   @NonNull TotalCaptureResult result) {
        process(result);
    }

    private void process(CaptureResult result) {
        switch (mState) {
            case CameraHelper.STATE_PREVIEW: {
                // We have nothing to do when the camera preview is working normally.

                break;
            }
            case CameraHelper.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 = CameraHelper.STATE_PICTURE_TAKEN;
                        captureStillPicture();
                    } else {
                        runPrecaptureSequence();
                    }
                }
                break;
            }
            case CameraHelper.STATE_WAITING_PRECAPTURE: {
                // 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_PRECAPTURE ||
                        aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED) {
                    mState = CameraHelper.STATE_WAITING_NON_PRECAPTURE;
                }
                break;
            }
            case CameraHelper.STATE_WAITING_NON_PRECAPTURE: {
                // 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_PRECAPTURE) {
                    mState = CameraHelper.STATE_PICTURE_TAKEN;
                    captureStillPicture();
                }
                break;
            }
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

这是标准的回调

如何在常规相机中实现smth,当用户点击拍照按钮时,第一个相机拍摄焦点,然后才拍照...

但就我而言,即使图片不在焦点,也可以拍照...

我究竟做错了什么?

编辑

private void captureStillPicture() {
    try {
        if (null == cameraDevice) {
            return;
        }
        // This is the CaptureRequest.Builder that we use to take a picture.
        final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
        captureBuilder.addTarget(imageReader.getSurface());

        captureBuilder.set(CaptureRequest.JPEG_QUALITY, (byte) 100);

        // Use the same AE and AF modes as the preview.
        captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
        setAutoFlash(captureBuilder);

        // Orientation
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, CameraHelper.ORIENTATIONS.get(rotation));

        // ???? ????? ??????????, ??? ????? ??????????? ????? ???? ??? ?????? ????? ??????
        final CameraCaptureSession.CaptureCallback CaptureCallback = new CameraCaptureSession.CaptureCallback() {

            @Override
            public void onCaptureCompleted(@NonNull CameraCaptureSession session,
                                           @NonNull CaptureRequest request,
                                           @NonNull TotalCaptureResult result) {
                Logger.logGeneral("LENS_FOCAL_LENGTH : " + request.get(CaptureRequest.LENS_FOCAL_LENGTH));
                unlockFocus();
            }
        };

        captureSession.stopRepeating();
        captureSession.capture(captureBuilder.build(), CaptureCallback, null);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

EDIT2

如果没有设置焦点,也许我不必让用户拍照?这意味着我应该每时每刻跟踪焦点状态,最终当用户点击take picture按钮时我应该检查焦点状态,如果它是焦点 - 拍照,如果没有 - 显示Toast - set up focus

我根据这个问题找到了代码片段

private CameraCaptureSession.CaptureCallback mCaptureCallback
    = new CameraCaptureSession.CaptureCallback() {

private void process(CaptureResult result) {
    switch (mState) {
        case STATE_PREVIEW: {

            int afState = result.get(CaptureResult.CONTROL_AF_STATE);
            if (CaptureResult.CONTROL_AF_TRIGGER_START == afState) {
                if (areWeFocused) {
                    //Run specific task here
                }
            }
            if (CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED == afState) {
                areWeFocused = true;
            } else {
                areWeFocused = false;
            }

            break;
        }
    }
}

@Override
public void onCaptureProgressed(CameraCaptureSession session, CaptureRequest request,
                                CaptureResult partialResult) {
    process(partialResult);
}

@Override
public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request,
                               TotalCaptureResult result) {
    process(result);
}
};
Run Code Online (Sandbox Code Playgroud)

我遇到了这种情况result.get(CaptureResult.CONTROL_AF_STATE); 总是返回0 ... 0它是CaptureResult.CONTROL_AF_STATE_INACTIVE.我在三星S5上面对它进行测试......但同样的代码总是在魅族MX5上返回1或2 ...有什么区别?

我究竟做错了什么?

只有当相机对焦时我才能拍照?