1# 拍照实现方案 2 3## 开发流程 4 5在获取到相机支持的输出流能力后,开始创建拍照流,开发流程如下。 6 7![Photographing Development Process](figures/photographing-development-process.png) 8 9## 完整示例 10 11```ts 12import camera from '@ohos.multimedia.camera' 13import image from '@ohos.multimedia.image' 14import media from '@ohos.multimedia.media' 15 16// 创建CameraManager对象 17context: any = getContext(this) 18let cameraManager = camera.getCameraManager(this.context) 19if (!cameraManager) { 20 console.error("camera.getCameraManager error") 21 return; 22} 23// 监听相机状态变化 24cameraManager.on('cameraStatus', (cameraStatusInfo) => { 25 console.info(`camera : ${cameraStatusInfo.camera.cameraId}`); 26 console.info(`status: ${cameraStatusInfo.status}`); 27}) 28 29// 获取相机列表 30let cameraArray = cameraManager.getSupportedCameras(); 31if (cameraArray.length <= 0) { 32 console.error("cameraManager.getSupportedCameras error") 33 return; 34} 35 36for (let index = 0; index < cameraArray.length; index++) { 37 console.info('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID 38 console.info('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置 39 console.info('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型 40 console.info('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型 41} 42 43// 创建相机输入流 44let cameraInput 45try { 46 cameraInput = cameraManager.createCameraInput(cameraArray[0]); 47} catch (error) { 48 console.error('Failed to createCameraInput errorCode = ' + error.code); 49} 50 51// 监听cameraInput错误信息 52let cameraDevice = cameraArray[0]; 53cameraInput.on('error', cameraDevice, (error) => { 54 console.info(`Camera input error code: ${error.code}`); 55}) 56 57// 打开相机 58await cameraInput.open(); 59 60// 获取相机设备支持的输出流能力 61let cameraOutputCap = cameraManager.getSupportedOutputCapability(cameraArray[0]); 62if (!cameraOutputCap) { 63 console.error("cameraManager.getSupportedOutputCapability error") 64 return; 65} 66console.info("outputCapability: " + JSON.stringify(cameraOutputCap)); 67 68let previewProfilesArray = cameraOutputCap.previewProfiles; 69if (!previewProfilesArray) { 70 console.error("createOutput previewProfilesArray == null || undefined") 71} 72 73let photoProfilesArray = cameraOutputCap.photoProfiles; 74if (!photoProfilesArray) { 75 console.error("createOutput photoProfilesArray == null || undefined") 76} 77 78// 创建预览输出流,其中参数 surfaceId 参考上文 XComponent 组件,预览流为XComponent组件提供的surface 79let previewOutput 80try { 81 previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId) 82} catch (error) { 83 console.error("Failed to create the PreviewOutput instance.") 84} 85 86// 监听预览输出错误信息 87previewOutput.on('error', (error) => { 88 console.info(`Preview output error code: ${error.code}`); 89}) 90 91// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置 92let imageReceiver = await image.createImageReceiver(1920, 1080, 4, 8) 93// 获取照片显示SurfaceId 94let photoSurfaceId = await imageReceiver.getReceivingSurfaceId() 95// 创建拍照输出流 96let photoOutput 97try { 98 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId) 99} catch (error) { 100 console.error('Failed to createPhotoOutput errorCode = ' + error.code); 101} 102//创建会话 103let captureSession 104try { 105 captureSession = cameraManager.createCaptureSession() 106} catch (error) { 107 console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code); 108} 109 110// 监听session错误信息 111captureSession.on('error', (error) => { 112 console.info(`Capture session error code: ${error.code}`); 113}) 114 115// 开始配置会话 116try { 117 captureSession.beginConfig() 118} catch (error) { 119 console.error('Failed to beginConfig. errorCode = ' + error.code); 120} 121 122// 向会话中添加相机输入流 123try { 124 captureSession.addInput(cameraInput) 125} catch (error) { 126 console.error('Failed to addInput. errorCode = ' + error.code); 127} 128 129// 向会话中添加预览输出流 130try { 131 captureSession.addOutput(previewOutput) 132} catch (error) { 133 console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code); 134} 135 136// 向会话中添加拍照输出流 137try { 138 captureSession.addOutput(photoOutput) 139} catch (error) { 140 console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code); 141} 142 143// 提交会话配置 144await captureSession.commitConfig() 145 146// 启动会话 147await captureSession.start().then(() => { 148 console.info('Promise returned to indicate the session start success.'); 149}) 150// 判断设备是否支持闪光灯 151let flashStatus 152try { 153 flashStatus = captureSession.hasFlash() 154} catch (error) { 155 console.error('Failed to hasFlash. errorCode = ' + error.code); 156} 157console.info('Promise returned with the flash light support status:' + flashStatus); 158 159if (flashStatus) { 160 // 判断是否支持自动闪光灯模式 161 let flashModeStatus 162 try { 163 let status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO) 164 flashModeStatus = status 165 } catch (error) { 166 console.error('Failed to check whether the flash mode is supported. errorCode = ' + error.code); 167 } 168 if(flashModeStatus) { 169 // 设置自动闪光灯模式 170 try { 171 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO) 172 } catch (error) { 173 console.error('Failed to set the flash mode. errorCode = ' + error.code); 174 } 175 } 176} 177 178// 判断是否支持连续自动变焦模式 179let focusModeStatus 180try { 181 let status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO) 182 focusModeStatus = status 183} catch (error) { 184 console.error('Failed to check whether the focus mode is supported. errorCode = ' + error.code); 185} 186 187if (focusModeStatus) { 188 // 设置连续自动变焦模式 189 try { 190 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO) 191 } catch (error) { 192 console.error('Failed to set the focus mode. errorCode = ' + error.code); 193 } 194} 195 196// 获取相机支持的可变焦距比范围 197let zoomRatioRange 198try { 199 zoomRatioRange = captureSession.getZoomRatioRange() 200} catch (error) { 201 console.error('Failed to get the zoom ratio range. errorCode = ' + error.code); 202} 203 204// 设置可变焦距比 205try { 206 captureSession.setZoomRatio(zoomRatioRange[0]) 207} catch (error) { 208 console.error('Failed to set the zoom ratio value. errorCode = ' + error.code); 209} 210let settings = { 211 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // 设置图片质量高 212 rotation: camera.ImageRotation.ROTATION_0 // 设置图片旋转角度0 213} 214// 使用当前拍照设置进行拍照 215photoOutput.capture(settings, async (err) => { 216 if (err) { 217 console.error('Failed to capture the photo ${err.message}'); 218 return; 219 } 220 console.info('Callback invoked to indicate the photo capture request success.'); 221}); 222// 停止当前会话 223captureSession.stop() 224 225// 释放相机输入流 226cameraInput.close() 227 228// 释放预览输出流 229previewOutput.release() 230 231// 释放拍照输出流 232photoOutput.release() 233 234// 释放会话 235captureSession.release() 236 237// 会话置空 238captureSession = null 239``` 240 241