使用Seekbar for Camera2 API手动对焦

时间:2018-10-05 16:50:06

标签: android focus android-camera2 manual

我正在使用Camera2 API开发自定义的Camera App,并尝试通过搜索栏启用手动对焦。我引用了几篇文章,但遇到了可能来自我的Java编码的错误。

在“ CameraCharacteristics features = manager.getCameraCharacteristics(cameraDevice.getId());”行中,我看到一条消息“错误:未报告的异常CameraAccessException;必须被捕获或声明为抛出”。

你们中有人有主意吗?

       public void onStartTrackingTouch(SeekBar seekBar) {

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

        }


        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            progressChangedValue = progress;


            CameraManager manager = (CameraManager)getSystemService(CAMERA_SERVICE);
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());

            float minimumLens = characteristics.get(CameraCharacteristics.LENS_INFO_MINIMUM_FOCUS_DISTANCE);
            float num = (((float)progress) * minimumLens / 100);
            previewBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, num);

        }

1 个答案:

答案 0 :(得分:0)

这是正常的Java编程,与照相机API无关。

getCameraCharacteristics方法可以引发已检查的CameraAccessException异常。 Java语言要求所有已检查的异常都必须由调用可能抛出已检查的异常的方法的函数处理,或者该函数还必须声明它可能会抛出该已检查的异常。

因此,您可以将getCameraCharacteristics调用包围在捕获CameraAccessException的“ try ... catch”块中,也可以在“ public void onProgressChanged ...”的末尾添加“ throws CameraAccessException”。

您可能想要前一种情况:

CameraCharacteristics characteristics;
try {
    characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
} catch (CameraAccessException e) {
    // Code ends up here if getCameraCharacteristics can't get the information.
    // The getReason method on CameraAccessException will tell you why
    switch (e.getReason()) {
        case CameraAccessException.CAMERA_DISABLED:
          ....
        case CameraAccessException.CAMERA_DISCONNECTED:
           ...
    }
}