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

2024年10月27日 第44讲 AF自动对焦实战 极客笔记

本讲是Android Camera专题系列的第44讲,我们介绍Android Camera2 API专题的AF自动对焦实战。

更多资源:

资源 描述
在线课程 极客笔记在线课程
知识星球 星球名称:深入浅出Android Camera
星球ID: 17296815
Wechat 极客笔记圈

连续(被动)对焦(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结果

本文链接:http://so.lmcjl.com/news/16319/

展开阅读全文