1e41f4b71Sopenharmony_ci# Camera Session Management (ArkTS)
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/js-apis-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_ci1. Import the modules.
18e41f4b71Sopenharmony_ci     
19e41f4b71Sopenharmony_ci   ```ts
20e41f4b71Sopenharmony_ci   import { camera } from '@kit.CameraKit';
21e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
22e41f4b71Sopenharmony_ci   ```
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci2. Call [createSession](../../reference/apis-camera-kit/js-apis-camera.md#createsession11) in the **CameraManager** class to create a session.
25e41f4b71Sopenharmony_ci     
26e41f4b71Sopenharmony_ci   ```ts
27e41f4b71Sopenharmony_ci   function getSession(cameraManager: camera.CameraManager): camera.Session | undefined {
28e41f4b71Sopenharmony_ci     let session: camera.Session | undefined = undefined;
29e41f4b71Sopenharmony_ci     try {
30e41f4b71Sopenharmony_ci       session = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
31e41f4b71Sopenharmony_ci     } catch (error) {
32e41f4b71Sopenharmony_ci       let err = error as BusinessError;
33e41f4b71Sopenharmony_ci       console.error(`Failed to create the session instance. error: ${JSON.stringify(err)}`);
34e41f4b71Sopenharmony_ci     }
35e41f4b71Sopenharmony_ci     return session;
36e41f4b71Sopenharmony_ci   }
37e41f4b71Sopenharmony_ci   ```
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci3. Call [beginConfig](../../reference/apis-camera-kit/js-apis-camera.md#beginconfig11) in the **PhotoSession** class to configure the session.
40e41f4b71Sopenharmony_ci     
41e41f4b71Sopenharmony_ci   ```ts
42e41f4b71Sopenharmony_ci   function beginConfig(photoSession: camera.PhotoSession): void {
43e41f4b71Sopenharmony_ci     try {
44e41f4b71Sopenharmony_ci       photoSession.beginConfig();
45e41f4b71Sopenharmony_ci     } catch (error) {
46e41f4b71Sopenharmony_ci       let err = error as BusinessError;
47e41f4b71Sopenharmony_ci       console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`);
48e41f4b71Sopenharmony_ci     }
49e41f4b71Sopenharmony_ci   }
50e41f4b71Sopenharmony_ci   ```
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci4. Configure the session. You can call [addInput](../../reference/apis-camera-kit/js-apis-camera.md#addinput11) and [addOutput](../../reference/apis-camera-kit/js-apis-camera.md#addoutput11) in the **PhotoSession** class 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.
53e41f4b71Sopenharmony_ci     After the configuration, call [commitConfig](../../reference/apis-camera-kit/js-apis-camera.md#commitconfig11) and [start](../../reference/apis-camera-kit/js-apis-camera.md#start11) in the **PhotoSession** class in sequence to commit the configuration and start the session.
54e41f4b71Sopenharmony_ci   ```ts
55e41f4b71Sopenharmony_ci   async function startSession(photoSession: camera.PhotoSession, cameraInput: camera.CameraInput, previewOutput: camera.PreviewOutput, photoOutput: camera.PhotoOutput): Promise<void> {
56e41f4b71Sopenharmony_ci     try {
57e41f4b71Sopenharmony_ci       photoSession.addInput(cameraInput);
58e41f4b71Sopenharmony_ci     } catch (error) {
59e41f4b71Sopenharmony_ci       let err = error as BusinessError;
60e41f4b71Sopenharmony_ci       console.error(`Failed to addInput. error: ${JSON.stringify(err)}`);
61e41f4b71Sopenharmony_ci     }
62e41f4b71Sopenharmony_ci     try {
63e41f4b71Sopenharmony_ci       photoSession.addOutput(previewOutput);
64e41f4b71Sopenharmony_ci     } catch (error) {
65e41f4b71Sopenharmony_ci       let err = error as BusinessError;
66e41f4b71Sopenharmony_ci       console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`);
67e41f4b71Sopenharmony_ci     }
68e41f4b71Sopenharmony_ci     try {
69e41f4b71Sopenharmony_ci       photoSession.addOutput(photoOutput);
70e41f4b71Sopenharmony_ci     } catch (error) {
71e41f4b71Sopenharmony_ci       let err = error as BusinessError;
72e41f4b71Sopenharmony_ci       console.error(`Failed to add photoOutput. error: ${JSON.stringify(err)}`);
73e41f4b71Sopenharmony_ci     }
74e41f4b71Sopenharmony_ci     try {
75e41f4b71Sopenharmony_ci       await photoSession.commitConfig();
76e41f4b71Sopenharmony_ci     } catch (error) {
77e41f4b71Sopenharmony_ci       let err = error as BusinessError;
78e41f4b71Sopenharmony_ci       console.error(`Failed to commitConfig. error: ${JSON.stringify(err)}`);
79e41f4b71Sopenharmony_ci     }
80e41f4b71Sopenharmony_ci   
81e41f4b71Sopenharmony_ci     try {
82e41f4b71Sopenharmony_ci       await photoSession.start();
83e41f4b71Sopenharmony_ci     } catch (error) {
84e41f4b71Sopenharmony_ci       let err = error as BusinessError;
85e41f4b71Sopenharmony_ci       console.error(`Failed to start. error: ${JSON.stringify(err)}`);
86e41f4b71Sopenharmony_ci     }
87e41f4b71Sopenharmony_ci   }
88e41f4b71Sopenharmony_ci   ```
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci5. Control the session. You can call [stop](../../reference/apis-camera-kit/js-apis-camera.md#stop11) in the **PhotoSession** class to stop the current session, and call [removeOutput](../../reference/apis-camera-kit/js-apis-camera.md#removeoutput11) and [addOutput](../../reference/apis-camera-kit/js-apis-camera.md#addoutput11) 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.
91e41f4b71Sopenharmony_ci     
92e41f4b71Sopenharmony_ci   ```ts
93e41f4b71Sopenharmony_ci   async function switchOutput(photoSession: camera.PhotoSession, videoOutput: camera.VideoOutput, photoOutput: camera.PhotoOutput): Promise<void> {
94e41f4b71Sopenharmony_ci     try {
95e41f4b71Sopenharmony_ci       await photoSession.stop();
96e41f4b71Sopenharmony_ci     } catch (error) {
97e41f4b71Sopenharmony_ci       let err = error as BusinessError;
98e41f4b71Sopenharmony_ci       console.error(`Failed to stop. error: ${JSON.stringify(err)}`);
99e41f4b71Sopenharmony_ci     }
100e41f4b71Sopenharmony_ci   
101e41f4b71Sopenharmony_ci     try {
102e41f4b71Sopenharmony_ci       photoSession.beginConfig();
103e41f4b71Sopenharmony_ci     } catch (error) {
104e41f4b71Sopenharmony_ci       let err = error as BusinessError;
105e41f4b71Sopenharmony_ci       console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`);
106e41f4b71Sopenharmony_ci     }
107e41f4b71Sopenharmony_ci     // Remove the photo output stream from the session.
108e41f4b71Sopenharmony_ci     try {
109e41f4b71Sopenharmony_ci       photoSession.removeOutput(photoOutput);
110e41f4b71Sopenharmony_ci     } catch (error) {
111e41f4b71Sopenharmony_ci       let err = error as BusinessError;
112e41f4b71Sopenharmony_ci       console.error(`Failed to remove photoOutput. error: ${JSON.stringify(err)}`);
113e41f4b71Sopenharmony_ci     }
114e41f4b71Sopenharmony_ci     // Add the video output stream to the session.
115e41f4b71Sopenharmony_ci     try {
116e41f4b71Sopenharmony_ci       photoSession.addOutput(videoOutput);
117e41f4b71Sopenharmony_ci     } catch (error) {
118e41f4b71Sopenharmony_ci       let err = error as BusinessError;
119e41f4b71Sopenharmony_ci       console.error(`Failed to add videoOutput. error: ${JSON.stringify(err)}`);
120e41f4b71Sopenharmony_ci     }
121e41f4b71Sopenharmony_ci   }
122e41f4b71Sopenharmony_ci   ```
123