1# 录像(C/C++) 2 3录像也是相机应用的最重要功能之一,录像是循环帧的捕获。对于录像的流畅度,开发者可以参考[拍照参考](native-camera-shooting.md)中的步骤5,设置分辨率、闪光灯、焦距、照片质量及旋转角度等信息。 4 5## 开发步骤 6 7详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/_o_h___camera.md)。 8 91. 导入NDK接口,接口中提供了相机相关的属性和方法,导入方法如下。 10 11 ```c++ 12 // 导入NDK接口头文件 13 #include "hilog/log.h" 14 #include "ohcamera/camera.h" 15 #include "ohcamera/camera_input.h" 16 #include "ohcamera/capture_session.h" 17 #include "ohcamera/photo_output.h" 18 #include "ohcamera/preview_output.h" 19 #include "ohcamera/video_output.h" 20 #include "ohcamera/camera_manager.h" 21 ``` 22 232. 在CMake脚本中链接相关动态库。 24 25 ```txt 26 target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so) 27 ``` 28 293. 获取SurfaceId。 30 31 系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface()方法获取SurfaceId。 32 334. 创建录像输出流。 34 35 根据传入的SurfaceId,通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput()方法创建录像输出流。 36 37 ```c++ 38 NDKCamera::NDKCamera(char *str) 39 { 40 Camera_Manager *cameraManager = nullptr; 41 Camera_Device* cameras = nullptr; 42 Camera_OutputCapability* cameraOutputCapability = nullptr; 43 Camera_VideoOutput* videoOutput = nullptr; 44 const Camera_VideoProfile* videoProfile; 45 uint32_t size = 0; 46 uint32_t cameraDeviceIndex = 0; 47 char* videoSurfaceId = str; 48 Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager); 49 if (cameraManager == nullptr || ret != CAMERA_OK) { 50 OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed."); 51 } 52 ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size); 53 if (cameras == nullptr || size < 0 || ret != CAMERA_OK) { 54 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed."); 55 } 56 ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex], 57 &cameraOutputCapability); 58 if (cameraOutputCapability == nullptr || ret != CAMERA_OK) { 59 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed."); 60 } 61 if (cameraOutputCapability->videoProfilesSize < 0) { 62 OH_LOG_ERROR(LOG_APP, "videorofilesSize == null"); 63 } 64 videoProfile = cameraOutputCapability->videoProfiles[0]; 65 // 创建VideoOutput对象 66 ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceId, &videoOutput); 67 if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) { 68 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed."); 69 } 70 } 71 ``` 72 735. 开始录像。 74 75 通过videoOutput的[OH_VideoOutput_Start()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_start)方法启动录像输出流。 76 77 ```c++ 78 // 启动录像输出流 79 ret = OH_VideoOutput_Start(videoOutput); 80 if (ret != CAMERA_OK) { 81 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed."); 82 } 83 ``` 84 856. 停止录像。 86 87 通过videoOutput的[OH_VideoOutput_Stop()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_stop)方法停止录像输出流。 88 89 ```c++ 90 // 停止录像输出流 91 ret = OH_VideoOutput_Stop(videoOutput); 92 if (ret != CAMERA_OK) { 93 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed."); 94 } 95 ``` 96 97 98## 状态监听 99 100在相机应用开发过程中,可以随时监听录像输出流状态,包括录像开始、录像结束、录像流输出的错误。 101 102- 通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。 103 104 ```c++ 105 ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener()); 106 if (ret != CAMERA_OK) { 107 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed."); 108 } 109 ``` 110 ```c++ 111 void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput) 112 { 113 OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart"); 114 } 115 116 VideoOutput_Callbacks* GetVideoOutputListener(void) 117 { 118 static VideoOutput_Callbacks videoOutputListener = { 119 .onFrameStart = VideoOutputOnFrameStart, 120 .onFrameEnd = VideoOutputOnFrameEnd, 121 .onError = VideoOutputOnError 122 }; 123 return &videoOutputListener; 124 } 125 ``` 126 127- 通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。 128 129 ```c++ 130 void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount) 131 { 132 OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount); 133 } 134 ``` 135 136- 通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1)。 137 138 ```c++ 139 void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) 140 { 141 OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode); 142 } 143 ```