在Camera2 API中实现点击聚焦 [英] Implement Tap to Focus in Camera2 API

查看:497
本文介绍了在Camera2 API中实现点击聚焦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的自定义相机中实现点击聚焦功能. 这是Google https://github.com/googlesamples/android-Camera2Basic 提供的基本代码.

i want to implement tap to focus feature in my custom camera. This is the basic code provided by Google https://github.com/googlesamples/android-Camera2Basic

这是我认为应该添加功能的代码段 如果有人实现了Camera2 API,请提供帮助!

Here's the code snippet where i think i should add my feature If anyone has implemented the Camera2 API please help!

  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();
    }
}

推荐答案

您需要将自动对焦和自动曝光区域设置为用户点击的区域.

You'll need to set the autofocus and auto-exposure region to the area tapped by the user.

键是 CONTROL_AF_REGIONS CONTROL_AE_REGIONS .

它们的单位位于传感器活动数组坐标系中,因此您必须将UI触摸坐标转换为相对于预览视图的坐标,然后再转换为活动数组坐标.

The units for them are in the sensor active array coordinate system, so you'll have to translate from your UI touch coordinates to coordinates relative to your preview view, and from there, to the active array coordinates.

如果预览的长宽比与传感器的长宽比匹配,则很简单;如果不是,则必须针对创建预览输出所做的裁剪进行调整.目前,此处是最好的裁剪方法.请注意,如果您还要应用缩放,则还需要在计算中包括缩放因子.

If the aspect ratio of your preview matches that of the sensor, then that's straightforward; if not, you'll have to adjust for the crop that is done to create the preview output. The best diagram for how the cropping works is currently here. Note that if you're also applying zoom, you'll want to include the zoom factor as well in your calculations.

计算完区域后,您可能需要将AF模式设置为AUTO(而不是通常用于常规预览的CONTINUOUS_PICTURE),然后触发AF.收敛AF后(查看捕获结果中的AF状态,等待AF_STATE_FOCUSED_LOCKED),可以拍摄清晰的照片.如果您想在一段时间后返回到正常操作状态,或者用户取消对焦点的触摸,请将AF模式切换回CONTINUOUS_PICTURE.

Once you've calculated the region, you'll probably want to set the AF mode to AUTO (instead of CONTINUOUS_PICTURE which is usually used for normal preview), and then trigger AF. Once you converge AF (look at the AF state in the capture results, wait for AF_STATE_FOCUSED_LOCKED), you're good to take a picture that's in focus. If you want to return to normal operation after some time or the user cancels the touch to focus, switch AF mode back to CONTINUOUS_PICTURE.

这篇关于在Camera2 API中实现点击聚焦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆