• 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 { CameraPlatformCapability } from '../../camera/CameraPlatformCapability'
17import { CameraId } from './CameraId'
18import { Log } from '../../utils/Log'
19
20export default class AspectRatio {
21  private static TAG = '[AspectRatio]:'
22  public static readonly ALIAS = 'AspectRatio'
23  public static readonly ASPECT_RATIO_4_3 = '4:3'
24  public static readonly ASPECT_RATIO_1_1 = '1:1'
25  public static readonly ASPECT_RATIO_16_9 = '16:9'
26  public static readonly DEFAULT_VALUE = $r('app.string.photo_ratio_4_3')
27  public static readonly RESOURCE_RATIO_4_3 = AspectRatio.DEFAULT_VALUE
28  public static readonly RESOURCE_RATIO_1_1 = $r('app.string.photo_ratio_1_1')
29  public static readonly RESOURCE_RATIO_16_9 = $r('app.string.photo_ratio_16_9')
30
31  private static getIndex(aspectRatio: Resource): number {
32    if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_4_3.id) {
33      return 0
34    } else if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_1_1.id) {
35      return 1
36    } else if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_16_9.id) {
37      return 2
38    }
39    return 0
40  }
41
42  public static getPhotoPreviewSize(platform: CameraPlatformCapability, cameraId: string, aspectRatio: Resource) {
43    const index = AspectRatio.getIndex(aspectRatio)
44    Log.info(`${this.TAG} getPhotoPreviewSize size = ${JSON.stringify(platform.mPhotoPreviewSize[index])}`)
45    return platform.mPhotoPreviewSize[index]
46  }
47
48  public static getImageSize(platform: CameraPlatformCapability, cameraId: string, aspectRatio: Resource) {
49    const index = AspectRatio.getIndex(aspectRatio)
50    Log.info(`${this.TAG} getImageSize size = ${JSON.stringify(platform.mImageSize[index])}`)
51    return platform.mImageSize[index]
52  }
53
54  public static convertToResource(aspectRatio: string): Resource {
55    switch (aspectRatio) {
56    case AspectRatio.ASPECT_RATIO_4_3:
57      return AspectRatio.RESOURCE_RATIO_4_3
58    case AspectRatio.ASPECT_RATIO_1_1:
59      return AspectRatio.RESOURCE_RATIO_1_1
60    case AspectRatio.ASPECT_RATIO_16_9:
61      return AspectRatio.RESOURCE_RATIO_16_9
62    default:
63      return AspectRatio.DEFAULT_VALUE
64    }
65  }
66
67  public static convertToString(res: Resource): string {
68    if (res.id === AspectRatio.RESOURCE_RATIO_4_3.id) {
69      return AspectRatio.ASPECT_RATIO_4_3
70    } else if (res.id == AspectRatio.RESOURCE_RATIO_1_1.id) {
71      return AspectRatio.ASPECT_RATIO_1_1
72    } else if (res.id == AspectRatio.RESOURCE_RATIO_16_9.id) {
73      return AspectRatio.ASPECT_RATIO_16_9
74    }
75  }
76}