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