文档管理中心

录像实践(C/C++)

在开发相机应用时,需要先申请相关权限

当前示例提供完整的录像流程及其接口调用顺序的介绍。对于单个流程(如设备输入会话管理录像)的介绍请参考具体章节。

开发流程

在获取到相机支持的输出流能力后,开始创建录像流,开发流程如下。

完整示例

  1. 在CMake脚本中链接相关动态库。

    收起
    自动换行
    深色代码主题
    复制
    1. target_link_libraries(entry PUBLIC
    2. libace_napi.z.so
    3. libohcamera.so
    4. libhilog_ndk.z.so
    5. )
  2. 创建头文件ndk_camera.h。

    收起
    自动换行
    深色代码主题
    复制
    1. #include "ohcamera/camera.h"
    2. #include "ohcamera/camera_input.h"
    3. #include "ohcamera/capture_session.h"
    4. #include "ohcamera/photo_output.h"
    5. #include "ohcamera/preview_output.h"
    6. #include "ohcamera/video_output.h"
    7. #include "ohcamera/camera_manager.h"
    8. class NDKCamera {
    9. public:
    10. ~NDKCamera();
    11. NDKCamera(char* previewId, char* videoId);
    12. };
  3. cpp侧导入NDK接口,并根据传入的SurfaceId进行录像。

    收起
    自动换行
    深色代码主题
    复制
    1. #include "hilog/log.h"
    2. #include <cmath>
    3. bool IsAspectRatioEqual(float videoAspectRatio, float previewAspectRatio)
    4. {
    5. float epsilon = 1e-6f;
    6. return fabsf(videoAspectRatio - previewAspectRatio) <= epsilon;
    7. }
    8. void OnCameraInputError(const Camera_Input* cameraInput, Camera_ErrorCode errorCode)
    9. {
    10. OH_LOG_INFO(LOG_APP, "OnCameraInput errorCode = %{public}d", errorCode);
    11. }
    12. CameraInput_Callbacks* GetCameraInputListener(void)
    13. {
    14. static CameraInput_Callbacks cameraInputCallbacks = {
    15. .onError = OnCameraInputError
    16. };
    17. return &cameraInputCallbacks;
    18. }
    19. void CaptureSessionOnFocusStateChange(Camera_CaptureSession* session, Camera_FocusState focusState)
    20. {
    21. OH_LOG_INFO(LOG_APP, "CaptureSessionOnFocusStateChange");
    22. }
    23. void CaptureSessionOnError(Camera_CaptureSession* session, Camera_ErrorCode errorCode)
    24. {
    25. OH_LOG_INFO(LOG_APP, "CaptureSessionOnError = %{public}d", errorCode);
    26. }
    27. CaptureSession_Callbacks* GetCaptureSessionRegister(void)
    28. {
    29. static CaptureSession_Callbacks captureSessionCallbacks = {
    30. .onFocusStateChange = CaptureSessionOnFocusStateChange,
    31. .onError = CaptureSessionOnError
    32. };
    33. return &captureSessionCallbacks;
    34. }
    35. void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
    36. {
    37. OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
    38. }
    39. void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
    40. {
    41. OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
    42. }
    43. void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
    44. {
    45. OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
    46. }
    47. VideoOutput_Callbacks* GetVideoOutputListener(void)
    48. {
    49. static VideoOutput_Callbacks videoOutputListener = {
    50. .onFrameStart = VideoOutputOnFrameStart,
    51. .onFrameEnd = VideoOutputOnFrameEnd,
    52. .onError = VideoOutputOnError
    53. };
    54. return &videoOutputListener;
    55. }
    56. void CameraManagerStatusCallback(Camera_Manager* cameraManager, Camera_StatusInfo* status)
    57. {
    58. OH_LOG_INFO(LOG_APP, "CameraManagerStatusCallback is called");
    59. }
    60. CameraManager_Callbacks* GetCameraManagerListener()
    61. {
    62. static CameraManager_Callbacks cameraManagerListener = {
    63. .onCameraStatus = CameraManagerStatusCallback
    64. };
    65. return &cameraManagerListener;
    66. }
    67. NDKCamera::NDKCamera(char* previewId, char* videoId)
    68. {
    69. Camera_Manager* cameraManager = nullptr;
    70. Camera_Device* cameras = nullptr;
    71. Camera_CaptureSession* captureSession = nullptr;
    72. Camera_OutputCapability* cameraOutputCapability = nullptr;
    73. Camera_VideoOutput* videoOutput = nullptr;
    74. const Camera_Profile* previewProfile = nullptr;
    75. const Camera_Profile* photoProfile = nullptr;
    76. const Camera_VideoProfile* videoProfile = nullptr;
    77. Camera_PreviewOutput* previewOutput = nullptr;
    78. Camera_PhotoOutput* photoOutput = nullptr;
    79. Camera_Input* cameraInput = nullptr;
    80. uint32_t size = 0;
    81. uint32_t cameraDeviceIndex = 0;
    82. char* videoSurfaceId = videoId;
    83. char* previewSurfaceId = previewId;
    84. // 创建CameraManager对象。
    85. Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
    86. if (cameraManager == nullptr || ret != CAMERA_OK) {
    87. OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
    88. return;
    89. }
    90. // 监听相机状态变化。
    91. ret = OH_CameraManager_RegisterCallback(cameraManager, GetCameraManagerListener());
    92. if (ret != CAMERA_OK) {
    93. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_RegisterCallback failed.");
    94. return;
    95. }
    96. // 获取相机列表。
    97. ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
    98. if (cameras == nullptr || size <= 0 || ret != CAMERA_OK) {
    99. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
    100. return;
    101. }
    102. for (int index = 0; index < size; index++) {
    103. OH_LOG_ERROR(LOG_APP, "cameraId = %{public}s ", cameras[index].cameraId); // 获取相机ID。
    104. OH_LOG_ERROR(LOG_APP, "cameraPosition = %{public}d ", cameras[index].cameraPosition); // 获取相机位置。
    105. OH_LOG_ERROR(LOG_APP, "cameraType = %{public}d ", cameras[index].cameraType); // 获取相机类型。
    106. OH_LOG_ERROR(LOG_APP, "connectionType = %{public}d ", cameras[index].connectionType); // 获取相机连接类型。
    107. }
    108. if (size < cameraDeviceIndex + 1) {
    109. OH_LOG_ERROR(LOG_APP, "cameraDeviceIndex is invalid.");
    110. return;
    111. }
    112. // 获取相机设备支持的输出流能力。
    113. ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
    114. &cameraOutputCapability);
    115. if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
    116. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
    117. return;
    118. }
    119. if (cameraOutputCapability->previewProfiles == nullptr) {
    120. OH_LOG_ERROR(LOG_APP, "previewProfiles == null");
    121. return;
    122. }
    123. previewProfile = cameraOutputCapability->previewProfiles[0];
    124. OH_LOG_INFO(LOG_APP, "previewProfile width: %{public}d, height: %{public}d.", previewProfile->size.width,
    125. previewProfile->size.height);
    126. if (cameraOutputCapability->photoProfiles == nullptr) {
    127. OH_LOG_ERROR(LOG_APP, "photoProfiles == null");
    128. return;
    129. }
    130. photoProfile = cameraOutputCapability->photoProfiles[0];
    131. if (cameraOutputCapability->videoProfiles == nullptr) {
    132. OH_LOG_ERROR(LOG_APP, "videoProfiles == null");
    133. return;
    134. }
    135. // 预览流宽高比要与录像流的宽高比一致,如果录制的是hdr视频,请筛选支持hdr的Camera_VideoProfile。
    136. Camera_VideoProfile** videoProfiles = cameraOutputCapability->videoProfiles;
    137. for (int index = 0; index < cameraOutputCapability->videoProfilesSize; index++) {
    138. bool isEqual = IsAspectRatioEqual((float)videoProfiles[index]->size.width / videoProfiles[index]->size.height,
    139. (float)previewProfile->size.width / previewProfile->size.height);
    140. // 默认筛选CAMERA_FORMAT_YUV_420_SP的profile。
    141. if (isEqual && videoProfiles[index]->format == Camera_Format::CAMERA_FORMAT_YUV_420_SP) {
    142. videoProfile = videoProfiles[index];
    143. OH_LOG_INFO(LOG_APP, "videoProfile width: %{public}d, height: %{public}d.", videoProfile->size.width,
    144. videoProfile->size.height);
    145. break;
    146. }
    147. }
    148. if (videoProfile == nullptr) {
    149. OH_LOG_ERROR(LOG_APP, "Get videoProfile failed.");
    150. return;
    151. }
    152. // 创建VideoOutput对象。
    153. ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput);
    154. if (videoOutput == nullptr || ret != CAMERA_OK) {
    155. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
    156. return;
    157. }
    158. // 监听视频输出错误信息。
    159. ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
    160. if (ret != CAMERA_OK) {
    161. OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed.");
    162. }
    163. // 创建会话。
    164. ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
    165. if (captureSession == nullptr || ret != CAMERA_OK) {
    166. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
    167. return;
    168. }
    169. // 监听session错误信息。
    170. ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
    171. if (ret != CAMERA_OK) {
    172. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
    173. }
    174. // 开始配置会话。
    175. ret = OH_CaptureSession_BeginConfig(captureSession);
    176. if (ret != CAMERA_OK) {
    177. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
    178. return;
    179. }
    180. // 创建相机输入流。
    181. ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[cameraDeviceIndex], &cameraInput);
    182. if (cameraInput == nullptr || ret != CAMERA_OK) {
    183. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCameraInput failed.");
    184. return;
    185. }
    186. // 监听cameraInput错误信息。
    187. ret = OH_CameraInput_RegisterCallback(cameraInput, GetCameraInputListener());
    188. if (ret != CAMERA_OK) {
    189. OH_LOG_ERROR(LOG_APP, "OH_CameraInput_RegisterCallback failed.");
    190. }
    191. // 打开相机。
    192. ret = OH_CameraInput_Open(cameraInput);
    193. if (ret != CAMERA_OK) {
    194. OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Open failed.");
    195. return;
    196. }
    197. // 向会话中添加相机输入流。
    198. ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
    199. if (ret != CAMERA_OK) {
    200. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
    201. return;
    202. }
    203. // 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface。
    204. ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceId, &previewOutput);
    205. if (previewProfile == nullptr || previewOutput == nullptr || ret != CAMERA_OK) {
    206. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
    207. return;
    208. }
    209. // 向会话中添加预览输出流。
    210. ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
    211. if (ret != CAMERA_OK) {
    212. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
    213. return;
    214. }
    215. // 向会话中添加录像输出流。
    216. ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
    217. if (ret != CAMERA_OK) {
    218. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed.");
    219. return;
    220. }
    221. // 提交会话配置。
    222. ret = OH_CaptureSession_CommitConfig(captureSession);
    223. if (ret != CAMERA_OK) {
    224. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
    225. return;
    226. }
    227. // 启动会话。
    228. ret = OH_CaptureSession_Start(captureSession);
    229. if (ret != CAMERA_OK) {
    230. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
    231. return;
    232. }
    233. // 启动录像输出流。
    234. ret = OH_VideoOutput_Start(videoOutput);
    235. if (ret != CAMERA_OK) {
    236. OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
    237. return;
    238. }
    239. // 开始录像 ts侧调用avRecorder.start()。
    240. // 停止录像输出流。
    241. ret = OH_VideoOutput_Stop(videoOutput);
    242. if (ret != CAMERA_OK) {
    243. OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
    244. }
    245. // 停止录像 ts侧调用avRecorder.stop()。
    246. // 停止当前会话。
    247. ret = OH_CaptureSession_Stop(captureSession);
    248. if (ret == CAMERA_OK) {
    249. OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
    250. } else {
    251. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
    252. }
    253. // 释放相机输入流。
    254. ret = OH_CameraInput_Close(cameraInput);
    255. if (ret == CAMERA_OK) {
    256. OH_LOG_INFO(LOG_APP, "OH_CameraInput_Close success ");
    257. } else {
    258. OH_LOG_ERROR(LOG_APP, "OH_CameraInput_Close failed. %d ", ret);
    259. }
    260. // 释放预览输出流。
    261. ret = OH_PreviewOutput_Release(previewOutput);
    262. if (ret == CAMERA_OK) {
    263. OH_LOG_INFO(LOG_APP, "OH_PreviewOutput_Release success ");
    264. } else {
    265. OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_Release failed. %d ", ret);
    266. }
    267. // 释放录像输出流。
    268. ret = OH_VideoOutput_Release(videoOutput);
    269. if (ret == CAMERA_OK) {
    270. OH_LOG_INFO(LOG_APP, "OH_VideoOutput_Release success ");
    271. } else {
    272. OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Release failed. %d ", ret);
    273. }
    274. // 释放会话。
    275. ret = OH_CaptureSession_Release(captureSession);
    276. if (ret == CAMERA_OK) {
    277. OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Release success ");
    278. } else {
    279. OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Release failed. %d ", ret);
    280. }
    281. // 资源释放。
    282. ret = OH_CameraManager_DeleteSupportedCameras(cameraManager, cameras, size);
    283. if (ret != CAMERA_OK) {
    284. OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
    285. } else {
    286. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameras. ok");
    287. }
    288. ret = OH_CameraManager_DeleteSupportedCameraOutputCapability(cameraManager, cameraOutputCapability);
    289. if (ret != CAMERA_OK) {
    290. OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
    291. } else {
    292. OH_LOG_ERROR(LOG_APP, "OH_CameraManager_DeleteSupportedCameraOutputCapability success");
    293. }
    294. ret = OH_Camera_DeleteCameraManager(cameraManager);
    295. if (ret != CAMERA_OK) {
    296. OH_LOG_ERROR(LOG_APP, "Delete Cameras failed.");
    297. } else {
    298. OH_LOG_ERROR(LOG_APP, "OH_Camera_DeleteCameraManager success");
    299. }
    300. }

示例代码

在 指南 中进行搜索
请输入您想要搜索的关键词