创建固定焦距预览所需的最小CaptureRequest是什么

时间:2016-09-20 07:40:06

标签: android camera samsung-mobile android-camera2 samsung-galaxy-camera

下面的代码段无法为固定焦距的预览创建CaptureRequest。即使TotalCaptureRequest的报告另有说明,预览中显示的图像始终聚焦在无限远处。

以下是代码段:

mPreviewBuilder = mCameraDevice.createCaptureRequest(SCameraDevice.TEMPLATE_PREVIEW);
mPreviewBuilder.set(SCaptureRequest.LENS_FOCUS_DISTANCE, 9.5f);
mPreviewBuilder.set(SCaptureRequest.CONTROL_AF_MODE, SCaptureRequest.CONTROL_AF_MODE_OFF);

我的设备是三星Galaxy S7 ,它会报告后置摄像头的以下属性: [code] CamId:0 LensFace:BACK HwdSupportLevel:FULL SensorOrientation:90 LensCalibration:CALIBRATED LensMinFocusDist:10.0 [/ code]

这是我在onCaptureComplete()

期间捕获的日志
 callback: AF_STATE: 0 Lens Focus Distance: 9.555555 LENS_STATE: 0 HAS FOCUS RANGE: true Focus Range Min: 9.762765, Max: 9.348346

我尝试使用普通的Android Camera2 API以及Samsung SDK(在此处找到http://developer.samsung.com/galaxy/camera

三星自己的相机应用程序以及Camera FV-5支持此功能,没有任何问题。

如何让它发挥作用?

为上下文添加更多代码

/**
 * Start the camera preview.
 */
private void startPreview() {
    if (null == mCameraDevice || !mTextureView.isAvailable() || null == mPreviewSize) {
        return;
    }
    try {
        closePreviewSession();
        SurfaceTexture texture = mTextureView.getSurfaceTexture();
        assert texture != null;
        texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        mPreviewBuilder = mCameraDevice.createCaptureRequest(SCameraDevice.TEMPLATE_PREVIEW);
        //mPreviewBuilder.set(SCaptureRequest.CONTROL_CAPTURE_INTENT, SCaptureRequest.CONTROL_CAPTURE_INTENT_MANUAL);
        //mPreviewBuilder.set(SCaptureRequest.CONTROL_MODE, SCaptureRequest.CONTROL_MODE_OFF);
        mPreviewBuilder.set(SCaptureRequest.CONTROL_AF_MODE, SCaptureRequest.CONTROL_AF_MODE_OFF);
        mPreviewBuilder.set(SCaptureRequest.CONTROL_AE_MODE, SCaptureRequest.CONTROL_AE_MODE_ON);
        mPreviewBuilder.set(SCaptureRequest.LENS_FOCUS_DISTANCE, 9.5f);

        Surface previewSurface = new Surface(texture);
        mPreviewBuilder.addTarget(previewSurface);

        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface), new SCameraCaptureSession.StateCallback() {

            @Override
            public void onConfigured(SCameraCaptureSession cameraCaptureSession) {
                mPreviewSession = cameraCaptureSession;
                updatePreview();
            }

            @Override
            public void onConfigureFailed(SCameraCaptureSession cameraCaptureSession) {
                Toast.makeText(getApplicationContext(),"Failed", Toast.LENGTH_SHORT).show();
            }
        }, mBackgroundHandler);


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


/**
 * Update the camera preview. {@link #startPreview()} needs to be called in advance.
 */
private void updatePreview() {
    if (null == mCameraDevice) {
        return;
    }
    try {
        setUpCaptureRequestBuilder(mPreviewBuilder);
        HandlerThread thread = new HandlerThread("CameraPreview");
        thread.start();
        mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), mCaptureCallBack, mBackgroundHandler);
        Log.i(TAG, "New Session values, control mode " + mPreviewBuilder.get(SCaptureRequest.CONTROL_MODE) +
                ", AF mode: " + mPreviewBuilder.get(SCaptureRequest.CONTROL_AF_MODE) +
                ", Focus value; " + +mPreviewBuilder.get(SCaptureRequest.LENS_FOCUS_DISTANCE));
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

您是否在mPreviewBuilder中添加了表面?

 Surface surface = new Surface(texture);
 //Add surfaces
 mPreviewBuilder.addTarget(surface);
 surfaces.add(surface);

其次,在设置LENS_FOCUS_DISTANCE之前将AF_MODE设置为关闭,最后使用CaptureSesion上的setRepeatingRequest更新您的sesion。有了它,它必须工作。

 mCaptureSession.setRepeatingRequest(mPreviewBuilder.build(),
                callback, mHandler);

使用日志检查CaptureRequest的值,检查一切是否正确,并且您的AF不会重叠:

 Log.i(TAG, "New Session values, control mode " + mPreviewRequestBuilder.get(CaptureRequest.CONTROL_MODE) +
            ", AF mode: " + mPreviewRequestBuilder.get(CaptureRequest.CONTROL_AF_MODE) +
            ", Focus value; " + +mPreviewRequestBuilder.get(CaptureRequest.LENS_FOCUS_DISTANCE));