1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15// @ts-nocheck 16 17import camera from '@ohos.multimedia.camera'; 18import deviceInfo from '@ohos.deviceInfo'; 19import fileio from '@ohos.fileio'; 20import image from '@ohos.multimedia.image'; 21import media from '@ohos.multimedia.media'; 22import Logger from '../model/Logger'; 23import prompt from '@ohos.prompt'; 24import fs from '@ohos.file.fs'; 25import screen from '@ohos.screen'; 26import DateTimeUtil from '../model/DateTimeUtil'; 27 28const CameraSize = { 29 WIDTH: 1280, 30 HEIGHT: 720 31} 32 33class CameraService { 34 private tag: string = 'qlw CameraService'; 35 private static instance: CameraService = new CameraService(); 36 private cameraManager: camera.CameraManager = undefined; 37 cameras: Array<camera.CameraDevice> = undefined; 38 private cameraInput: camera.CameraInput = undefined; 39 private previewOutput: camera.PreviewOutput = undefined; 40 private photoOutput: camera.PhotoOutput = undefined; 41 private cameraOutputCapability: camera.CameraOutputCapability = undefined; 42 captureSession: camera.CaptureSession = undefined; 43 private mReceiver: image.ImageReceiver = undefined; 44 private fd: number = -1; 45 private videoRecorder: media.VideoRecorder = undefined; 46 private videoOutput: camera.VideoOutput = undefined; 47 private handleTakePicture: (photoUri: string) => void = undefined; 48 private videoConfig: any = { 49 audioSourceType: 1, 50 videoSourceType: 0, 51 profile: { 52 audioBitrate: 48000, 53 audioChannels: 2, 54 audioCodec: 'audio/mp4a-latm', 55 audioSampleRate: 48000, 56 durationTime: 1000, 57 fileFormat: 'mp4', 58 videoBitrate: 280000, 59 videoCodec: 'video/avc', 60 videoFrameWidth: 640, 61 videoFrameHeight: 480, 62 videoFrameRate: 15, 63 64 }, 65 rotation: 270, 66 url: '', 67 orientationHint: 0, 68 location: { latitude: 30, longitude: 130 }, 69 maxSize: 10000, 70 maxDuration: 10000 71 }; 72 private videoProfileObj: camera.VideoProfile = { 73 format: 1, 74 size: { 75 "width": 640, 76 "height": 480 77 }, 78 frameRateRange: { 79 "min": 5, 80 "max": 5 81 } 82 }; 83 private photoProfileObj: camera.Profile = { 84 format: 1, 85 size: { 86 "width": 640, 87 "height": 480 88 } 89 }; 90 private videoOutputStopBol: boolean = true; 91 resolution: any = null; 92 photoResolution: any = null; 93 videoResolution: any = null; 94 screensObj: any = null; 95 96 async getScreensObj() { 97 let screens = await screen.getAllScreens(); 98 this.screensObj = screens[0].supportedModeInfo; 99 Logger.info(this.tag, ` this.screensObj success` + JSON.stringify(this.screensObj)); 100 } 101 102 constructor() { 103 try { 104 this.mReceiver = image.createImageReceiver(CameraSize.WIDTH, CameraSize.HEIGHT, image.ImageFormat.JPEG, 8); 105 Logger.info(this.tag, 'createImageReceiver'); 106 this.mReceiver.on('imageArrival', () => { 107 Logger.info(this.tag, 'imageArrival'); 108 this.mReceiver.readNextImage((err, image) => { 109 Logger.info(this.tag, 'readNextImage'); 110 if (err || image === undefined) { 111 Logger.error(this.tag, 'failed to get valid image'); 112 return 113 } 114 image.getComponent(4, (errMsg, img) => { 115 Logger.info(this.tag, 'getComponent'); 116 if (errMsg || img === undefined) { 117 Logger.info(this.tag, 'failed to get valid buffer'); 118 return 119 } 120 let buffer; 121 if (img.byteBuffer) { 122 buffer = img.byteBuffer; 123 } else { 124 Logger.error(this.tag, 'img.byteBuffer is undefined'); 125 } 126 this.savePicture(buffer, image); 127 }) 128 }) 129 }) 130 } catch (err) { 131 Logger.info(this.tag, `image Receiver err ${err.message}`); 132 } 133 } 134 135 async savePicture(buffer: ArrayBuffer, img: image.Image) { 136 try { 137 Logger.info(this.tag, 'savePicture'); 138 let dateTimeUtil = new DateTimeUtil(); 139 let filesDir = globalThis.abilityContext.filesDir; 140 let name = `${dateTimeUtil.getDate()}_${dateTimeUtil.getTime()}` 141 let path = filesDir + '/IMG_' + name + '.jpg'; 142 Logger.info(this.tag, `getFileFd path : ${path}`); 143 let Fd = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE); 144 fs.writeSync(Fd.fd, buffer); 145 if (Fd.fd) { 146 fs.closeSync(Fd.fd); 147 Logger.info(this.tag, `release Fd.fd success`); 148 } 149 await img.release(); 150 Logger.info(this.tag, 'save image done'); 151 if (this.handleTakePicture) { 152 this.handleTakePicture(path) 153 } 154 } catch (err) { 155 Logger.info(this.tag, `save picture err ${err.message}`); 156 } 157 } 158 159 async initCamera(surfaceId: number, cameraDeviceIndex: number, obj?, photoIndex?, previewObj?) { 160 await this.getCameraManagerFn(); 161 await this.getSupportedCamerasFn(); 162 this.cameraOutputCapability = this.cameraManager.getSupportedOutputCapability(this.cameras[cameraDeviceIndex]); 163 let videoPro = this.cameraOutputCapability.videoProfiles[0].format; 164 try { 165 if (videoPro === camera.CameraFormat.CAMERA_FORMAT_RGBA_8888) { 166 this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_ES; 167 } else { 168 this.videoConfig.videoSourceType = media.VideoSourceType.VIDEO_SOURCE_TYPE_SURFACE_YUV; 169 } 170 Logger.info(this.tag, `cameraDeviceIndex success: ${cameraDeviceIndex}`); 171 await this.getScreensObj(); 172 await this.releaseCamera(); 173 await this.getSupportedOutputCapabilityFn(cameraDeviceIndex); 174 if (previewObj) { 175 previewObj.format = this.cameraOutputCapability.previewProfiles[0].format; 176 Logger.info(this.tag, `previewObj format: ${previewObj.format}`); 177 } 178 await this.createPreviewOutputFn(previewObj ? previewObj : this.cameraOutputCapability.previewProfiles[0], surfaceId); 179 // await this.createPhotoOutputFn(this.photoProfileObj) 180 await this.createPhotoOutputFn(obj ? obj : this.cameraOutputCapability.photoProfiles[photoIndex?photoIndex:0]); 181 await this.createCameraInputFn(this.cameras[cameraDeviceIndex]); 182 await this.cameraInputOpenFn(); 183 await this.sessionFlowFn(); 184 185 } catch (err) { 186 Logger.info(this.tag, 'initCamera err: ' + JSON.stringify(err.message)); 187 } 188 } 189 190 // 曝光模式 191 isExposureModeSupportedFn(ind) { 192 try { 193 let status = this.captureSession.isExposureModeSupported(ind); 194 Logger.info(this.tag, `isExposureModeSupported success: ${status}`); 195 prompt.showToast({ 196 message: status ? '支持此模式' : '不支持此模式', 197 duration: 2000, 198 bottom: '60%' 199 }) 200 // 设置曝光模式 201 this.captureSession.setExposureMode(ind); 202 Logger.info(this.tag, `setExposureMode success`); 203 // 获取当前曝光模式 204 let exposureMode = this.captureSession.getExposureMode(); 205 Logger.info(this.tag, `getExposureMode success: ${exposureMode}`); 206 } catch (err) { 207 Logger.info(this.tag, `isExposureModeSupportedFn fail: ${err} , message: ${err.message}, code: ${err.code}`); 208 } 209 } 210 211 // 曝光区域 212 isMeteringPoint(Point1) { 213 try { 214 // 获取当前曝光模式 215 let exposureMode = this.captureSession.getExposureMode(); 216 Logger.info(this.tag, `getExposureMode success: ${exposureMode}`); 217 // 设置曝光区域中心点 218 this.captureSession.setMeteringPoint(Point1); 219 Logger.info(this.tag, `setMeteringPoint success`); 220 // 查询曝光区域中心点 221 let exposurePoint = this.captureSession.getMeteringPoint(); 222 Logger.info(this.tag, `getMeteringPoint success: ${exposurePoint}`); 223 } catch (err) { 224 Logger.info(this.tag, `isMeteringPoint fail err: ${err}, message: ${err.message}, code: ${err.code}`); 225 } 226 } 227 228 // 曝光补偿 229 isExposureBiasRange(ind) { 230 try { 231 // 查询曝光补偿范围 232 let biasRangeArray = this.captureSession.getExposureBiasRange(); 233 Logger.info(this.tag, `getExposureBiasRange success: ${biasRangeArray}`); 234 // 设置曝光补偿 235 this.captureSession.setExposureBias(ind); 236 Logger.info(this.tag, `setExposureBias success: ${ind}`); 237 // 查询当前曝光值 238 let exposureValue = this.captureSession.getExposureValue(); 239 Logger.info(this.tag, `getExposureValue success: ${exposureValue}`); 240 } catch (err) { 241 Logger.info(this.tag, `isExposureBiasRange fail err: ${err}, message: ${err.message}, code: ${err.code}`); 242 } 243 } 244 245 // 对焦模式 246 isFocusMode(ind) { 247 try { 248 // 检测对焦模式是否支持 249 let status = this.captureSession.isFocusModeSupported(ind); 250 Logger.info(this.tag, `isFocusModeSupported success: ${status}`); 251 prompt.showToast({ 252 message: status ? '支持此模式' : '不支持此模式', 253 duration: 2000, 254 bottom: '60%' 255 }) 256 // 设置对焦模式 257 this.captureSession.setFocusMode(ind); 258 Logger.info(this.tag, `setFocusMode success`); 259 // 获取当前对焦模式 260 let afMode = this.captureSession.getFocusMode(); 261 Logger.info(this.tag, `getFocusMode success: ${afMode}`); 262 } catch (err) { 263 Logger.info(this.tag, `isFocusMode fail err: ${err}, message: ${err.message}, code: ${err.code}`); 264 } 265 } 266 267 // 对焦点 268 isFocusPoint(Point) { 269 try { 270 // 设置对焦点 271 this.captureSession.setFocusPoint(Point); 272 Logger.info(this.tag, `setFocusPoint success`); 273 // 获取当前对焦点 274 let point = this.captureSession.getFocusPoint(); 275 Logger.info(this.tag, `getFocusPoint success: ${point}`); 276 } catch (err) { 277 Logger.info(this.tag, `isFocusPoint fail err: ${err}, message: ${err.message}, code: ${err.code}`); 278 } 279 } 280 281 // 闪光灯 282 hasFlashFn(ind) { 283 try { 284 // 检测是否有闪光灯 285 let status = this.captureSession.hasFlash(); 286 Logger.info(this.tag, `hasFlash success: ${status}`); 287 // 检测闪光灯模式是否支持 288 let status1 = this.captureSession.isFlashModeSupported(ind); 289 Logger.info(this.tag, `isFlashModeSupported success: ${status1}`); 290 prompt.showToast({ 291 message: status1 ? '支持此模式' : '不支持此模式', 292 duration: 2000, 293 bottom: '60%' 294 }) 295 // 设置闪光灯模式 296 this.captureSession.setFlashMode(ind); 297 Logger.info(this.tag, `setFlashMode success`); 298 // 获取当前设备的闪光灯模式 299 let flashMode = this.captureSession.getFlashMode(); 300 Logger.info(this.tag, `getFlashMode success: ${flashMode}`); 301 } catch (err) { 302 Logger.info(this.tag, `hasFlashFn fail err: ${err}, message: ${err.message}, code: ${err.code}`); 303 } 304 } 305 306 // 变焦 307 setZoomRatioFn(num) { 308 try { 309 // 获取当前支持的变焦范围 310 let zoomRatioRange = this.captureSession.getZoomRatioRange(); 311 Logger.info(this.tag, `getZoomRatioRange success: ${zoomRatioRange}`); 312 // 设置变焦比 313 Logger.info(this.tag, `setZoomRatioFn num: ${num}`); 314 this.captureSession.setZoomRatio(num); 315 Logger.info(this.tag, `setZoomRatio success`); 316 // 获取当前对焦比 317 let zoomRatio = this.captureSession.getZoomRatio(); 318 Logger.info(this.tag, `getZoomRatio success: ${zoomRatio}`); 319 } catch (err) { 320 Logger.info(this.tag, `setZoomRatioFn fail err: ${err}, message: ${err.message}, code: ${err.code}`); 321 } 322 } 323 324 // 防抖 325 isVideoStabilizationModeSupportedFn(ind) { 326 try { 327 // 查询是否支持指定的视频防抖模式 328 let isSupported = this.captureSession.isVideoStabilizationModeSupported(ind); 329 Logger.info(this.tag, `isVideoStabilizationModeSupported success: ${isSupported}`); 330 prompt.showToast({ 331 message: isSupported ? '支持此模式' : '不支持此模式', 332 duration: 2000, 333 bottom: '60%' 334 }) 335 // 设置视频防抖 336 this.captureSession.setVideoStabilizationMode(ind); 337 Logger.info(this.tag, `setVideoStabilizationMode success`); 338 // 查询当前正在使用的防抖模式 339 let vsMode = this.captureSession.getActiveVideoStabilizationMode(); 340 Logger.info(this.tag, `getActiveVideoStabilizationMode success: ${vsMode}`); 341 } catch (err) { 342 Logger.info(this.tag, `isVideoStabilizationModeSupportedFn fail err: ${err}, message: ${err.message}, code: ${err.code}`); 343 } 344 } 345 346 setTakePictureCallback(callback) { 347 this.handleTakePicture = callback; 348 } 349 350 // 照片方向判断 351 onChangeRotation() { 352 if (globalThis.photoOrientation == 0) { 353 return 0 354 } 355 if (globalThis.photoOrientation == 1) { 356 return 90 357 } 358 if (globalThis.photoOrientation == 2) { 359 return 180 360 } 361 if (globalThis.photoOrientation == 3) { 362 return 270 363 } 364 } 365 366 // 照片地理位置逻辑,后续靠定位实现,当前传入固定值 367 onChangeLocation() { 368 if (globalThis.settingDataObj.locationBol) { 369 return { 370 latitude: 12, 371 longitude: 77, 372 altitude: 1000 373 } 374 } 375 return { 376 latitude: 0, 377 longitude: 0, 378 altitude: 0 379 } 380 } 381 382 // 拍照 383 async takePicture(imageRotation?) { 384 try { 385 Logger.info(this.tag, 'takePicture start') 386 let photoSettings = { 387 rotation: imageRotation ? Number(imageRotation) : 0, 388 quality: 1, 389 location: { 390 latitude: 0, 391 longitude: 0, 392 altitude: 0 393 }, 394 mirror: false 395 } 396 Logger.info(this.tag, `photoOutput capture photoSettings: ` + JSON.stringify(photoSettings)) 397 await this.photoOutput.capture(photoSettings) 398 Logger.info(this.tag, 'takePicture end') 399 } catch (err) { 400 Logger.info(this.tag, `takePicture fail err: ${err}, message: ${err.message}, code: ${err.code}`) 401 } 402 } 403 404 // 获取Fd 405 async getFileFd() { 406 let filesDir = globalThis.abilityContext.filesDir 407 Logger.info(this.tag, `beginConfig 3`) 408 let path = filesDir + '/' + 'test.mp4' 409 Logger.info(this.tag, `beginConfig 4`) 410 let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE | fs.OpenMode.TRUNC) 411 Logger.info(this.tag, `getFileFd : ${file.fd}`) 412 return file.fd 413 } 414 415 // 开始录制 416 async startVideo() { 417 try { 418 Logger.info(this.tag, `startVideo begin`) 419 await this.captureSession.stop() 420 await this.captureSession.beginConfig() 421 // await this.captureSession.removeOutput(this.photoOutput); 422 this.fd = await this.getFileFd() 423 this.videoRecorder = await media.createVideoRecorder() 424 this.videoConfig.url = `fd://${this.fd}` 425 this.videoConfig.profile.videoFrameWidth = this.cameraOutputCapability.videoProfiles[0].size.width 426 this.videoConfig.profile.videoFrameHeight = this.cameraOutputCapability.videoProfiles[0].size.height 427 await this.videoRecorder.prepare(this.videoConfig) 428 let videoId = await this.videoRecorder.getInputSurface() 429 // Logger.info(this.tag, `videoProfileObj: ` + JSON.stringify(this.videoProfileObj)) 430 Logger.info(this.tag, `videoConfig.profile: ` + JSON.stringify(this.videoConfig.profile)) 431 Logger.info(this.tag, `videoProfileObj1: ` + JSON.stringify(this.cameraOutputCapability.videoProfiles[0])) 432 this.videoOutput = await this.cameraManager.createVideoOutput(this.cameraOutputCapability.videoProfiles[0], videoId) 433 Logger.info(this.tag, `createVideoOutput success: ${this.videoOutput}`) 434 await this.captureSession.addOutput(this.videoOutput) 435 Logger.info(this.tag, `startVideo begin1`) 436 await this.captureSession.commitConfig() 437 Logger.info(this.tag, `startVideo begin2`) 438 await this.captureSession.start() 439 Logger.info(this.tag, `startVideo begin3`) 440 await this.videoOutput.start() 441 Logger.info(this.tag, `startVideo begin4`) 442 await this.videoRecorder.start().then(() => { 443 setTimeout(async () => { 444 await this.stopVideo() 445 Logger.info(this.tag, `setTimeout stopVideo end`) 446 }, 3000) 447 }) 448 Logger.info(this.tag, `videoOutput end`) 449 } catch (err) { 450 Logger.info(this.tag, `startVideo fail err: ${err}, message: ${err.message}, code: ${err.code}`) 451 } 452 } 453 454 // 停止录制 455 async stopVideo() { 456 try { 457 if (this.videoRecorder) { 458 await this.videoRecorder.stop() 459 await this.videoRecorder.release() 460 } 461 if (this.videoOutput) { 462 if (this.videoOutputStopBol) { 463 await this.videoOutput.stop() 464 } 465 await this.videoOutput.release() 466 } 467 Logger.info(this.tag, `stopVideo success`) 468 } catch (err) { 469 Logger.info(this.tag, `stopVideo fail err: ${err}, message: ${err.message}, code: ${err.code}`) 470 } 471 } 472 473 // 查询相机设备在模式下支持的输出能力 474 async getSupportedOutputCapabilityFn(cameraDeviceIndex) { 475 Logger.info(this.tag, `cameraOutputCapability cameraId: ${this.cameras[cameraDeviceIndex].cameraId}`) 476 // @ts-ignore 477 this.cameraOutputCapability = this.cameraManager.getSupportedOutputCapability(this.cameras[cameraDeviceIndex]) 478 let previewSize = [] 479 let photoSize = [] 480 let videoSize = [] 481 this.cameraOutputCapability.previewProfiles.forEach((item, index) => { 482 Logger.info(this.tag, `cameraOutputCapability previewProfiles index: ${index}, item:` + JSON.stringify(item)) 483 previewSize.push({ 484 value: `${item.size.width}x${item.size.height}` 485 }) 486 }) 487 this.cameraOutputCapability.photoProfiles.forEach((item, index) => { 488 Logger.info(this.tag, `cameraOutputCapability photoProfiles index: ${index}, item:` + JSON.stringify(item)) 489 photoSize.push({ 490 value: `${item.size.width}x${item.size.height}` 491 }) 492 }) 493 this.cameraOutputCapability.videoProfiles.forEach((item, index) => { 494 Logger.info(this.tag, `cameraOutputCapability videoProfiles index: ${index}, item:` + JSON.stringify(item)) 495 videoSize.push({ 496 value: `${item.size.width}x${item.size.height}` 497 }) 498 }) 499 Logger.info(this.tag, `cameraOutputCapability previewProfiles:` + JSON.stringify(this.cameraOutputCapability.previewProfiles)) 500 Logger.info(this.tag, `cameraOutputCapability photoProfiles:` + JSON.stringify(this.cameraOutputCapability.photoProfiles)) 501 Logger.info(this.tag, `cameraOutputCapability videoProfiles:` + JSON.stringify(this.cameraOutputCapability.videoProfiles)) 502 Logger.info(this.tag, `cameraOutputCapability previewProfiles previewSize:` + JSON.stringify(previewSize)) 503 this.resolution = previewSize 504 this.photoResolution = photoSize 505 this.videoResolution = videoSize 506 return previewSize 507 } 508 509 // 释放会话及其相关参数 510 async releaseCamera() { 511 try { 512 if (this.cameraInput) { 513 await this.cameraInput.release() 514 } 515 if (this.previewOutput) { 516 await this.previewOutput.release() 517 } 518 if (this.photoOutput) { 519 await this.photoOutput.release() 520 } 521 if (this.videoOutput) { 522 await this.videoOutput.release() 523 } 524 if (this.captureSession) { 525 await this.captureSession.release() 526 } 527 Logger.info(this.tag, `releaseCamera success`) 528 } catch (err) { 529 Logger.info(this.tag, `releaseCamera fail err: ${err}, message: ${err.message}, code: ${err.code}`) 530 } 531 } 532 533 // 释放会话 534 async releaseSession() { 535 await this.previewOutput.stop() 536 await this.photoOutput.release() 537 await this.captureSession.release() 538 Logger.info(this.tag, `releaseSession success`) 539 } 540 541 // 获取相机管理器实例 542 async getCameraManagerFn() { 543 try { 544 this.cameraManager = await camera.getCameraManager(globalThis.abilityContext) 545 Logger.info(this.tag, `getCameraManager success: ` + JSON.stringify(this.cameraManager)) 546 } catch (err) { 547 Logger.info(this.tag, `getCameraManagerFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 548 } 549 } 550 551 // 获取支持指定的相机设备对象 552 async getSupportedCamerasFn() { 553 try { 554 this.cameras = await this.cameraManager.getSupportedCameras() 555 Logger.info(this.tag, `getSupportedCameras success: ` + JSON.stringify(this.cameras)) 556 Logger.info(this.tag, `getSupportedCameras length success: ${this.cameras.length}`) 557 } catch (err) { 558 Logger.info(this.tag, `getSupportedCamerasFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 559 } 560 } 561 562 // 创建previewOutput输出对象 563 async createPreviewOutputFn(previewProfilesObj, surfaceId) { 564 try { 565 Logger.info(this.tag, `createPreviewOutputFn previewProfilesObj success: ` + JSON.stringify(previewProfilesObj)) 566 this.previewOutput = await this.cameraManager.createPreviewOutput(previewProfilesObj, surfaceId.toString()) 567 Logger.info(this.tag, `createPreviewOutputFn success: ` + JSON.stringify(this.previewOutput)) 568 } catch (err) { 569 Logger.info(this.tag, `createPreviewOutputFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 570 } 571 } 572 573 // 创建photoOutput输出对象 574 async createPhotoOutputFn(photoProfileObj) { 575 try { 576 Logger.info(this.tag, `createPhotoOutputFn photoProfileObj success: ` + JSON.stringify(photoProfileObj)) 577 let mSurfaceId = await this.mReceiver.getReceivingSurfaceId() 578 this.photoOutput = await this.cameraManager.createPhotoOutput(photoProfileObj, mSurfaceId) 579 Logger.info(this.tag, `createPhotoOutputFn success: ` + JSON.stringify(this.photoOutput)) 580 } catch (err) { 581 Logger.info(this.tag, `createPhotoOutputFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 582 } 583 } 584 585 // 创建cameraInput输出对象 586 async createCameraInputFn(cameraDeviceIndex) { 587 try { 588 this.cameraInput = await this.cameraManager.createCameraInput(cameraDeviceIndex) 589 Logger.info(this.tag, `createCameraInputFn success: ${this.cameraInput}`) 590 } catch (err) { 591 Logger.info(this.tag, `createCameraInputFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 592 } 593 } 594 595 // 打开相机 596 async cameraInputOpenFn() { 597 await this.cameraInput.open() 598 .then((data) => { 599 Logger.info(this.tag, `cameraInputOpenFn open success: ${data}`) 600 }) 601 .catch((err) => { 602 Logger.info(this.tag, `cameraInputOpenFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 603 }) 604 } 605 606 // 会话流程 607 async sessionFlowFn() { 608 try { 609 // 创建captureSession实例 610 this.captureSession = await this.cameraManager.createCaptureSession() 611 // 开始配置会话 612 await this.captureSession.beginConfig() 613 // cameraInput加入会话 614 await this.captureSession.addInput(this.cameraInput) 615 // previewOutput加入会话 616 await this.captureSession.addOutput(this.previewOutput) 617 // photoOutput加入会话 618 await this.captureSession.addOutput(this.photoOutput) 619 // 提交配置会话 620 await this.captureSession.commitConfig() 621 // 开启会话 622 await this.captureSession.start() 623 Logger.info(this.tag, `sessionFlowFn success`) 624 } catch (err) { 625 Logger.info(this.tag, `sessionFlowFn fail err: ${err}, message: ${err.message}, code: ${err.code}`) 626 } 627 } 628} 629 630export default new CameraService()