1# Camera Session Management 2 3Before using the camera application for preview, photographing, video recording, and metadata, 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/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 17 181. Call **createCaptureSession()** in the **CameraManager** class to create a session. 19 20 ```ts 21 let captureSession; 22 try { 23 captureSession = cameraManager.createCaptureSession(); 24 } catch (error) { 25 console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code); 26 } 27 ``` 28 292. Call **beginConfig()** in the **CaptureSession** class to start configuration for the session. 30 31 ```ts 32 try { 33 captureSession.beginConfig(); 34 } catch (error) { 35 console.error('Failed to beginConfig. errorCode = ' + error.code); 36 } 37 ``` 38 393. Configure the session. You can call **addInput()** and **addOutput()** in the **CaptureSession** 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. 40 41 After the configuration, call **commitConfig()** and **start()** in the **CaptureSession** class in sequence to commit the configuration and start the session. 42 43 ```ts 44 try { 45 captureSession.addInput(cameraInput); 46 } catch (error) { 47 console.error('Failed to addInput. errorCode = ' + error.code); 48 } 49 try { 50 captureSession.addOutput(previewOutput); 51 } catch (error) { 52 console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code); 53 } 54 try { 55 captureSession.addOutput(photoOutput); 56 } catch (error) { 57 console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code); 58 } 59 await captureSession.commitConfig() ; 60 await captureSession.start().then(() => { 61 console.info('Promise returned to indicate the session start success.'); 62 }) 63 ``` 64 654. 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. 66 67 ```ts 68 await captureSession.stop(); 69 try { 70 captureSession.beginConfig(); 71 } catch (error) { 72 console.error('Failed to beginConfig. errorCode = ' + error.code); 73 } 74 // Remove the photo output stream from the session. 75 try { 76 captureSession.removeOutput(photoOutput); 77 } catch (error) { 78 console.error('Failed to removeOutput(photoOutput). errorCode = ' + error.code); 79 } 80 // Add the video output stream to the session. 81 try { 82 captureSession.addOutput(videoOutput); 83 } catch (error) { 84 console.error('Failed to addOutput(videoOutput). errorCode = ' + error.code); 85 } 86 ``` 87