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