Java 不带自动对焦的Camera2 api支持设备

Java 不带自动对焦的Camera2 api支持设备,java,android,android-camera2,Java,Android,Android Camera2,当设备没有自动对焦功能时,使用Camera2 api拍照的正确方法是什么 根据对其他问题的回答,到目前为止,我得到的代码是: private void setUpCameraOutputs(int width, int height){ Activity activity = getActivity(); CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE

当设备没有自动对焦功能时,使用Camera2 api拍照的正确方法是什么

根据对其他问题的回答,到目前为止,我得到的代码是:

private void setUpCameraOutputs(int width, int height){
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {

            CameraCharacteristics characteristics
                    = manager.getCameraCharacteristics(cameraId);

            //Check if autofocus is available on camera
            int[] afAvailableModes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES);

            if (afAvailableModes.length == 0 || (afAvailableModes.length == 1
                    && afAvailableModes[0] == CameraMetadata.CONTROL_AF_MODE_OFF)) {
                mAutoFocusSupported = false;
            } else {
                mAutoFocusSupported = true;
            }
            //end autofocus check

    //additional camera setup code here that is not relevant to this problem
}

private void takePicture() {
    if (mAutoFocusSupported) {
        lockFocus();
    } else {
        captureStillPicture();
    }
}

private void lockFocus() {
    try {
        // This is how to tell the camera to lock focus.
        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                CameraMetadata.CONTROL_AF_TRIGGER_START);
        // Tell #mCaptureCallback to wait for the lock.
        mState = STATE_WAITING_LOCK;
        mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                mBackgroundHandler);
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

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

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

        //setFlashMode(captureBuilder);
        setFlashOn(captureBuilder);

        // Orientation
        int captureRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, getOrientation(captureRotation));


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

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

                unlockFocus();

                synchronized(lock) {
                    captureComplete(captureRotation);
                }
            }
        };

        mCaptureSession.stopRepeating();
        mCaptureSession.abortCaptures();
        mCaptureSession.capture(captureBuilder.build(), CaptureCallback, null);

    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}
else
括号内,应显示代码

  • 修改
    mPreviewRequestBuilder
    以在调用捕获之前删除对AF的任何提及
  • 在调用捕获之前更改
    mState
    ,以适当地反映状态更改,指示它将要拍照(但没有锁定)
  • 调用一个独特的
    mCaptureSession
    ,类似于
    lockFocus
    调用的方式,而不是
    captureStillPicture
    (它还引用了
    控制自动对焦模式