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 
18 1. 导入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 
31 2. 在CMake脚本中链接相关动态库。
32 
33    ```txt
34     target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
35    ```
36 
37 3. 调用cameraManager类中的[OH_CameraManager_CreateCaptureSession()](../../reference/apis-camera-kit/_o_h___camera.md#oh_cameramanager_createcapturesession)方法创建一个会话。
38 
39    ```c++
40     Camera_Manager *cameraManager = nullptr;
41     Camera_Input* cameraInput = nullptr;
42     Camera_PreviewOutput* previewOutput = nullptr;
43     Camera_PhotoOutput* photoOutput = nullptr;
44     Camera_VideoOutput* videoOutput = nullptr;
45     Camera_CaptureSession* captureSession = nullptr;
46     Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
47     if (captureSession == nullptr || ret != CAMERA_OK) {
48         OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
49     }
50    ```
51 
52 4. 调用captureSession类中的[OH_CaptureSession_SetSessionMode()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_setsessionmode)方法配置会话模式。
53 
54    ```c++
55     ret = OH_CaptureSession_SetSessionMode(captureSession, NORMAL_PHOTO);
56     if (ret != CAMERA_OK) {
57         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetSessionMode failed.");
58     }
59    ```
60 
61 5. 调用captureSession类中的[OH_CaptureSession_BeginConfig()](../../reference/apis-camera-kit/_o_h___camera.md#oh_capturesession_beginconfig)方法配置会话。
62 
63    ```c++
64     ret = OH_CaptureSession_BeginConfig(captureSession);
65     if (ret != CAMERA_OK) {
66         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
67     }
68    ```
69 
70 6. 使能。向会话中添加相机的输入流和输出流,调用[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为例,即当前模式支持拍照和预览。
71 
72      调用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)方法提交相关配置,并启动会话。
73 
74    ```c++
75     // 向会话中添加相机输入流
76     ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
77     if (ret != CAMERA_OK) {
78         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
79     }
80 
81     // 向会话中添加预览输出流
82     ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
83     if (ret != CAMERA_OK) {
84         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
85     }
86 
87     // 向会话中添加拍照输出流
88     ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
89     if (ret != CAMERA_OK) {
90         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.");
91     }
92 
93     // 提交会话配置
94     ret = OH_CaptureSession_CommitConfig(captureSession);
95     if (ret != CAMERA_OK) {
96         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
97     }
98 
99     // 启动会话
100     ret = OH_CaptureSession_Start(captureSession);
101     if (ret != CAMERA_OK) {
102         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
103     }
104    ```
105 
106 7. 会话控制。调用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为例,完成了拍照到录像的切换。
107 
108    ```c++
109     ret = OH_CaptureSession_Stop(captureSession);
110     if (ret == CAMERA_OK) {
111         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
112     } else {
113         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
114     }
115     ret = OH_CaptureSession_BeginConfig(captureSession);
116     if (ret != CAMERA_OK) {
117         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
118     }
119     // 从会话中移除拍照输出流
120     ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput);
121     if (ret == CAMERA_OK) {
122         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
123     } else {
124         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
125     }
126      // 向会话中添加视频输出流
127     ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
128     if (ret == CAMERA_OK) {
129         OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
130     } else {
131         OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
132     }
133    ```