第44讲 Android Camera2 API AF自动对焦实战

本讲是Android Camera专题系列的第44讲,我们介绍Android Camera2 API专题的AF自动对焦实战,包括如下内容:

  1. 连续(被动)对焦(Continuous AF)
  2. 主动对焦(Touch AF)

视频在线观看:

加入知识星球与更多Camera同学交流
– 星球名称:深入浅出Android Camera
– 星球ID: 17296815

连续(被动)对焦(Continuous AF)

判断支持哪些AF Mode

int [] supported_focus_modes = characteristics.get(CameraCharacteristics.CONTROL_AF_AVAILABLE_MODES); // Android format
camera_features.supported_focus_values = convertFocusModesToValues(supported_focus_modes, camera_features.minimum_focus_distance); // convert to our format (also resorts)

设置AF Mode

private void setFocusMode(CaptureRequest.Builder builder) {
    if( has_af_mode ) {
        if( MyDebug.LOG )
            Log.i(TAG, "[AF_Practise] setFocusMode change af mode to " + af_mode);
        builder.set(CaptureRequest.CONTROL_AF_MODE, af_mode);
    }
    else {
        if( MyDebug.LOG ) {
            Log.i(TAG, "af mode left at " + builder.get(CaptureRequest.CONTROL_AF_MODE));
        }
    }
}

监听AF Results

if (MyDebug.LOG) {
    if (result.get(CaptureResult.CONTROL_AF_STATE) != null) {
        int afState = result.get(CaptureResult.CONTROL_AF_STATE);
        String afStateStr = "";
        switch (afState) {
            case CaptureResult.CONTROL_AF_STATE_INACTIVE:
                afStateStr = "INACTIVE";
                break;
            case CaptureResult.CONTROL_AF_STATE_PASSIVE_SCAN:
                afStateStr = "PASSIVE_SCAN";
                break;
            case CaptureResult.CONTROL_AF_STATE_PASSIVE_FOCUSED:
                afStateStr = "PASSIVE_FOCUSED";
                break;
            case CaptureResult.CONTROL_AF_STATE_ACTIVE_SCAN:
                afStateStr = "ACTIVE_SCAN";
                break;
            case CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED:
                afStateStr = "FOCUSED_LOCKED";
                break;
            case CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
                afStateStr = "NOT_FOCUSED_LOCKED";
                break;
            case CaptureResult.CONTROL_AF_STATE_PASSIVE_UNFOCUSED:
                afStateStr = "PASSIVE_UNFOCUSED";
                break;
            default:
                break;
        }
        if (mPrintResultCounter % 30 == 0 ||
            result.get(CaptureResult.LENS_STATE) == CaptureResult.LENS_STATE_MOVING) {
            Log.i(TAG, "[AF_Practise] AF_STATE:" + afStateStr +
                    ", af mode:" + result.get(CaptureResult.CONTROL_AF_MODE) +
                    ", af scene changed:" + result.get(CaptureResult.CONTROL_AF_SCENE_CHANGE) +
                    ", lens state:" + result.get(CaptureResult.LENS_STATE) +
                    ", focus distance:" + result.get(CaptureResult.LENS_FOCUS_DISTANCE) +
                    ", focus range:" + result.get(CaptureResult.LENS_FOCUS_RANGE));
        }
    }
}

主动对焦(Touch AF)

GeekCamera2 代码逻辑:

Preview#touchEvent
|---->CameraController2#setFocusAndMeteringArea
|-------->CameraSettings#setAFRegions //设置对焦区域
|---->CameraController2#autofocus // 触发对焦

CameraController2#handleStateChange //监听Touch AF结果

赞(2)
未经允许不得转载:极客笔记 » 第44讲 Android Camera2 API AF自动对焦实战
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

Android Camera2 API

最新文章