会话管理(ArkTS)
===========

相机使用预览、拍照、录像、元数据功能前，均需要创建相机会话。

在会话中，可以完成以下功能：

*   配置相机的输入流和输出流。相机在拍摄前，必须完成输入输出流的配置。
    
    配置输入流即添加设备输入，对用户而言，相当于选择设备的某一相机拍摄；配置输出流，即选择数据将以什么形式输出。当应用需要实现拍照时，输出流应配置为预览流和拍照流，预览流的数据将显示在[XComponent](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/ts-basic-components-xcomponent)组件上，拍照流的数据将通过[ImageReceiver](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-image-imagereceiver)接口的能力保存到相册中。
    
*   添加闪光灯、调整焦距等配置。具体支持的配置及接口说明请参考[@ohos.multimedia.camera (相机管理)](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera)。
    
*   会话切换控制。应用可以通过移除和添加输出流的方式，切换相机模式。如当前会话的输出流为拍照流，应用可以将拍照流移除，然后添加视频流作为输出流，即完成了拍照到录像的切换。
    

完成会话配置后，应用提交和开启会话，可以开始调用相机相关功能。

#### 开发步骤

1.  导入相关接口，导入方法如下。
    
    import { camera } from '@kit.CameraKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
2.  调用[cameraManager](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-cameramanager)中的[createSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-cameramanager#createsession11)方法创建一个会话。
    
    // 此处以videoSession为例。
    function getSession(cameraManager: camera.CameraManager): camera.VideoSession | undefined {
      let videoSession: camera.VideoSession | undefined = undefined;
      try {
        videoSession = cameraManager.createSession(camera.SceneMode.NORMAL\_VIDEO) as camera.VideoSession;
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to create the session instance. error: ${err.code}\`);
      }
      return videoSession;
    }
    
3.  调用[VideoSession](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-videosession)中的[beginConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#beginconfig11)方法配置会话。
    
    function beginConfig(videoSession: camera.VideoSession): void {
      try {
        videoSession.beginConfig();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to beginConfig. error: ${err.code}\`);
      }
    }
    
4.  使能。向会话中添加相机的输入流和输出流，调用[addInput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#addinput11)添加相机的输入流；调用[addOutput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#addoutput11)添加相机的输出流。以下示例代码以添加预览流previewOutput和拍照流photoOutput为例，即当前模式支持拍照和预览。
    
    调用VideoSession中的[commitConfig](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#commitconfig11)和[start](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#start11)方法提交相关配置，并启动会话。
    
    ![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/7/v3/bl6OFRnHSWmb55fgd6cWcA/note_3.0-zh-cn.png?HW-CC-KV=V1&HW-CC-Date=20260709T132520Z&HW-CC-Expire=86400&HW-CC-Sign=20D337E94A627BD7DE0D9B75943876FEEADC4934821815BB3C56173843F05B4A)
    
    在调用[addOutput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#addoutput11)添加相机的输出流前，可通过[canAddOutput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#canaddoutput11)判断当前相机输出流是否可以添加到session中。
    
    相机输入流cameraInput创建流程请参考[设备输入](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-device-input)，相机预览输出流previewOutput和拍照输出流photoOutput创建流程请分别参考[预览](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-preview)和[拍照](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/camera-shooting)。
    
    async function startSession(videoSession: camera.VideoSession, cameraInput: camera.CameraInput, previewOutput: camera.PreviewOutput, photoOutput: camera.PhotoOutput): Promise<void> {
      try {
        videoSession.addInput(cameraInput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to addInput. error: ${err.code}\`);
      }
      let canAddPreviewOutput : boolean = false;
      try {
        canAddPreviewOutput = videoSession.canAddOutput(previewOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add previewOutput. error: ${err.code}\`);
      }
      if (!canAddPreviewOutput) {
        console.error(\`Failed to add preview output.\`);
        return;
      }
      try {
        videoSession.addOutput(previewOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add previewOutput. error: ${err.code}\`);
      }
      let canAddPhotoOutput : boolean = false
      try {
        canAddPhotoOutput = videoSession.canAddOutput(photoOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add photoOutput error: ${err.code}\`);
      }
      if (!canAddPhotoOutput) {
        console.error(\`Failed to add photo output.\`);
        return;
      }
      try {
        videoSession.addOutput(photoOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add photoOutput. error: ${err.code}\`);
      }
      try {
        await videoSession.commitConfig();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to commitConfig. error: ${err.code}\`);
       return;
      }
    
      try {
        await videoSession.start();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to start. error: ${err.code}\`);
      }
    }
    
5.  会话控制。调用VideoSession中的[stop](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#stop11)方法可以停止当前会话。调用[removeOutput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#removeoutput11)和[addOutput](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-camera-session#addoutput11)方法可以完成会话切换控制。以下示例代码以移除拍照流photoOutput，添加视频流videoOutput为例，完成了拍照到录像的切换。
    
    async function switchOutput(videoSession: camera.VideoSession, videoOutput: camera.VideoOutput, photoOutput: camera.PhotoOutput): Promise<void> {
      try {
        await videoSession.stop();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to stop. error: ${err.code}\`);
      }
    
      try {
        videoSession.beginConfig();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to beginConfig. error: ${err.code}\`);
      }
      // 从会话中移除拍照输出流。
      try {
        videoSession.removeOutput(photoOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to remove photoOutput. error: ${err.code}\`);
      }
      try {
        videoSession.canAddOutput(videoOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add videoOutput error: ${err.code}\`);
      }
      // 向会话中添加视频输出流。
      try {
        videoSession.addOutput(videoOutput);
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to add videoOutput. error: ${err.code}\`);
      }
      try {
        await videoSession.commitConfig();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to commitConfig. error: ${err.code}\`);
      }
    
      try {
        await videoSession.start();
      } catch (error) {
        let err = error as BusinessError;
        console.error(\`Failed to start. error: ${err.code}\`);
      }
    }