1# 设备输入(C/C++) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--Designer: @leo_ysl--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9在开发相机应用时,需要先[申请相关权限](camera-preparation.md)。 10 11相机应用通过调用和控制相机设备,完成预览、拍照和录像等基础操作。 12 13## 开发步骤 14 15详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/capi-oh-camera.md)。 16 171. 导入NDK接口。选择系统提供的NDK接口能力,导入NDK接口的方法如下。 18 19 ```c++ 20 // 导入NDK接口头文件。 21 #include "hilog/log.h" 22 #include "ohcamera/camera.h" 23 #include "ohcamera/camera_input.h" 24 #include "ohcamera/capture_session.h" 25 #include "ohcamera/photo_output.h" 26 #include "ohcamera/preview_output.h" 27 #include "ohcamera/video_output.h" 28 #include "ohcamera/camera_manager.h" 29 ``` 30 312. 在CMake脚本中链接相关动态库。 32 33 ```txt 34 target_link_libraries(entry PUBLIC 35 libace_napi.z.so 36 libohcamera.so 37 libhilog_ndk.z.so 38 ) 39 ``` 40 413. 通过[OH_CameraManager_CreateCameraInput()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_createcamerainput)方法,获取cameraInput对象。 42 ```c++ 43 // 监听cameraInput错误信息。 44 void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode) 45 { 46 OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode: %{public}d", errorCode); 47 } 48 49 CameraInput_Callbacks* GetCameraInputListener(void) 50 { 51 static CameraInput_Callbacks cameraInputCallbacks = { 52 .onError = OnCameraInputError 53 }; 54 return &cameraInputCallbacks; 55 } 56 ``` 57 ```c++ 58 // 监听cameraStatus信息。 59 void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status) 60 { 61 OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called"); 62 } 63 64 CameraManager_Callbacks* GetCameraManagerListener() 65 { 66 static CameraManager_Callbacks cameraManagerListener = { 67 .onCameraStatus = CameraManagerStatusCallback 68 }; 69 return &cameraManagerListener; 70 } 71 ``` 72 ```c++ 73 void CreateAndOpenCamera() 74 { 75 Camera_Manager* cameraManager = nullptr; 76 Camera_Input* cameraInput = nullptr; 77 Camera_Device* cameras = nullptr; 78 uint32_t size = 0; 79 uint32_t cameraDeviceIndex = 0; 80 // 创建CameraManager对象。 81 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 82 if (cameraManager == nullptr || ret != CAMERA_OK) { 83 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 84 return; 85 } 86 // 监听相机状态变化。 87 ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener()); 88 if (ret != CAMERA_OK) { 89 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed."); 90 } 91 // 获取相机列表。 92 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 93 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 94 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 95 return; 96 } 97 // 创建相机输入流。 98 ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput); 99 if (cameraInput == nullptr || ret != CAMERA_OK) { 100 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed."); 101 return; 102 } 103 ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener()); 104 if (ret != CAMERA_OK) { 105 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed."); 106 } 107 // 打开相机。 108 ret = OH_CameraInput_Open(cameraInput); 109 if (ret != CAMERA_OK) { 110 OH_LOG_ERROR(LOG_APP, "OH_CameraInput_open failed."); 111 return; 112 } 113 } 114 ``` 115 116 > **说明:** 117 > 118 > 在相机设备输入之前需要先完成相机管理,详细开发步骤请参考[相机管理](native-camera-device-management.md)。 119 1204. 通过[OH_CameraManager_GetSupportedSceneModes()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_getsupportedscenemodes)方法,获取当前相机设备支持的模式列表,列表中存储了相机设备支持的所有模式[Camera_SceneMode](../../reference/apis-camera-kit/capi-camera-h.md#camera_scenemode)。 121 122 ```c++ 123 bool IsSupportedSceneMode(Camera_Device camera, Camera_SceneMode sceneMode) 124 { 125 Camera_SceneMode* sceneModes = nullptr; 126 uint32_t sceneModeSize = 0; 127 Camera_ErrorCode ret = OH_CameraManager_GetSupportedSceneModes(&camera, &sceneModes, &sceneModeSize); 128 if (sceneModes == nullptr || ret != CAMERA_OK) { 129 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedSceneModes failed."); 130 return false; 131 } 132 for (int index = 0; index < sceneModeSize; index++) { 133 OH_LOG_INFO(LOG_APP, "scene mode = %{public}u ", sceneModes[index]); // 获取相机指定模式。 134 if (sceneModes[index] == sceneMode) { 135 return true; 136 } 137 } 138 return false; 139 } 140 ``` 141 1425. 通过[OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_getsupportedcameraoutputcapabilitywithscenemode)方法,获取当前设备支持的所有输出流,如预览流、拍照流等。输出流在CameraOutputCapability中的各个profile字段中。根据相机设备指定模式[Camera_SceneMode](../../reference/apis-camera-kit/capi-camera-h.md#camera_scenemode)的不同,需要添加不同类型的输出流。 143 144 145 ```c++ 146 Camera_OutputCapability* GetSupportedCameraOutputCapability(Camera_Manager* cameraManager, Camera_Device &camera) 147 { 148 Camera_OutputCapability* cameraOutputCapability = nullptr; 149 // 示例代码以NORMAL_PHOTO模式为例,查询NORMAL_PHOTO在camera中是否支持。 150 bool isSupported = IsSupportedSceneMode(camera, Camera_SceneMode::NORMAL_PHOTO); 151 if (!isSupported) { 152 OH_LOG_ERROR(LOG_APP, "NORMAL_PHOTO is not supported."); 153 return cameraOutputCapability; 154 } 155 // 获取相机设备支持的输出流能力。 156 const Camera_Profile* previewProfile = nullptr; 157 const Camera_Profile* photoProfile = nullptr; 158 Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameraOutputCapabilityWithSceneMode(cameraManager, &camera, 159 Camera_SceneMode::NORMAL_PHOTO, &cameraOutputCapability); 160 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 161 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 162 return cameraOutputCapability; 163 } 164 // 以NORMAL_PHOTO模式为例,需要添加预览流、拍照流。 165 if (cameraOutputCapability->previewProfiles == nullptr) { 166 OH_LOG_ERROR(LOG_APP, "previewProfiles == null"); 167 } else { 168 // 根据所需从cameraOutputCapability->previewProfiles中选择合适的预览分辨率。 169 previewProfile = cameraOutputCapability->previewProfiles[0]; 170 } 171 if (cameraOutputCapability->photoProfiles == nullptr) { 172 OH_LOG_ERROR(LOG_APP, "photoProfiles == null"); 173 } else { 174 // 根据所需从cameraOutputCapability->photoProfiles中选择合适的拍照分辨率。 175 photoProfile = cameraOutputCapability->photoProfiles[0]; 176 } 177 return cameraOutputCapability; 178 } 179 ```