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