设备输入(C/C++)
===========

在开发相机应用时，需要先[申请相关权限](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-preparation)。

相机应用通过调用和控制相机设备，完成预览、拍照和录像等基础操作。

#### 开发步骤

详细的API说明请参考[OH\_Camera](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-oh-camera)。

1.  导入NDK接口。选择系统提供的NDK接口能力，导入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
        libace\_napi.z.so
        libohcamera.so
        libhilog\_ndk.z.so
    )
    
3.  通过[OH\_CameraManager\_CreateCameraInput()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-camera-manager-h#oh_cameramanager_createcamerainput)方法，获取cameraInput对象。
    
    // 监听cameraInput错误信息。
    void OnCameraInputError(const Camera\_Input\* cameraInput, Camera\_ErrorCode errorCode)
    {
       OH\_LOG\_INFO(LOG\_APP, "OnCameraInput errorCode: %{public}d", errorCode);
    }
    
    CameraInput\_Callbacks\* GetCameraInputListener(void)
    {
       static CameraInput\_Callbacks cameraInputCallbacks = {
          .onError = OnCameraInputError
       };
       return &cameraInputCallbacks;
    }
    
    // 监听cameraStatus信息。
    void CameraManagerStatusCallback(Camera\_Manager\* cameraManager, Camera\_StatusInfo\* status)
    {
        OH\_LOG\_INFO(LOG\_APP, "CameraManagerStatusCallback is called");
    }
    
    CameraManager\_Callbacks\* GetCameraManagerListener()
    {
        static CameraManager\_Callbacks cameraManagerListener = {
            .onCameraStatus = CameraManagerStatusCallback
        };
        return &cameraManagerListener;
    }
    
    void CreateAndOpenCamera()
    {
        Camera\_Manager\* cameraManager = nullptr;
        Camera\_Input\* cameraInput = nullptr;
        Camera\_Device\* cameras = nullptr;
        uint32\_t size = 0;
        uint32\_t cameraDeviceIndex = 0;
        // 创建CameraManager对象。
        Camera\_ErrorCode ret = OH\_Camera\_GetCameraManager(&cameraManager);
        if (cameraManager == nullptr || ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_Camera\_GetCameraManager failed.");
            return;
        }
        // 监听相机状态变化。
        ret = OH\_CameraManager\_RegisterCallback(cameraManager, GetCameraManagerListener());
        if (ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraManager\_RegisterCallback failed.");
        }
        // 获取相机列表。
         ret = OH\_CameraManager\_GetSupportedCameras(cameraManager, &cameras, &size);
         if (cameras == nullptr || size == 0 || ret != CAMERA\_OK) {
             OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraManager\_GetSupportedCameras failed.");
             return;
         }
        // 创建相机输入流。
        ret = OH\_CameraManager\_CreateCameraInput(cameraManager, &cameras\[cameraDeviceIndex\], &cameraInput);
        if (cameraInput == nullptr || ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraManager\_CreateCameraInput failed.");
            return;
        }
        ret = OH\_CameraInput\_RegisterCallback(cameraInput, GetCameraInputListener());
        if (ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraInput\_RegisterCallback failed.");
        }
        // 打开相机。
        ret = OH\_CameraInput\_Open(cameraInput);
        if (ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraInput\_open failed.");
            return;
        }
    }
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/88/v3/r1akwLcoT2qKqaF3Srg6lg/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132524Z&HW-CC-Expire=86400&HW-CC-Sign=12E4A95D6419FDEC50BAC4551D2B9433388D9CAF4EB2611671197CB4E1C4E608)
    
    在相机设备输入之前需要先完成相机管理，详细开发步骤请参考[相机管理](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/native-camera-device-management)。
    
4.  通过[OH\_CameraManager\_GetSupportedSceneModes()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-camera-manager-h#oh_cameramanager_getsupportedscenemodes)方法，获取当前相机设备支持的模式列表，列表中存储了相机设备支持的所有模式[Camera\_SceneMode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-camera-h#camera_scenemode)。
    
    bool IsSupportedSceneMode(Camera\_Device camera, Camera\_SceneMode sceneMode)
    {
        Camera\_SceneMode\* sceneModes = nullptr;
        uint32\_t sceneModeSize = 0;
        Camera\_ErrorCode ret = OH\_CameraManager\_GetSupportedSceneModes(&camera, &sceneModes, &sceneModeSize);
        if (sceneModes == nullptr || ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraManager\_GetSupportedSceneModes failed.");
            return false;
        }
        for (uint32\_t index = 0; index < sceneModeSize; index++) {
            OH\_LOG\_INFO(LOG\_APP, "scene mode = %{public}u ", sceneModes\[index\]);    // 获取相机指定模式。
            if (sceneModes\[index\] == sceneMode) {
                return true;
            }
        }
        return false;
    }
    
5.  通过[OH\_CameraManager\_GetSupportedCameraOutputCapabilityWithSceneMode()](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-camera-manager-h#oh_cameramanager_getsupportedcameraoutputcapabilitywithscenemode)方法，获取当前设备在当前模式下支持的所有输出流，如预览流、拍照流等。输出流在[Camera\_OutputCapability](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-oh-camera-camera-outputcapability)中的各个profile字段中。根据相机设备指定模式[Camera\_SceneMode](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-camera-h#camera_scenemode)的不同，需要向Session中添加对应类型的输出流，请参考会话管理[开发步骤](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/native-camera-session-management#开发步骤)中的第6步。
    
    Camera\_OutputCapability\* GetSupportedCameraOutputCapability(Camera\_Manager\* cameraManager, Camera\_Device &camera)
    {
        Camera\_OutputCapability\* cameraOutputCapability = nullptr;
        // 示例代码以NORMAL\_PHOTO模式为例，查询NORMAL\_PHOTO在camera中是否支持。
        bool isSupported = IsSupportedSceneMode(camera, Camera\_SceneMode::NORMAL\_PHOTO);
        if (!isSupported) {
            OH\_LOG\_ERROR(LOG\_APP, "NORMAL\_PHOTO is not supported.");
            return cameraOutputCapability;
        }
        // 获取相机设备支持的输出流能力。
        const Camera\_Profile\* previewProfile = nullptr;
        const Camera\_Profile\* photoProfile = nullptr;
        Camera\_ErrorCode ret = OH\_CameraManager\_GetSupportedCameraOutputCapabilityWithSceneMode(cameraManager, &camera,
            Camera\_SceneMode::NORMAL\_PHOTO, &cameraOutputCapability);
        if (cameraOutputCapability == nullptr || ret != CAMERA\_OK) {
            OH\_LOG\_ERROR(LOG\_APP, "OH\_CameraManager\_GetSupportedCameraOutputCapability failed.");
            return cameraOutputCapability;
        }
        // 以NORMAL\_PHOTO模式为例，需要添加预览流、拍照流。
        if (cameraOutputCapability->previewProfiles == nullptr) {
            OH\_LOG\_ERROR(LOG\_APP, "previewProfiles == null");
        } else {
            // 根据所需从cameraOutputCapability->previewProfiles中选择合适的预览分辨率。
            previewProfile = cameraOutputCapability->previewProfiles\[0\];
        }
        if (cameraOutputCapability->photoProfiles == nullptr) {
            OH\_LOG\_ERROR(LOG\_APP, "photoProfiles == null");
        } else {
            // 根据所需从cameraOutputCapability->photoProfiles中选择合适的拍照分辨率。
            photoProfile = cameraOutputCapability->photoProfiles\[0\];
        }
        return cameraOutputCapability;
    }