1# Camera Session Management (ArkTS)
2
3Before using the camera application for preview, photo capture, video recording, and metadata management, you must create a camera session.
4
5You can implement the following functions in the session:
6
7- Configure the camera input and output streams. This is mandatory for photo capture.
8
9  Configuring an input stream is to add a device input, which means that the user selects a camera for photo capture. Configuring an output stream is to select a data output mode. For example, to implement photo capture, 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.
10
11- 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).
12
13- Control session switching. The application can switch the camera mode by removing and adding output streams. For example, to switch from photo capture to video recording, the application must remove the photo output stream and add the video output stream.
14
15After the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
16
17## How to Develop
181. Import the modules.
19
20   ```ts
21   import { camera } from '@kit.CameraKit';
22   import { BusinessError } from '@kit.BasicServicesKit';
23   ```
24
252. Call [createSession](../../reference/apis-camera-kit/js-apis-camera.md#createsession11) in the **CameraManager** class to create a session.
26
27   ```ts
28   function getSession(cameraManager: camera.CameraManager): camera.Session | undefined {
29     let session: camera.Session | undefined = undefined;
30     try {
31       session = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession;
32     } catch (error) {
33       let err = error as BusinessError;
34       console.error(`Failed to create the session instance. error: ${JSON.stringify(err)}`);
35     }
36     return session;
37   }
38   ```
39
403. Call [beginConfig](../../reference/apis-camera-kit/js-apis-camera.md#beginconfig11) in the **PhotoSession** class to configure the session.
41
42   ```ts
43   function beginConfig(photoSession: camera.PhotoSession): void {
44     try {
45       photoSession.beginConfig();
46     } catch (error) {
47       let err = error as BusinessError;
48       console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`);
49     }
50   }
51   ```
52
534. 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 photo capture and preview mode.
54     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.
55   ```ts
56   async function startSession(photoSession: camera.PhotoSession, cameraInput: camera.CameraInput, previewOutput: camera.PreviewOutput, photoOutput: camera.PhotoOutput): Promise<void> {
57     try {
58       photoSession.addInput(cameraInput);
59     } catch (error) {
60       let err = error as BusinessError;
61       console.error(`Failed to addInput. error: ${JSON.stringify(err)}`);
62     }
63     try {
64       photoSession.addOutput(previewOutput);
65     } catch (error) {
66       let err = error as BusinessError;
67       console.error(`Failed to add previewOutput. error: ${JSON.stringify(err)}`);
68     }
69     try {
70       photoSession.addOutput(photoOutput);
71     } catch (error) {
72       let err = error as BusinessError;
73       console.error(`Failed to add photoOutput. error: ${JSON.stringify(err)}`);
74     }
75     try {
76       await photoSession.commitConfig();
77     } catch (error) {
78       let err = error as BusinessError;
79       console.error(`Failed to commitConfig. error: ${JSON.stringify(err)}`);
80     }
81
82     try {
83       await photoSession.start();
84     } catch (error) {
85       let err = error as BusinessError;
86       console.error(`Failed to start. error: ${JSON.stringify(err)}`);
87     }
88   }
89   ```
90
915. 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 photo capture to recording.
92
93   ```ts
94   async function switchOutput(photoSession: camera.PhotoSession, videoOutput: camera.VideoOutput, photoOutput: camera.PhotoOutput): Promise<void> {
95     try {
96       await photoSession.stop();
97     } catch (error) {
98       let err = error as BusinessError;
99       console.error(`Failed to stop. error: ${JSON.stringify(err)}`);
100     }
101
102     try {
103       photoSession.beginConfig();
104     } catch (error) {
105       let err = error as BusinessError;
106       console.error(`Failed to beginConfig. error: ${JSON.stringify(err)}`);
107     }
108     // Remove the photo output stream from the session.
109     try {
110       photoSession.removeOutput(photoOutput);
111     } catch (error) {
112       let err = error as BusinessError;
113       console.error(`Failed to remove photoOutput. error: ${JSON.stringify(err)}`);
114     }
115     // Add the video output stream to the session.
116     try {
117       photoSession.addOutput(videoOutput);
118     } catch (error) {
119       let err = error as BusinessError;
120       console.error(`Failed to add videoOutput. error: ${JSON.stringify(err)}`);
121     }
122   }
123   ```
124