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 27 libace_napi.z.so 28 libohcamera.so 29 libhilog_ndk.z.so 30 ) 31 ``` 32 333. 获取SurfaceId。 34 35 系统提供的media接口可以创建一个录像AVRecorder实例,通过该实例的getInputSurface()方法获取SurfaceId。 36 374. 创建录像输出流。 38 39 根据传入的SurfaceId,通过CameraOutputCapability类中的videoProfiles,可获取当前设备支持的录像输出流。然后,定义创建录像的参数,通过createVideoOutput()方法创建录像输出流。 40 41 ```c++ 42 Camera_VideoOutput* CreateVideoOutput(Camera_Manager* cameraManager, char* videoSurfaceIdStr, 43 const Camera_VideoProfile* videoProfile) 44 { 45 // 创建VideoOutput对象。 46 Camera_VideoOutput* videoOutput = nullptr; 47 Camera_ErrorCode ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceIdStr, 48 &videoOutput); 49 if (videoProfile == nullptr || videoOutput == nullptr || ret != CAMERA_OK) { 50 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed."); 51 } 52 return videoOutput; 53 } 54 ``` 55 565. 开始录像。 57 58 通过videoOutput的[OH_VideoOutput_Start()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_start)方法启动录像输出流。 59 60 ```c++ 61 // 启动录像输出流。 62 Camera_ErrorCode VideoOutputStart(Camera_VideoOutput* videoOutput) 63 { 64 Camera_ErrorCode ret = OH_VideoOutput_Start(videoOutput); 65 if (ret != CAMERA_OK) { 66 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed."); 67 } 68 return ret; 69 } 70 ``` 71 726. 停止录像。 73 74 通过videoOutput的[OH_VideoOutput_Stop()](../../reference/apis-camera-kit/_o_h___camera.md#oh_videooutput_stop)方法停止录像输出流。 75 76 ```c++ 77 // 停止录像输出流。 78 Camera_ErrorCode VideoOutputStop(Camera_VideoOutput* videoOutput) 79 { 80 Camera_ErrorCode ret = OH_VideoOutput_Stop(videoOutput); 81 if (ret != CAMERA_OK) { 82 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed."); 83 } 84 return ret; 85 } 86 ``` 87 88 89## 状态监听 90 91在相机应用开发过程中,可以随时监听录像输出流状态,包括录像开始、录像结束、录像流输出的错误。 92 93- 通过注册固定的frameStart回调函数获取监听录像开始结果,videoOutput创建成功时即可监听,录像第一次曝光时触发,有该事件返回结果则认为录像开始。 94 95 ```c++ 96 void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput) 97 { 98 OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart"); 99 } 100 ``` 101 102- 通过注册固定的frameEnd回调函数获取监听录像结束结果,videoOutput创建成功时即可监听,录像完成最后一帧时触发,有该事件返回结果则认为录像流已结束。 103 104 ```c++ 105 void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount) 106 { 107 OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount); 108 } 109 ``` 110 111- 通过注册固定的error回调函数获取监听录像输出错误结果,callback返回预览输出接口使用错误时对应的错误码,错误码类型参见[Camera_ErrorCode](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1)。 112 113 ```c++ 114 void VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode) 115 { 116 OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode); 117 } 118 ``` 119 ```c++ 120 VideoOutput_Callbacks* GetVideoOutputListener(void) 121 { 122 static VideoOutput_Callbacks videoOutputListener = { 123 .onFrameStart = VideoOutputOnFrameStart, 124 .onFrameEnd = VideoOutputOnFrameEnd, 125 .onError = VideoOutputOnError 126 }; 127 return &videoOutputListener; 128 } 129 130 Camera_ErrorCode RegisterVideoOutputCallback(Camera_VideoOutput* videoOutput) 131 { 132 Camera_ErrorCode ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener()); 133 if (ret != CAMERA_OK) { 134 OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_RegisterCallback failed."); 135 } 136 return ret; 137 } 138 ```