1# 相机开发指导 2 3## 场景介绍 4 5OpenHarmony相机模块支持相机业务的开发,开发者可以通过已开放的接口实现相机硬件的访问、操作和新功能开发,最常见的操作如:预览、拍照和录像等。开发者也可以通过合适的接口或者接口组合实现闪光灯控制、曝光时间控制、手动对焦和自动对焦控制、变焦控制以及更多的功能。 6 7开发者在调用Camera能力时,需要了解Camera的一些基本概念: 8 9- **相机静态能力**:用于描述相机的固有能力的一系列参数,比如朝向、支持的分辨率等信息。 10- **物理相机**:物理相机就是独立的实体摄像头设备。物理相机ID是用于标志每个物理摄像头的唯一字串。 11- **异步操作**:为保证UI线程不被阻塞,部分Camera接口采用异步调用方式。异步方式API均提供了callback函数和Promise函数。 12 13## 开发步骤 14 15### 接口说明 16 17详细API含义请参考:[相机管理API文档](../reference/apis/js-apis-camera.md) 18 19### 全流程场景 20 21包含流程:权限申请、创建实例、参数设置、会话管理、拍照、录像、释放资源等。 22 23#### 权限申请 24 25在使用相机之前,需要申请相机的相关权限,保证应用拥有相机硬件及其他功能权限,应用权限的介绍请参考权限章节,相机涉及权限如下表。 26 27| 权限名称 | 权限属性值 | 28| -------- | ------------------------------ | 29| 相机权限 | ohos.permission.CAMERA | 30| 录音权限 | ohos.permission.MICROPHONE | 31| 存储权限 | ohos.permission.WRITE_MEDIA | 32| 读取权限 | ohos.permission.READ_MEDIA | 33| 位置权限 | ohos.permission.MEDIA_LOCATION | 34 35参考代码如下: 36 37```typescript 38const PERMISSIONS: Array<string> = [ 39 'ohos.permission.CAMERA', 40 'ohos.permission.MICROPHONE', 41 'ohos.permission.MEDIA_LOCATION', 42 'ohos.permission.READ_MEDIA', 43 'ohos.permission.WRITE_MEDIA' 44] 45 46function applyPermission() { 47 console.info('[permission] get permission'); 48 globalThis.abilityContext.requestPermissionFromUser(PERMISSIONS) 49 } 50``` 51 52#### 创建实例 53 54在实现一个相机应用之前必须先创建一个独立的相机设备,然后才能继续相机的其他操作。如果此步骤操作失败,相机可能被占用或无法使用。如果被占用,必须等到相机释放后才能重新获取CameraManager对象。通过getSupportedCameras() 方法,获取当前使用的设备支持的相机列表。相机列表中存储了当前设备拥有的所有相机ID,如果列表不为空,则列表中的每个ID都支持独立创建相机对象;否则,说明正在使用的设备无可用的相机,不能继续后续的操作。相机设备具备预览、拍照、录像、Metadata等输出流,需要通过getSupportedOutputCapability()接口获取各个输出流的具体能力,通过该接口,可以获取当前设备支持的所有输出流能力,分别在CameraOutputCapability中的各个profile字段中,相机设备创建的建议步骤如下: 55 56```typescript 57import camera from '@ohos.multimedia.camera' 58import image from '@ohos.multimedia.image' 59import media from '@ohos.multimedia.media' 60 61// 创建CameraManager对象 62context: any = getContext(this) 63let cameraManager = camera.getCameraManager(this.context) 64if (!cameraManager) { 65 console.error("camera.getCameraManager error") 66 return; 67} 68// 监听相机状态变化 69cameraManager.on('cameraStatus', (cameraStatusInfo) => { 70 console.log(`camera : ${cameraStatusInfo.camera.cameraId}`); 71 console.log(`status: ${cameraStatusInfo.status}`); 72}) 73 74// 获取相机列表 75let cameraArray = cameraManager.getSupportedCameras(); 76if (cameraArray.length <= 0) { 77 console.error("cameraManager.getSupportedCameras error") 78 return; 79} 80 81for (let index = 0; index < cameraArray.length; index++) { 82 console.log('cameraId : ' + cameraArray[index].cameraId); // 获取相机ID 83 console.log('cameraPosition : ' + cameraArray[index].cameraPosition); // 获取相机位置 84 console.log('cameraType : ' + cameraArray[index].cameraType); // 获取相机类型 85 console.log('connectionType : ' + cameraArray[index].connectionType); // 获取相机连接类型 86} 87 88// 创建相机输入流 89let cameraInput 90try { 91 cameraInput = cameraManager.createCameraInput(cameraArray[0]); 92} catch () { 93 console.error('Failed to createCameraInput errorCode = ' + error.code); 94} 95 96// 监听cameraInput错误信息 97let cameraDevice = cameraArray[0]; 98cameraInput.on('error', cameraDevice, (error) => { 99 console.log(`Camera input error code: ${error.code}`); 100}) 101 102// 打开相机 103await cameraInput.open(); 104 105// 获取相机设备支持的输出流能力 106let cameraOutputCap = cameraManager.getSupportedOutputCapability(cameraArray[0]); 107if (!cameraOutputCap) { 108 console.error("cameraManager.getSupportedOutputCapability error") 109 return; 110} 111console.info("outputCapability: " + JSON.stringify(cameraOutputCap)); 112 113let previewProfilesArray = cameraOutputCap.previewProfiles; 114if (!previewProfilesArray) { 115 console.error("createOutput previewProfilesArray == null || undefined") 116} 117 118let photoProfilesArray = cameraOutputCap.photoProfiles; 119if (!photoProfilesArray) { 120 console.error("createOutput photoProfilesArray == null || undefined") 121} 122 123let videoProfilesArray = cameraOutputCap.videoProfiles; 124if (!videoProfilesArray) { 125 console.error("createOutput videoProfilesArray == null || undefined") 126} 127 128let metadataObjectTypesArray = cameraOutputCap.supportedMetadataObjectTypes; 129if (!metadataObjectTypesArray) { 130 console.error("createOutput metadataObjectTypesArray == null || undefined") 131} 132 133// 创建预览输出流,其中参数 surfaceId 参考下面 XComponent 组件,预览流为XComponent组件提供的surface 134let previewOutput 135try { 136 previewOutput = cameraManager.createPreviewOutput(previewProfilesArray[0], surfaceId) 137} catch (error) { 138 console.error("Failed to create the PreviewOutput instance.") 139} 140 141// 监听预览输出错误信息 142previewOutput.on('error', (error) => { 143 console.log(`Preview output error code: ${error.code}`); 144}) 145 146// 创建ImageReceiver对象,并设置照片参数:分辨率大小是根据前面 photoProfilesArray 获取的当前设备所支持的拍照分辨率大小去设置 147let imageReceiver = await image.createImageReceiver(1920, 1080, 4, 8) 148// 获取照片显示SurfaceId 149let photoSurfaceId = await imageReceiver.getReceivingSurfaceId() 150// 创建拍照输出流 151let photoOutput 152try { 153 photoOutput = cameraManager.createPhotoOutput(photoProfilesArray[0], photoSurfaceId) 154} catch (error) { 155 console.error('Failed to createPhotoOutput errorCode = ' + error.code); 156} 157 158// 创建视频录制的参数 159let videoConfig = { 160 audioSourceType: 1, 161 videoSourceType: 1, 162 profile: { 163 audioBitrate: 48000, 164 audioChannels: 2, 165 audioCodec: 'audio/mp4v-es', 166 audioSampleRate: 48000, 167 durationTime: 1000, 168 fileFormat: 'mp4', 169 videoBitrate: 48000, 170 videoCodec: 'video/mp4v-es', 171 videoFrameWidth: 640, 172 videoFrameHeight: 480, 173 videoFrameRate: 30 174 }, 175 url: 'file:///data/media/01.mp4', 176 orientationHint: 0, 177 maxSize: 100, 178 maxDuration: 500, 179 rotation: 0 180} 181 182// 创建录像输出流 183let videoRecorder 184media.createVideoRecorder().then((recorder) => { 185 console.log('createVideoRecorder called') 186 videoRecorder = recorder 187}) 188// 设置视频录制的参数 189videoRecorder.prepare(videoConfig) 190//获取录像SurfaceId 191let videoSurfaceId 192videoRecorder.getInputSurface().then((id) => { 193 console.log('getInputSurface called') 194 videoSurfaceId = id 195}) 196 197// 创建VideoOutput对象 198let videoOutput 199try { 200 videoOutput = cameraManager.createVideoOutput(videoProfilesArray[0], videoSurfaceId) 201} catch (error) { 202 console.error('Failed to create the videoOutput instance. errorCode = ' + error.code); 203} 204 205// 监听视频输出错误信息 206videoOutput.on('error', (error) => { 207 console.log(`Preview output error code: ${error.code}`); 208}) 209``` 210预览流、拍照流和录像流的输入均需要提前创建surface,其中预览流为XComponent组件提供的surface,拍照流为ImageReceiver提供的surface,录像流为VideoRecorder的surface。 211 212**XComponent** 213 214```typescript 215mXComponentController: XComponentController = new XComponentController // 创建XComponentController 216 217build() { 218 Flex() { 219 XComponent({ // 创建XComponent 220 id: '', 221 type: 'surface', 222 libraryname: '', 223 controller: this.mXComponentController 224 }) 225 .onload(() => { // 设置onload回调 226 // 设置Surface宽高(1920*1080),预览尺寸设置参考前面 previewProfilesArray 获取的当前设备所支持的预览分辨率大小去设置 227 this.mXComponentController.setXComponentSurfaceSize({surfaceWidth:1920,surfaceHeight:1080}) 228 // 获取Surface ID 229 globalThis.surfaceId = mXComponentController.getXComponentSurfaceId() 230 }) 231 .width('1920px') // 设置XComponent宽度 232 .height('1080px') // 设置XComponent高度 233 } 234} 235``` 236 237**ImageReceiver** 238 239```typescript 240function getImageReceiverSurfaceId() { 241 let receiver = image.createImageReceiver(640, 480, 4, 8) 242 console.log(TAG + 'before ImageReceiver check') 243 if (receiver !== undefined) { 244 console.log('ImageReceiver is ok') 245 surfaceId1 = receiver.getReceivingSurfaceId() 246 console.log('ImageReceived id: ' + JSON.stringify(surfaceId1)) 247 } else { 248 console.log('ImageReceiver is not ok') 249 } 250 } 251``` 252 253**VideoRecorder** 254 255```typescript 256function getVideoRecorderSurface() { 257 await getFd('CameraManager.mp4'); 258 mVideoConfig.url = mFdPath; 259 media.createVideoRecorder((err, recorder) => { 260 console.info('Entering create video receiver') 261 mVideoRecorder = recorder 262 console.info('videoRecorder is :' + JSON.stringify(mVideoRecorder)) 263 console.info('videoRecorder.prepare called.') 264 mVideoRecorder.prepare(mVideoConfig, (err) => { 265 console.info('videoRecorder.prepare success.') 266 mVideoRecorder.getInputSurface((err, id) => { 267 console.info('getInputSurface called') 268 mVideoSurface = id 269 console.info('getInputSurface surfaceId: ' + JSON.stringify(mVideoSurface)) 270 }) 271 }) 272 }) 273 } 274``` 275 276#### 会话管理 277 278##### 创建会话 279 280```typescript 281//创建会话 282let captureSession 283try { 284 captureSession = cameraManager.createCaptureSession() 285} catch (error) { 286 console.error('Failed to create the CaptureSession instance. errorCode = ' + error.code); 287} 288 289// 监听session错误信息 290captureSession.on('error', (error) => { 291 console.log(`Capture session error code: ${error.code}`); 292}) 293 294// 开始配置会话 295try { 296 captureSession.beginConfig() 297} catch (error) { 298 console.error('Failed to beginConfig. errorCode = ' + error.code); 299} 300 301// 向会话中添加相机输入流 302try { 303 captureSession.addInput(cameraInput) 304} catch (error) { 305 console.error('Failed to addInput. errorCode = ' + error.code); 306} 307 308// 向会话中添加预览输入流 309try { 310 captureSession.addOutput(previewOutput) 311} catch (error) { 312 console.error('Failed to addOutput(previewOutput). errorCode = ' + error.code); 313} 314 315// 向会话中添加拍照输出流 316try { 317 captureSession.addOutput(photoOutput) 318} catch (error) { 319 console.error('Failed to addOutput(photoOutput). errorCode = ' + error.code); 320} 321 322// 提交会话配置 323await captureSession.commitConfig() 324 325// 启动会话 326await captureSession.start().then(() => { 327 console.log('Promise returned to indicate the session start success.'); 328}) 329``` 330 331##### 切换会话 332 333```typescript 334// 停止当前会话 335await captureSession.stop() 336 337// 开始配置会话 338try { 339 captureSession.beginConfig() 340} catch (error) { 341 console.error('Failed to beginConfig. errorCode = ' + error.code); 342} 343 344// 从会话中移除拍照输出流 345try { 346 captureSession.removeOutput(photoOutput) 347} catch (error) { 348 console.error('Failed to removeOutput(photoOutput). errorCode = ' + error.code); 349} 350 351// 向会话中添加录像输出流 352try { 353 captureSession.addOutput(videoOutput) 354} catch (error) { 355 console.error('Failed to addOutput(videoOutput). errorCode = ' + error.code); 356} 357 358// 提交会话配置 359await captureSession.commitConfig() 360 361// 启动会话 362await captureSession.start().then(() => { 363 console.log('Promise returned to indicate the session start success.'); 364}) 365``` 366 367#### 参数设置 368 369```typescript 370// 判断设备是否支持闪光灯 371let flashStatus 372try { 373 flashStatus = captureSession.hasFlash() 374} catch (error) { 375 console.error('Failed to hasFlash. errorCode = ' + error.code); 376} 377console.log('Promise returned with the flash light support status:' + flashStatus); 378 379if (flashStatus) { 380 // 判断是否支持自动闪光灯模式 381 let flashModeStatus 382 try { 383 let status = captureSession.isFlashModeSupported(camera.FlashMode.FLASH_MODE_AUTO) 384 flashModeStatus = status 385 } catch (error) { 386 console.error('Failed to check whether the flash mode is supported. errorCode = ' + error.code); 387 } 388 if(flashModeStatus) { 389 // 设置自动闪光灯模式 390 try { 391 captureSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO) 392 } catch (error) { 393 console.error('Failed to set the flash mode. errorCode = ' + error.code); 394 } 395 } 396} 397 398// 判断是否支持连续自动变焦模式 399let focusModeStatus 400try { 401 let status = captureSession.isFocusModeSupported(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO) 402 focusModeStatus = status 403} catch (error) { 404 console.error('Failed to check whether the focus mode is supported. errorCode = ' + error.code); 405} 406 407if (focusModeStatus) { 408 // 设置连续自动变焦模式 409 try { 410 captureSession.setFocusMode(camera.FocusMode.FOCUS_MODE_CONTINUOUS_AUTO) 411 } catch (error) { 412 console.error('Failed to set the focus mode. errorCode = ' + error.code); 413 } 414} 415 416// 获取相机支持的可变焦距比范围 417let zoomRatioRange 418try { 419 zoomRatioRange = captureSession.getZoomRatioRange() 420} catch (error) { 421 console.error('Failed to get the zoom ratio range. errorCode = ' + error.code); 422} 423 424// 设置可变焦距比 425try { 426 captureSession.setZoomRatio(zoomRatioRange[0]) 427} catch (error) { 428 console.error('Failed to set the zoom ratio value. errorCode = ' + error.code); 429} 430``` 431 432#### 拍照 433 434```typescript 435let settings = { 436 quality: camera.QualityLevel.QUALITY_LEVEL_HIGH, // 设置图片质量高 437 rotation: camera.ImageRotation.ROTATION_0 // 设置图片旋转角度0 438} 439// 使用当前拍照设置进行拍照 440photoOutput.capture(settings, async (err) => { 441 if (err) { 442 console.error('Failed to capture the photo ${err.message}'); 443 return; 444 } 445 console.log('Callback invoked to indicate the photo capture request success.'); 446}); 447``` 448 449#### 录像 450 451```typescript 452// 启动录像输出流 453videoOutput.start(async (err) => { 454 if (err) { 455 console.error('Failed to start the video output ${err.message}'); 456 return; 457 } 458 console.log('Callback invoked to indicate the video output start success.'); 459}); 460 461// 开始录像 462videoRecorder.start().then(() => { 463 console.info('videoRecorder start success'); 464} 465 466// 停止录像 467videoRecorder.stop().then(() => { 468 console.info('stop success'); 469} 470 471// 停止录像输出流 472videoOutput.stop((err) => { 473 if (err) { 474 console.error('Failed to stop the video output ${err.message}'); 475 return; 476 } 477 console.log('Callback invoked to indicate the video output stop success.'); 478}); 479``` 480 481拍照保存接口可参考:[图片处理API文档](image.md#imagereceiver的使用) 482 483#### 释放资源 484 485```typescript 486// 停止当前会话 487captureSession.stop() 488 489// 释放相机输入流 490cameraInput.close() 491 492// 释放预览输出流 493previewOutput.release() 494 495// 释放拍照输出流 496photoOutput.release() 497 498// 释放录像输出流 499videoOutput.release() 500 501// 释放会话 502captureSession.release() 503 504// 会话置空 505captureSession = null 506``` 507 508## 流程图 509 510应用使用相机的流程示意图如下 511