Session Management (Cangjie)

Note:

Currently in the beta phase.

Before using camera functions such as preview, photo capture, video recording, or metadata, a camera session must be created.

Within a session, the following functionalities can be achieved:

  • Configure camera input and output streams. The camera must complete input and output stream configuration before shooting. Configuring input streams involves adding device inputs, which for users means selecting a specific camera device for shooting. Configuring output streams determines the format in which data will be output. When an application needs to implement photo capture, the output streams should be configured as preview and photo capture streams. Photo capture stream data will be saved to the album via the ImageReceiver interface capability.

  • Add configurations such as flash and focus adjustment. For specific supported configurations and interface descriptions, refer to the Camera API Reference.

  • Session switching control. Applications can switch camera modes by removing and adding output streams. For example, if the current session's output stream is for photo capture, the application can remove the photo capture stream and add a video stream as the output, thereby switching from photo capture to video recording.

After completing session configuration, the application can submit and start the session to begin invoking camera-related functions.

Development Steps

  1. Import relevant interfaces as follows:

    import kit.CameraKit.*
    import ohos.business_exception.BusinessException
    import ohos.hilog.Hilog
    
    
  2. Call the createSession method in the cameraManager class to create a session.

    func getSession(cameraManager: CameraManager): Session {
        let session = cameraManager.createSession(SceneMode.NormalPhoto) as PhotoSession
        return session.getOrThrow()
    }
    
  3. Call the beginConfig method in the PhotoSession class to configure the session.

    func beginConfig(photoSession: PhotoSession): Unit {
        try {
            photoSession.beginConfig()
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to beginConfig. error: ${error.message}")
        }
    }
    
    
  4. Enable the session. Add camera input and output streams to the session. Call addInput to add camera input streams and addOutput to add camera output streams. The following example code demonstrates adding a preview stream (previewOutput) and a photo capture stream (photoOutput), meaning the current mode supports both photo capture and preview.

    Call the commitConfig and start methods in the PhotoSession class to submit the configuration and start the session.

    func startSession(photoSession: PhotoSession, cameraInput: CameraInput, previewOutput: PreviewOutput, photoOutput: PhotoOutput): Unit {
        try {
            photoSession.addInput(cameraInput)
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to addInput. error: ${error.message}")
        }
        try {
            photoSession.addOutput(previewOutput)
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to add previewOutput. error: ${error.message}")
        }
        try {
            photoSession.addOutput(photoOutput)
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to add photoOutput. error: ${error.message}")
        }
        try {
            photoSession.commitConfig()
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to commitConfig. error: ${error.message}")
        }
    
        try {
            photoSession.start()
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to start. error: ${error.message}")
        }
    }
    
  5. Session control. Call the stop method in the PhotoSession class to stop the current session. Call the removeOutput and addOutput methods to achieve session switching control. The following example code demonstrates removing the photo capture stream (photoOutput) and adding a video stream (videoOutput), thereby switching from photo capture to video recording.

    func switchOutput(photoSession: PhotoSession, videoOutput: VideoOutput, photoOutput: PhotoOutput): Unit {
        try {
            photoSession.stop()
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to stop. error: ${error.message}")
        }
    
        try {
            photoSession.beginConfig()
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to beginConfig. error: ${error.message}")
        }
        // Remove the photo capture output stream from the session.
        try {
            photoSession.removeOutput(photoOutput)
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to remove photoOutput. error: ${error.message}")
        }
        // Add the video output stream to the session.
        try {
            photoSession.addOutput(videoOutput)
        } catch (error: BusinessException) {
            Hilog.error(0,"","Failed to add videoOutput. error: ${error.message}")
        }
    }