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