如何在Android CameraX中使用手动对焦?

时间:2019-12-02 10:23:36

标签: android focus setfocus android-camerax

我试图像在Camera2 API中一样,对CameraX进行手动对焦

在Camera2 API中,我使用以下代码

final CaptureRequest.Builder captureBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); 

captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF); 

captureBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, mLensFocusDistance);

可以在android camera X中完成手动对焦吗?如果是这样

预先感谢

2 个答案:

答案 0 :(得分:0)

您应该将TouchListener设置为textureView,然后设置focus(Kotlin):

private fun setUpTapToFocus() {
    textureView.setOnTouchListener { _, event ->
        if (event.action != MotionEvent.ACTION_UP) {
            return false
        }

        val cameraControl = CameraX.getCameraControl(CameraX.LensFacing.BACK) // you can set it to front                     
        val factory = TextureViewMeteringPointFactory(textureView)
        val point = factory.createPoint(event.x, event.y)
        val action = FocusMeteringAction.Builder.from(point).build()
        cameraControl.startFocusAndMetering(action)
        return true
    }
}

希望有帮助

答案 1 :(得分:0)

有一种方法可以通过使用Camera2Interop.Extender访问低级camera2 API在cameraX中使用手动对焦。您应该设置两个额外的选项来预览构建器,例如:

void setFocusDistance(ExtendableBuilder<?> builder, float distance) {
    Camera2Interop.Extender extender = new Camera2Interop.Extender(builder);
    extender.setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF);
    extender.setCaptureRequestOption(CaptureRequest.LENS_FOCUS_DISTANCE, distance);
}

并在构建cameraX预览请求时使用它:

float focusDistance = 0F; // example: infinite focus
Preview.Builder previewBuilder = new Preview.Builder();
setFocusDistance(previewBuilder, focusDistance);
preview = previewBuilder.build();
preview.setSurfaceProvider(viewFinder.getSurfaceProvider());

请注意,您还可以通过这种方式设置其他camera2 CaptureRequest选项。