• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import deviceInfo from '@ohos.deviceInfo'
17
18import { Log } from '../utils/Log'
19
20export class CameraPlatformCapability {
21  private TAG = '[CameraPlatformCapability]:'
22  public mZoomRatioRangeMap = new Map()
23  public mPhotoPreviewSizeMap = new Map()
24  public mVideoPreviewSizeMap = new Map()
25  public mImageSizeMap = new Map()
26  public mVideoFrameSizeMap = new Map()
27  public mCameraCount = 0
28  // TODO 需要针对不同设备固定不同的值
29  public mPhotoPreviewSize = [
30    { width: 640, height: 480 }, //Photo 4:3
31    { width: 720, height: 720 }, //Photo 1:1
32    { width: 1920, height: 1080 }, //Photo 16:9
33  ]
34  public mVideoPreviewSize = [
35    { width: 1280, height: 720 }, //Video 16:9 720p
36    { width: 1920, height: 1080 }, //Video 16:9 1080p
37    { width: 1920, height: 1080 } //Video 16:9 4k
38  ]
39  public mImageSize = [
40    { width: 1280, height: 960 }, //4:3
41    { width: 3120, height: 3120 }, //1:1
42    { width: 1920, height: 1080 } //16:9
43  ]
44  public mVideoFrameSize = [
45    { width: 1280, height: 720 }, //16:9 720p
46    { width: 1920, height: 1080 }, //16:9 1080p
47    { width: 3840, height: 2160 } //16:9 4k
48  ]
49
50  constructor() {
51  }
52
53  public static getInstance(): CameraPlatformCapability {
54    if (!globalThis?.sInstanceCapability) {
55      globalThis.sInstanceCapability = new CameraPlatformCapability()
56    }
57    return globalThis.sInstanceCapability;
58  }
59
60  public async init(cameraCount: number) {
61    Log.info(`${this.TAG} init E.`)
62    this.mCameraCount = cameraCount
63    Log.info(`${this.TAG} init X.`)
64  }
65
66  public async calcSupportedSizes(cameraInput, outputCapability) {
67    Log.info(`${this.TAG} calcSupportedSizes start.`)
68    if (deviceInfo.deviceType == 'default') {
69      return
70    }
71    const photoSize = outputCapability.photoProfiles //CAMERA_FORMAT_JPEG
72    const previewCurSize = outputCapability.previewProfiles //CAMERA_FORMAT_YCRCb_420_SP
73
74    this.mImageSize[0] = this.getMaxSize(photoSize, 4, 3)
75    this.mImageSize[1] = this.getMaxSize(photoSize, 1, 1)
76    this.mImageSize[2] = this.getMaxSize(photoSize, 16, 9)
77    this.mPhotoPreviewSize[0] = this.getMaxSize(previewCurSize, 4, 3)
78    this.mPhotoPreviewSize[1] = this.getMaxSize(previewCurSize, 1, 1)
79    this.mPhotoPreviewSize[2] = this.getMaxSize(previewCurSize, 16, 9)
80
81    this.mVideoFrameSize[0] = this.getSpecifiedSize(previewCurSize, 1280, 720)
82    this.mVideoFrameSize[1] = this.getSpecifiedSize(previewCurSize, 1920, 1080)
83    this.mVideoFrameSize[2] = this.getSpecifiedSize(previewCurSize, 3840, 2160)
84    this.mVideoPreviewSize[0] = this.mVideoFrameSize[0]
85    this.mVideoPreviewSize[1] = this.mVideoFrameSize[1]
86    this.mVideoPreviewSize[2] = this.mVideoFrameSize[2]
87    Log.info(`${this.TAG} calcSupportedSizes end.`)
88  }
89
90  private getMaxSize(sizeList, width: number, height: number) {
91    const maxSize = { width: 0, height: 0 }
92    const fitList = []
93    for (let i = 0; i < sizeList.length; i++) {
94      const errorValue = sizeList[i].size.width * height - sizeList[i].size.height * width
95      if (errorValue <= 4 && errorValue >= -4) {
96        fitList.push(sizeList[i])
97      }
98    }
99    if (fitList.length == 0) {
100      Log.error(`${this.TAG} calc failed based on the supportedSizesList, try default value.`)
101      maxSize.width = 640
102      maxSize.height = 480
103      Log.info(`${this.TAG} -----------SupportedSizes List Start-----------`)
104      for (let i = 0; i < sizeList.length; i++) {
105        Log.info(`${this.TAG} supportedSize width: ${sizeList[i].size.width} height: ${sizeList[i].size.height}`)
106      }
107      Log.info(`${this.TAG} -----------SupportedSizes List End-----------`)
108      return maxSize
109    } else {
110      const index = Math.floor(fitList.length / 2)
111      maxSize.width = fitList[index].size.width
112      maxSize.height = fitList[index].size.height
113    }
114    return maxSize
115  }
116
117  private getSpecifiedSize(sizeList, width: number, height: number) {
118    const specifiedSize = { width: 0, height: 0 }
119    for (let i = 0; i < sizeList.length; i++) {
120      const widthError = sizeList[i].size.width - width
121      const heightError = sizeList[i].size.height - height
122      if (widthError <= 4 && widthError >= -4 && heightError <= 4 && heightError >= -4) {
123        if (sizeList[i].size.width > specifiedSize.width) {
124          specifiedSize.width = sizeList[i].size.width
125          specifiedSize.height = sizeList[i].size.height
126        }
127      }
128    }
129    if (specifiedSize.width == 0) {
130      Log.error(`${this.TAG} calc failed based on the supportedSizesList, try default value.`)
131      specifiedSize.width = 1920
132      specifiedSize.height = 1080
133    }
134    return specifiedSize
135  }
136
137  public async getZoomRatioRange(captureSession) {
138    Log.info(`${this.TAG} getZoomRatioRange called`)
139    const zoomRatioRange = await captureSession.getZoomRatioRange()
140    Log.info(`${this.TAG} zoomRatioRange= ${zoomRatioRange}`)
141    return zoomRatioRange
142  }
143}