• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-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
16import camera from '@ohos.multimedia.camera'
17import image from '@ohos.multimedia.image'
18import Logger from '../utils/Logger'
19import { QRCodeScanConst } from './QRCodeScanConst'
20
21/**
22 * 相机服务
23 */
24class CameraService {
25  private cameraInput: camera.CameraInput | undefined = undefined;
26  private photoOutput: camera.PhotoOutput | undefined = undefined;
27  private previewOutput: camera.PreviewOutput | undefined = undefined;
28  private captureSession: camera.CaptureSession | undefined = undefined;
29  public imageReceiver: image.ImageReceiver;
30
31  constructor(imgReceiver?: image.ImageReceiver) {
32    if (imgReceiver === undefined) {
33      this.imageReceiver = image.createImageReceiver(QRCodeScanConst.IMG_DEFAULT_SIZE_WIDTH,
34      QRCodeScanConst.IMG_DEFAULT_SIZE_HEIGHT, image.ImageFormat.JPEG, QRCodeScanConst.MAX_IMAGE_CAPACITY);
35    } else {
36      this.imageReceiver = image.createImageReceiver(imgReceiver.size.width, imgReceiver.size.height,
37      imgReceiver.format, imgReceiver.capacity);
38    }
39  }
40
41  /**
42   * 以指定参数触发一次拍照
43   */
44  takePicture() {
45    let photoSetting: camera.PhotoCaptureSetting = {
46      rotation: camera.ImageRotation.ROTATION_0,
47      quality: camera.QualityLevel.QUALITY_LEVEL_MEDIUM,
48      mirror: false
49    };
50    this.photoOutput?.capture(photoSetting);
51  }
52
53  /**
54   * 创建相机
55   */
56  async createCamera(surfaceId: string) {
57    Logger.info("createCamera start")
58    // 根据context获取CameraManager
59    let cameraManager = camera.getCameraManager(AppStorage.Get('context'))
60    // 获取Camera对象数组
61    let cameras = cameraManager.getSupportedCameras()
62    // 没有相机就停止
63    if (cameras.length === 0) {
64      Logger.error("createCamera: cameras length is 0.")
65      return
66    }
67    // 拿到相机列表中的第一个默认相机id, 根据id获取相机输入流
68    this.cameraInput = cameraManager.createCameraInput(cameras[0])
69    this.cameraInput.open()
70    // 获取cameraOutputCapability参数
71    let cameraOutputCapability = cameraManager.getSupportedOutputCapability(cameras[0])
72    // 获取相机输出流
73    this.previewOutput = cameraManager.createPreviewOutput(cameraOutputCapability.previewProfiles[0], surfaceId)
74    // 获取一个可以创建相片输出流的id
75    let receivingSurfaceId = await this.imageReceiver.getReceivingSurfaceId()
76    // 创建相片输出流
77    this.photoOutput = cameraManager.createPhotoOutput(cameraOutputCapability.photoProfiles[0], receivingSurfaceId)
78    // 获取捕获会话的实例
79    this.captureSession = cameraManager.createCaptureSession()
80    // 开始会话配置
81    this.captureSession.beginConfig()
82    // 使用相机输入流---添加一个摄像头输入流
83    this.captureSession.addInput(this.cameraInput)
84    // 使用相机输出流---添加一个摄像头输出
85    this.captureSession.addOutput(this.previewOutput)
86    // 使用相片输出流---添加相机照片的输出
87    this.captureSession.addOutput(this.photoOutput)
88    // 结束并提交配置
89    await this.captureSession.commitConfig()
90    // 开始捕获会话
91    await this.captureSession.start()
92    Logger.info("createCamera end")
93  }
94
95  /**
96   * 创建相机
97   */
98  async releaseCamera() {
99    await this.captureSession?.release();
100    await this.cameraInput?.close();
101    await this.photoOutput?.release();
102    await this.previewOutput?.release();
103    await this.imageReceiver.release();
104  }
105}
106
107export { CameraService }