对焦(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/d1/v3/MHXi_xXUS1aqwCUX8jqPvw/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073516Z&HW-CC-Expire=86400&HW-CC-Sign=20D143F405B6B94A3588B5B369C9A366B2B38B427AD087BC09079427E69091E1)
    
    *   需要先调用[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/6/v3/EMkXkyfaSVeWteo8TrADIA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260612T073516Z&HW-CC-Expire=86400&HW-CC-Sign=07C430B6C9A57A23077C95A8E7A6ED1C6DAC0BCF439623CA557FEAC03F3D3519)
    
    需要在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;
}