1e41f4b71Sopenharmony_ci# Camera Session Management (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciBefore using the camera application for preview, photographing, video recording, and metadata management, you must create a camera session.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciYou can implement the following functions in the session:
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci- Configure the camera input and output streams. This is mandatory for photographing.
8e41f4b71Sopenharmony_ci  Configuring an input stream is to add a device input, which means that the user selects a camera for photographing. Configuring an output stream is to select a data output mode. For example, to implement photographing, you must configure both the preview stream and photo stream as the output stream. The data of the preview stream is displayed on the **\<XComponent>**, and that of the photo stream is saved to the Gallery application through the **ImageReceiver** API.
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci- Perform more operations on the camera hardware. For example, add the flash and adjust the focal length. For details about the supported configurations and APIs, see [Camera API Reference](../../reference/apis-camera-kit/_o_h___camera.md).
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Control session switching. The application can switch the camera mode by removing and adding output streams. For example, to switch from photographing to video recording, the application must remove the photo output stream and add the video output stream.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ciAfter the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## How to Develop
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci1. Import the NDK.
19e41f4b71Sopenharmony_ci     
20e41f4b71Sopenharmony_ci   ```c++
21e41f4b71Sopenharmony_ci    #include "hilog/log.h"
22e41f4b71Sopenharmony_ci    #include "ohcamera/camera.h"
23e41f4b71Sopenharmony_ci    #include "ohcamera/camera_input.h"
24e41f4b71Sopenharmony_ci    #include "ohcamera/capture_session.h"
25e41f4b71Sopenharmony_ci    #include "ohcamera/photo_output.h"
26e41f4b71Sopenharmony_ci    #include "ohcamera/preview_output.h"
27e41f4b71Sopenharmony_ci    #include "ohcamera/video_output.h"
28e41f4b71Sopenharmony_ci    #include "ohcamera/camera_manager.h"
29e41f4b71Sopenharmony_ci   ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci2. Link the dynamic library in the CMake script.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci   ```txt
34e41f4b71Sopenharmony_ci    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
35e41f4b71Sopenharmony_ci   ```
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci3. Call **OH_CameraManager_CreateCaptureSession()** in the **CameraManager** class to create a session.
38e41f4b71Sopenharmony_ci     
39e41f4b71Sopenharmony_ci   ```c++
40e41f4b71Sopenharmony_ci    Camera_Manager *cameraManager = nullptr;
41e41f4b71Sopenharmony_ci    Camera_Input* cameraInput = nullptr;
42e41f4b71Sopenharmony_ci    Camera_PreviewOutput* previewOutput = nullptr;
43e41f4b71Sopenharmony_ci    Camera_PhotoOutput* photoOutput = nullptr;
44e41f4b71Sopenharmony_ci    Camera_VideoOutput* videoOutput = nullptr;
45e41f4b71Sopenharmony_ci    Camera_CaptureSession* captureSession = nullptr;
46e41f4b71Sopenharmony_ci    Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession);
47e41f4b71Sopenharmony_ci    if (captureSession == nullptr || ret != CAMERA_OK) {
48e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed.");
49e41f4b71Sopenharmony_ci    }
50e41f4b71Sopenharmony_ci   ```
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci4. Call **OH_CaptureSession_BeginConfig()** in the **CaptureSession** class to start configuration for the session.
53e41f4b71Sopenharmony_ci     
54e41f4b71Sopenharmony_ci   ```c++
55e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_BeginConfig(captureSession);
56e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
57e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
58e41f4b71Sopenharmony_ci    }
59e41f4b71Sopenharmony_ci   ```
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci5. Configure the session. You can call **OH_CaptureSession_AddInput()**, **OH_CaptureSession_AddPreviewOutput()**, and **OH_CaptureSession_AddPhotoOutput()** to add the input and output streams to the session, respectively. The code snippet below uses adding the preview stream **previewOutput** and photo stream **photoOutput** as an example to implement the photographing and preview mode.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci     After the configuration, call **commitConfig()** and **start()** in the **CaptureSession** class in sequence to commit the configuration and start the session.
64e41f4b71Sopenharmony_ci     
65e41f4b71Sopenharmony_ci   ```c++
66e41f4b71Sopenharmony_ci    // Add the camera input stream to the session.
67e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
68e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
69e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
70e41f4b71Sopenharmony_ci    }
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci    // Add the preview output stream to the session.
73e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput);
74e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
75e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed.");
76e41f4b71Sopenharmony_ci    }
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci    // Add the photo output stream to the session.
79e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput);
80e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
81e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed.");
82e41f4b71Sopenharmony_ci    }
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci    // Commit the session configuration.
85e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_CommitConfig(captureSession);
86e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
87e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
88e41f4b71Sopenharmony_ci    }
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci    // Start the session.
91e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_Start(captureSession);
92e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
93e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
94e41f4b71Sopenharmony_ci    }
95e41f4b71Sopenharmony_ci   ```
96e41f4b71Sopenharmony_ci
97e41f4b71Sopenharmony_ci6. Control the session. You can call **stop()** in the **CaptureSession** class to stop the session, and call **removeOutput()** and **addOutput()** in this class to switch to another session. The code snippet below uses removing the photo stream **photoOutput** and adding the video stream **videoOutput** as an example to complete the switching from photographing to recording.
98e41f4b71Sopenharmony_ci     
99e41f4b71Sopenharmony_ci   ```c++
100e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_Stop(captureSession);
101e41f4b71Sopenharmony_ci    if (ret == CAMERA_OK) {
102e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success ");
103e41f4b71Sopenharmony_ci    } else {
104e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret);
105e41f4b71Sopenharmony_ci    }
106e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_BeginConfig(captureSession);
107e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
108e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
109e41f4b71Sopenharmony_ci    }
110e41f4b71Sopenharmony_ci    // Remove the photo output stream from the session.
111e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput);
112e41f4b71Sopenharmony_ci    if (ret == CAMERA_OK) {
113e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
114e41f4b71Sopenharmony_ci    } else {
115e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
116e41f4b71Sopenharmony_ci    }
117e41f4b71Sopenharmony_ci     // Add the video output stream to the session.
118e41f4b71Sopenharmony_ci    ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput);
119e41f4b71Sopenharmony_ci    if (ret == CAMERA_OK) {
120e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success ");
121e41f4b71Sopenharmony_ci    } else {
122e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret);
123e41f4b71Sopenharmony_ci    }
124e41f4b71Sopenharmony_ci   ```
125