简体   繁体   English

Android Camera2 API,限制焦距的最佳方法?

[英]Android Camera2 API, Best way to limit the focus distance?

I am trying to speed up barcode scanning which is slow with Google's Vision API due to the couple of seconds it takes to auto-focus.由于自动对焦需要几秒钟的时间,我正在尝试加快条码扫描速度,这在使用 Google Vision API 时速度很慢。 So, I want to try limiting the distance the camera tries to auto-focus so that it will always be within only a few cm instead of pulling from infinity.因此,我想尝试限制相机尝试自动对焦的距离,使其始终保持在几厘米之内,而不是从无限远拉。

However, I'm not sure how to achieve this as CaptureRequest doesn't seem to have anything for setting min/max focus distance.但是,我不确定如何实现这一点,因为 CaptureRequest 似乎没有设置最小/最大焦距的任何内容。 If I try to set a distance in the callback class CameraStateWatcher, nothing happens because AF just overrides it.如果我尝试在回调类 CameraStateWatcher 中设置距离,则不会发生任何事情,因为 AF 只是覆盖了它。

Is there somewhere I can override the focus distance value such as in CameraCaptureSession.CaptureCallback during onCaptureStarted etc?在 onCaptureStarted 等期间,是否有可以覆盖焦距值的地方,例如 CameraCaptureSession.CaptureCallback 中的值?

I am using the following code:我正在使用以下代码:

    private class CameraCaptureWatcher extends CameraCaptureSession.CaptureCallback {

        // POSSIBLE TO OVERRIDE THE FOCUS DISTANCE IN HERE TO SET MIN/MAX?

        // oncapturestarted
        // oncapturecompleted

    }

    private class CameraStateWatcher extends CameraDevice.StateCallback {

        @Override
        public void onOpened(@NonNull CameraDevice cameraDevice) {

            TestCamera.this.cameraDevice = cameraDevice;

            List<Surface> surfaces = new ArrayList<>();
            surfaces.add(previewSurface);
            surfaces.add(imageReader.getSurface());

            try {
                CaptureRequest.Builder captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);

                // POSSIBLE TO SET MIN/MAX FOCUS DISTANCE WITH REQUEST?
                captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);

                for ( Surface surface : surfaces ) {
                    captureRequestBuilder.addTarget(surface);
                }

                captureRequest = captureRequestBuilder.build();

                for ( CaptureRequest.Key<?> key : captureRequest.getKeys() ) {
                    Log.d("STATE_WATCHER", "Request Key = " + key.getName());
                }

                cameraDevice.createCaptureSession(surfaces, cameraSurfaceWatcher, handler);

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

        }

    }

}

The only control the API has for this is selecting a MACRO autofocus mode, which isn't necessarily supported on all devices. API 对此的唯一控制是选择 MACRO 自动对焦模式,不一定在所有设备上都支持。

Your sample code doesn't seem to be using autofocus;您的示例代码似乎没有使用自动对焦; which AF mode have you tried using?您尝试使用哪种自动对焦模式? We generally recommend CONTINUOUS_FOCUS_PICTURE for most use cases, when supported by the device.在设备支持的情况下,我们通常建议在大多数用例中使用 CONTINUOUS_FOCUS_PICTURE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM