对焦(C/C++)
=========

相机框架提供对设备对焦的能力，业务应用可以根据使用场景进行对焦模式和对焦点的设置。

#### 开发步骤

详细的API说明请参考[Camera API参考](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h)。

1.  导入NDK接口，导入方法如下。
    
    // 导入NDK接口头文件
    #include "hilog/log.h"
    #include "ohcamera/camera.h"
    #include "ohcamera/camera\_input.h"
    #include "ohcamera/capture\_session.h"
    #include "ohcamera/photo\_output.h"
    #include "ohcamera/preview\_output.h"
    #include "ohcamera/video\_output.h"
    #include "ohcamera/camera\_manager.h"
    
2.  在CMake脚本中链接相关动态库。
    
    target\_link\_libraries(entry PUBLIC libohcamera.so libhilog\_ndk.z.so)
    
3.  调用[OH\_CaptureSession\_SetFocusMode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_setfocusmode)设置对焦模式。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/f5/v3/J3juwr_JT3W5S0Flecshow/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132525Z&HW-CC-Expire=86400&HW-CC-Sign=1FD6479C5DB140F7B3EBB6E8ECE8308C0C53E948BEE28ABD8D12948DB46F2111)
    
    *   需要先调用[OH\_CaptureSession\_IsFocusModeSupported](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_isfocusmodesupported)检查设备是否支持指定的焦距模式。
        
    *   需要在Session调用[OH\_CaptureSession\_CommitConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_commitconfig)完成配流之后调用。
        
    
    Camera\_ErrorCode SetFocusMode(Camera\_CaptureSession \*captureSession, uint32\_t mode)
    {
        bool isFocusModeSupported = false;
        Camera\_FocusMode focusMode = static\_cast<Camera\_FocusMode>(mode);
        Camera\_ErrorCode ret = OH\_CaptureSession\_IsFocusModeSupported(captureSession, focusMode, &isFocusModeSupported);
        if (&isFocusModeSupported == nullptr || ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "IsFocusModeSupported failed.");
            return CAMERA\_INVALID\_ARGUMENT;
        }
    
        if (!isFocusModeSupported) {
            OH\_LOG\_INFO(LOG\_APP, "current focusMode(%{public}d) is not supported.", focusMode);
            return CAMERA\_OK;
        }
    
        OH\_LOG\_INFO(LOG\_APP, "OH\_CaptureSession\_SetFocusMode focusMode(%{public}d).", focusMode);
        ret = OH\_CaptureSession\_SetFocusMode(captureSession, focusMode);
        if (ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "SetFocusMode failed.");
            return CAMERA\_INVALID\_ARGUMENT;
        }
        return ret;
    }
    
4.  如果通过[OH\_CaptureSession\_SetFocusMode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_setfocusmode)设置对焦模式为自动对焦模式，支持调用[OH\_CaptureSession\_SetFocusPoint](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_setfocuspoint)设置对焦点，根据对焦点执行一次自动对焦。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/2f/v3/-FOB4wmPTRqcCZmoE-GESA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132525Z&HW-CC-Expire=86400&HW-CC-Sign=57F56F131FA89C29C308DED78EB3C2840239BD6251488FB2790AF0A809AFE25A)
    
    需要在Session调用[OH\_CaptureSession\_CommitConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_commitconfig)完成配流之后调用。
    
    Camera\_ErrorCode SetFocusPoint(Camera\_CaptureSession \*captureSession, float x, float y)
    {
        Camera\_Point focusPoint;
        focusPoint.x = x;
        focusPoint.y = y;
        Camera\_ErrorCode ret = OH\_CaptureSession\_SetFocusPoint(captureSession, focusPoint);
        if (ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "SetFocusPoint failed.");
            return CAMERA\_INVALID\_ARGUMENT;
        }
        return ret;
    }
    

#### 状态监听

在相机应用开发过程中，可以随时监听相机聚焦的状态变化。

通过注册[OnFocusStateChange](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-capture-session-h#oh_capturesession_onfocusstatechange)的回调函数获取监听结果，仅当自动对焦模式时，且相机对焦状态发生改变时触发该事件。

void CaptureSessionOnFocusStateChange(Camera\_CaptureSession\* captureSession, Camera\_FocusState focusState)
    {
        OH\_LOG\_INFO(LOG\_APP, "CaptureSession\_Callbacks CaptureSessionOnFocusStateChange");
        OH\_LOG\_INFO(LOG\_APP, "CaptureSession focusState = %{public}d", focusState);
        // 为保证对焦功能的用户体验，在自动对焦成功后，可将对焦模式设置为连续自动对焦
        if (focusState == Camera\_FocusState::FOCUS\_STATE\_FOCUSED) {
            Camera\_ErrorCode ret = SetFocusMode(captureSession, Camera\_FocusMode::FOCUS\_MODE\_CONTINUOUS\_AUTO);
        }
    }

    void CaptureSessionOnError(Camera\_CaptureSession\* captureSession, Camera\_ErrorCode errorCode)
    {
        OH\_LOG\_INFO(LOG\_APP, "CaptureSession\_Callbacks CaptureSessionOnError");
        OH\_LOG\_INFO(LOG\_APP, "CaptureSession errorCode = %{public}d", errorCode);
    }

    CaptureSession\_Callbacks\* GetCaptureSessionRegister(void)
    {
        static CaptureSession\_Callbacks captureSessionCallbacks = {
            .onFocusStateChange = CaptureSessionOnFocusStateChange,
            .onError = CaptureSessionOnError
        };
        return &captureSessionCallbacks;
    }

Camera\_ErrorCode RegisterCallback(Camera\_CaptureSession\* captureSession)
{
    Camera\_ErrorCode ret = OH\_CaptureSession\_RegisterCallback(captureSession, GetCaptureSessionRegister());
    if (ret != CAMERA\_OK) {
        OH\_LOG\_ERROR(LOG\_APP, "OH\_CaptureSession\_RegisterCallback failed.");
    }
    return ret;
}