• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Session Management (ArkTS)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8Before using the camera application for preview, photo capture, video recording, and metadata management, you must create a camera session.
9
10You can implement the following functions in the session:
11
12- Configure the camera input and output streams. This is mandatory for photo capture.
13
14  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.
15
16- 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 [Module Description](../../reference/apis-camera-kit/arkts-apis-camera.md).
17
18- 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.
19
20After the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
21
22## How to Develop
231. Import the modules.
24
25   ```ts
26   import { camera } from '@kit.CameraKit';
27   import { BusinessError } from '@kit.BasicServicesKit';
28   ```
29
302. Call [createSession](../../reference/apis-camera-kit/arkts-apis-camera-CameraManager.md#createsession11) in the **CameraManager** class to create a session.
31
32   ```ts
33   function getSession(cameraManager: camera.CameraManager): camera.Session | undefined {
34     let session: camera.Session | undefined = undefined;
35     try {
36       session = cameraManager.createSession(camera.SceneMode.NORMAL_VIDEO) as camera.VideoSession;
37     } catch (error) {
38       let err = error as BusinessError;
39       console.error(`Failed to create the session instance. error: ${err}`);
40     }
41     return session;
42   }
43   ```
44
453. Call [beginConfig](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#beginconfig11) in the **VideoSession** class to configure the session.
46
47   ```ts
48   function beginConfig(videoSession: camera.VideoSession): void {
49     try {
50       videoSession.beginConfig();
51     } catch (error) {
52       let err = error as BusinessError;
53       console.error(`Failed to beginConfig. error: ${err}`);
54     }
55   }
56   ```
57
584. Configure the session. You can call [addInput](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#addinput11) and [addOutput](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#addoutput11) in the **VideoSession** 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.
59
60    After the configuration, call [commitConfig](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#commitconfig11) and [start](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#start11) in the **VideoSession** class in sequence to commit the configuration and start the session.
61
62     > **NOTE**
63     >
64     > Before calling [addOutput](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#addoutput11) to add a camera output stream, you can call [canAddOutput](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#canaddoutput11) to check whether the camera output stream can be added to the session.
65
66   ```ts
67   async function startSession(videoSession: camera.VideoSession, cameraInput: camera.CameraInput, previewOutput: camera.PreviewOutput, photoOutput: camera.PhotoOutput): Promise<void> {
68     try {
69       videoSession.addInput(cameraInput);
70     } catch (error) {
71       let err = error as BusinessError;
72       console.error(`Failed to addInput. error: ${err}`);
73     }
74     try {
75       videoSession.canAddOutput(previewOutput);
76     } catch (error) {
77       let err = error as BusinessError;
78       console.error(`Failed to canAdd previewOutput. error: ${err}`);
79     }
80     try {
81       videoSession.addOutput(previewOutput);
82     } catch (error) {
83       let err = error as BusinessError;
84       console.error(`Failed to add previewOutput. error: ${err}`);
85     }
86     try {
87       videoSession.canAddOutput(photoOutput);
88     } catch (error) {
89       let err = error as BusinessError;
90       console.error(`Failed to canAdd photoOutput error: ${err}`);
91     }
92     try {
93       videoSession.addOutput(photoOutput);
94     } catch (error) {
95       let err = error as BusinessError;
96       console.error(`Failed to add photoOutput. error: ${err}`);
97     }
98     try {
99       await videoSession.commitConfig();
100     } catch (error) {
101       let err = error as BusinessError;
102       console.error(`Failed to commitConfig. error: ${err}`);
103      return;
104     }
105
106     try {
107       await videoSession.start();
108     } catch (error) {
109       let err = error as BusinessError;
110       console.error(`Failed to start. error: ${err}`);
111     }
112   }
113   ```
114
1155. Control the session. You can call [stop](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#stop11) in the **VideoSession** class to stop the current session, and call [removeOutput](../../reference/apis-camera-kit/arkts-apis-camera-Session.md#removeoutput11) and [addOutput](../../reference/apis-camera-kit/arkts-apis-camera-Session.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.
116
117   ```ts
118   async function switchOutput(videoSession: camera.VideoSession, videoOutput: camera.VideoOutput, photoOutput: camera.PhotoOutput): Promise<void> {
119     try {
120       await videoSession.stop();
121     } catch (error) {
122       let err = error as BusinessError;
123       console.error(`Failed to stop. error: ${err}`);
124     }
125
126     try {
127       videoSession.beginConfig();
128     } catch (error) {
129       let err = error as BusinessError;
130       console.error(`Failed to beginConfig. error: ${err}`);
131     }
132     // Remove the photo output stream from the session.
133     try {
134       videoSession.removeOutput(photoOutput);
135     } catch (error) {
136       let err = error as BusinessError;
137       console.error(`Failed to remove photoOutput. error: ${err}`);
138     }
139     try {
140       videoSession.canAddOutput(videoOutput);
141     } catch (error) {
142       let err = error as BusinessError;
143       console.error(`Failed to canAdd videoOutput error: ${err}`);
144     }
145     // Add the video output stream to the session.
146     try {
147       videoSession.addOutput(videoOutput);
148     } catch (error) {
149       let err = error as BusinessError;
150       console.error(`Failed to add videoOutput. error: ${err}`);
151     }
152     try {
153       await videoSession.commitConfig();
154     } catch (error) {
155       let err = error as BusinessError;
156       console.error(`Failed to commitConfig. error: ${err}`);
157     }
158
159     try {
160       await videoSession.start();
161     } catch (error) {
162       let err = error as BusinessError;
163       console.error(`Failed to start. error: ${err}`);
164     }
165   }
166   ```
167