camera-shooting.md 6.9 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4 5 6 7 8
# 拍照

拍照是相机的最重要功能之一,拍照模块基于相机复杂的逻辑,为了保证用户拍出的照片质量,在中间步骤可以设置分辨率、闪光灯、焦距、照片质量及旋转角度等信息。

## 开发步骤

详细的API说明请参考[Camera API参考](../reference/apis/js-apis-camera.md)

9
1. 导入image接口。创建拍照输出流的SurfaceId以及拍照输出的数据,都需要用到系统提供的image接口能力,导入image接口的方法如下。
Z
zengyawen 已提交
10 11 12 13 14 15
     
   ```ts
   import image from '@ohos.multimedia.image';
   ```

2. 获取SurfaceId。
16
   
Z
zengyawen 已提交
17 18 19
   通过image的createImageReceiver方法创建ImageReceiver实例,再通过实例的getReceivingSurfaceId方法获取SurfaceId,与拍照输出流相关联,获取拍照输出流的数据。
 
   ```ts
Z
zhangchao 已提交
20
   async function getImageReceiverSurfaceId() {
21 22 23 24
     let receiver: image.ImageReceiver = image.createImageReceiver(640, 480, 4, 8);
     console.info('before ImageReceiver check');
     if (receiver !== undefined) {
       console.info('ImageReceiver is ok');
Z
zhangchao 已提交
25
       let photoSurfaceId: string = await receiver.getReceivingSurfaceId();
26 27 28 29
       console.info('ImageReceived id: ' + JSON.stringify(photoSurfaceId));
     } else {
       console.info('ImageReceiver is not ok');
     }
Z
zengyawen 已提交
30 31 32 33
   }
   ```

3. 创建拍照输出流。
34
   
Z
zengyawen 已提交
35 36 37
   通过CameraOutputCapability类中的photoProfiles()方法,可获取当前设备支持的拍照输出流,通过createPhotoOutput()方法传入支持的某一个输出流及步骤一获取的SurfaceId创建拍照输出流。

   ```ts
38
   let photoProfilesArray: Array<camera.Profile> = cameraOutputCapability.photoProfiles;
Z
zengyawen 已提交
39
   if (!photoProfilesArray) {
40
     console.error("createOutput photoProfilesArray == null || undefined");
Z
zengyawen 已提交
41
   }
42
   let photoOutput: camera.PhotoOutput;
Z
zengyawen 已提交
43
   try {
44
     photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId);
Z
zengyawen 已提交
45
   } catch (error) {
Z
zhangchao 已提交
46 47
     let err = error as BusinessError;
     console.error('Failed to createPhotoOutput errorCode = ' + err.code);
Z
zengyawen 已提交
48 49 50 51
   }
   ```

4. 参数配置。
52
   
Z
zengyawen 已提交
53 54 55 56
   配置相机的参数可以调整拍照的一些功能,包括闪光灯、变焦、焦距等。
 
   ```ts
   // 判断设备是否支持闪光灯
57
   let flashStatus: boolean;
Z
zengyawen 已提交
58
   try {
59
     flashStatus = captureSession.hasFlash();
Z
zengyawen 已提交
60
   } catch (error) {
Z
zhangchao 已提交
61 62
     let err = error as BusinessError;
     console.error('Failed to hasFlash. errorCode = ' + err.code);
Z
zengyawen 已提交
63 64 65
   }
   console.info('Promise returned with the flash light support status:' + flashStatus); 
   if (flashStatus) {
66 67 68 69 70 71
     // 判断是否支持自动闪光灯模式
     let flashModeStatus: boolean;
     try {
       let status: boolean = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO);
       flashModeStatus = status;    
     } catch (error) {
Z
zhangchao 已提交
72 73
       let err = error as BusinessError;
       console.error('Failed to check whether the flash mode is supported. errorCode = ' + err.code);
74 75 76
     }    
     if(flashModeStatus) {
       // 设置自动闪光灯模式
Z
zengyawen 已提交
77
       try {
78
         captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO);
Z
zengyawen 已提交
79
       } catch (error) {
Z
zhangchao 已提交
80 81
         let err = error as BusinessError;
         console.error('Failed to set the flash mode. errorCode = ' + err.code);
Z
zengyawen 已提交
82
       }
83
     }
Z
zengyawen 已提交
84 85
   } 
   // 判断是否支持连续自动变焦模式
86
   let focusModeStatus: boolean;
Z
zengyawen 已提交
87
   try {
88 89
     let status: boolean = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
     focusModeStatus = status;
Z
zengyawen 已提交
90
   } catch (error) {
Z
zhangchao 已提交
91 92
     let err = error as BusinessError;
     console.error('Failed to check whether the focus mode is supported. errorCode = ' + err.code);
Z
zengyawen 已提交
93 94
   } 
   if (focusModeStatus) {
95 96 97
     // 设置连续自动变焦模式
     try {
         captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO);
Z
zhangchao 已提交
98 99 100
     } catch (error) {
        let err = error as BusinessError;
        console.error('Failed to set the focus mode. errorCode = ' + err.code);
101
     }
Z
zengyawen 已提交
102 103
   } 
   // 获取相机支持的可变焦距比范围
104
   let zoomRatioRange: Array<number>;
Z
zengyawen 已提交
105
   try {
106
     zoomRatioRange = captureSession.getZoomRatioRange();
Z
zengyawen 已提交
107
   } catch (error) {
Z
zhangchao 已提交
108 109
     let err = error as BusinessError;
     console.error('Failed to get the zoom ratio range. errorCode = ' + err.code);
Z
zengyawen 已提交
110 111 112
   } 
   // 设置可变焦距比
   try {
113
     captureSession.setZoomRatio(zoomRatioRange[0]);
Z
zengyawen 已提交
114
   } catch (error) {
Z
zhangchao 已提交
115 116
     let err = error as BusinessError;
     console.error('Failed to set the zoom ratio value. errorCode = ' + err.code);
Z
zengyawen 已提交
117 118 119 120
   }
   ```

5. 触发拍照。
121
   
Z
zengyawen 已提交
122 123 124
   通过photoOutput类的capture()方法,执行拍照任务。该方法有两个参数,第一个参数为拍照设置参数的setting,setting中可以设置照片的质量和旋转角度,第二参数为回调函数。
 
   ```ts
125 126 127 128 129
   let settings: camera.PhotoCaptureSetting = {
     quality: camera.QualityLevel.QUALITY_LEVEL_HIGH,                                     // 设置图片质量高
     rotation: camera.ImageRotation.ROTATION_0,                                           // 设置图片旋转角度0
     location: captureLocation,                                                           // 设置图片地理位置
     mirror: false                                                                        // 设置镜像使能开关(默认关)
Z
zengyawen 已提交
130
   };
131 132 133 134 135 136
   photoOutput.capture(settings, async (err: BusinessError) => {
     if (err) {
       console.error('Failed to capture the photo ${err.message}');
       return;
     }
     console.info('Callback invoked to indicate the photo capture request success.');
Z
zengyawen 已提交
137 138 139 140 141 142 143
   });
   ```

## 状态监听

在相机应用开发过程中,可以随时监听拍照输出流状态,包括拍照流开始、拍照帧的开始与结束、拍照输出流的错误。

144
- 通过注册固定的captureStart回调函数获取监听拍照开始结果,photoOutput创建成功时即可监听,拍照第一次曝光时触发,该事件返回此次拍照的captureId。
Z
zengyawen 已提交
145 146
    
  ```ts
147 148 149
  photoOutput.on('captureStart', (err: BusinessError, captureId: number) => {
    console.info(`photo capture stated, captureId : ${captureId}`);
  });
Z
zengyawen 已提交
150 151
  ```

Z
zengyawen 已提交
152
- 通过注册固定的captureEnd回调函数获取监听拍照结束结果,photoOutput创建成功时即可监听,该事件返回结果为拍照完全结束后的相关信息[CaptureEndInfo](../reference/apis/js-apis-camera.md#captureendinfo)
Z
zengyawen 已提交
153 154
    
  ```ts
155 156 157 158
  photoOutput.on('captureEnd', (err: BusinessError, captureEndInfo: camera.CaptureEndInfo) => {
    console.info(`photo capture end, captureId : ${captureEndInfo.captureId}`);
    console.info(`frameCount : ${captureEndInfo.frameCount}`);
  });
Z
zengyawen 已提交
159 160 161 162 163
  ```

- 通过注册固定的error回调函数获取监听拍照输出流的错误结果。callback返回拍照输出接口使用错误时的对应错误码,错误码类型参见[CameraErrorCode](../reference/apis/js-apis-camera.md#cameraerrorcode)
    
  ```ts
164 165 166
  photoOutput.on('error', (error: BusinessError) => {
    console.info(`Photo output error code: ${error.code}`);
  });
Z
zengyawen 已提交
167
  ```