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相机使用预览、拍照、录像、元数据功能前,均需要创建相机会话。 10 11在会话中,可以完成以下功能: 12 13- 配置相机的输入流和输出流。相机在拍摄前,必须完成输入输出流的配置。 14 配置输入流即添加设备输入,对用户而言,相当于选择设备的某一相机拍摄;配置输出流,即选择数据将以什么形式输出。当应用需要实现拍照时,输出流应配置为预览流和拍照流,预览流的数据将显示在XComponent组件上,拍照流的数据将通过ImageReceiver接口的能力保存到相册中。 15 16- 添加闪光灯、调整焦距等配置。具体支持的配置及接口说明请参考[Camera API参考](../../reference/apis-camera-kit/capi-oh-camera.md)。 17 18- 会话切换控制。应用可以通过移除和添加输出流的方式,切换相机模式。如当前会话的输出流为拍照流,应用可以将拍照流移除,然后添加视频流作为输出流,即完成了拍照到录像的切换。 19 20完成会话配置后,应用提交和开启会话,可以开始调用相机相关功能。 21 22## 开发步骤 23 241. 导入NDK相关接口,导入方法如下。 25 26 ```c++ 27 #include "hilog/log.h" 28 #include "ohcamera/camera.h" 29 #include "ohcamera/camera_input.h" 30 #include "ohcamera/capture_session.h" 31 #include "ohcamera/photo_output.h" 32 #include "ohcamera/preview_output.h" 33 #include "ohcamera/video_output.h" 34 #include "ohcamera/camera_manager.h" 35 ``` 36 372. 在CMake脚本中链接相关动态库。 38 39 ```txt 40 target_link_libraries(entry PUBLIC 41 libace_napi.z.so 42 libohcamera.so 43 libhilog_ndk.z.so 44 ) 45 ``` 46 473. 调用[OH_CameraManager_CreateCaptureSession()](../../reference/apis-camera-kit/capi-camera-manager-h.md#oh_cameramanager_createcapturesession)方法创建一个会话。 48 49 ```c++ 50 Camera_CaptureSession* CreateCaptureSession(Camera_Manager* cameraManager) 51 { 52 Camera_CaptureSession* captureSession = nullptr; 53 Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession); 54 if (captureSession == nullptr || ret != CAMERA_OK) { 55 OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed."); 56 } 57 return captureSession; 58 } 59 ``` 60 614. 调用[OH_CaptureSession_SetSessionMode()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_setsessionmode)方法配置会话模式。 62 63 ```c++ 64 Camera_ErrorCode SetSessionMode(Camera_CaptureSession* captureSession) 65 { 66 Camera_ErrorCode ret = OH_CaptureSession_SetSessionMode(captureSession, NORMAL_VIDEO); 67 if (ret != CAMERA_OK) { 68 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetSessionMode failed."); 69 } 70 return ret; 71 } 72 ``` 73 745. 调用[OH_CaptureSession_BeginConfig()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_beginconfig)方法配置会话。 75 76 ```c++ 77 Camera_ErrorCode BeginConfig(Camera_CaptureSession* captureSession) 78 { 79 Camera_ErrorCode ret = OH_CaptureSession_BeginConfig(captureSession); 80 if (ret != CAMERA_OK) { 81 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed."); 82 } 83 return ret; 84 } 85 ``` 86 876. 使能。向会话中添加相机的输入流和输出流,调用[OH_CaptureSession_AddInput()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_addinput)添加相机的输入流;调用[OH_CaptureSession_AddPreviewOutput()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_addpreviewoutput)和[OH_CaptureSession_AddPhotoOutput()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_addphotooutput)添加相机的输出流。以下示例代码以添加预览流previewOutput和拍照流photoOutput为例,即当前模式支持拍照和预览。 88 89 调用[OH_CaptureSession_CommitConfig()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_commitconfig)和[OH_CaptureSession_Start()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_start)方法提交相关配置,并启动会话。 90 91 ```c++ 92 Camera_ErrorCode StartSession(Camera_CaptureSession* captureSession, Camera_Input* cameraInput, 93 Camera_PreviewOutput* previewOutput, Camera_PhotoOutput* photoOutput) 94 { 95 // 向会话中添加相机输入流。 96 Camera_ErrorCode ret = OH_CaptureSession_AddInput(captureSession, cameraInput); 97 if (ret != CAMERA_OK) { 98 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed."); 99 return ret; 100 } 101 102 // 向会话中添加预览输出流。 103 ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput); 104 if (ret != CAMERA_OK) { 105 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed."); 106 return ret; 107 } 108 109 // 向会话中添加拍照输出流。 110 ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput); 111 if (ret != CAMERA_OK) { 112 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed."); 113 return ret; 114 } 115 116 // 提交会话配置。 117 ret = OH_CaptureSession_CommitConfig(captureSession); 118 if (ret != CAMERA_OK) { 119 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed."); 120 return ret; 121 } 122 123 // 启动会话。 124 ret = OH_CaptureSession_Start(captureSession); 125 if (ret != CAMERA_OK) { 126 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); 127 } 128 return ret; 129 } 130 ``` 131 1327. 会话控制。调用[OH_CaptureSession_Stop()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_stop)方法可以停止当前会话。调用[OH_CaptureSession_RemovePhotoOutput()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_removephotooutput)和[OH_CaptureSession_AddVideoOutput()](../../reference/apis-camera-kit/capi-capture-session-h.md#oh_capturesession_addvideooutput)方法可以完成会话切换控制。以下示例代码以移除拍照流photoOutput,添加视频流videoOutput为例,完成了拍照到录像的切换。 133 134 ```c++ 135 Camera_ErrorCode ReloadSession(Camera_CaptureSession* captureSession, Camera_PhotoOutput* photoOutput, 136 Camera_VideoOutput* videoOutput) 137 { 138 Camera_ErrorCode ret = OH_CaptureSession_Stop(captureSession); 139 if (ret == CAMERA_OK) { 140 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success "); 141 } else { 142 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret); 143 } 144 ret = OH_CaptureSession_BeginConfig(captureSession); 145 if (ret != CAMERA_OK) { 146 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed."); 147 return ret; 148 } 149 // 从会话中移除拍照输出流。 150 ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput); 151 if (ret == CAMERA_OK) { 152 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success "); 153 } else { 154 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret); 155 } 156 // 释放photoOutput。 157 ret = OH_PhotoOutput_Release(photoOutput); 158 if (ret == CAMERA_OK) { 159 OH_LOG_INFO(LOG_APP, "OH_PhotoOutput_Release success "); 160 } else { 161 OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_Release failed. %d ", ret); 162 } 163 // 向会话中添加视频输出流。 164 ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput); 165 if (ret == CAMERA_OK) { 166 OH_LOG_INFO(LOG_APP, "OH_CaptureSession_AddVideoOutput success "); 167 } else { 168 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed. %d ", ret); 169 return ret; 170 } 171 // 提交会话配置。 172 ret = OH_CaptureSession_CommitConfig(captureSession); 173 if (ret != CAMERA_OK) { 174 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed."); 175 return ret; 176 } 177 178 // 启动会话。 179 ret = OH_CaptureSession_Start(captureSession); 180 if (ret != CAMERA_OK) { 181 OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); 182 } 183 return ret; 184 } 185 ```