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
16import type { CameraPlatformCapability } from '../../camera/CameraPlatformCapability';
17import { Log } from '../../utils/Log';
18
19const TAG = '[Resolution]:';
20
21export default class Resolution {
22  public static readonly ALIAS = 'VideoResolution';
23  public static readonly RESOLUTION_16_9_720P = '[16:9] 720p';
24  public static readonly RESOLUTION_16_9_1080P = '[16:9] 1080p';
25  public static readonly DEFAULT_VALUE = $r('app.string.resolution_1280_720');
26  public static readonly RESOURCE_16_9_720P = Resolution.DEFAULT_VALUE;
27  public static readonly RESOURCE_16_9_1080P = $r('app.string.resolution_1620_1080');
28
29  public static getVideoPreviewSize(platform: CameraPlatformCapability, cameraId: string, resolution: Resource) {
30    const index = Resolution.getIndex(resolution);
31    Log.info(`${TAG} getVideoPreviewSize size = ${JSON.stringify(platform.mVideoPreviewSize[index])}`);
32    return platform.mVideoPreviewSize[index];
33  }
34
35  public static getVideoFrameSize(platform: CameraPlatformCapability, cameraId: string, resolution: Resource) {
36    const index = Resolution.getIndex(resolution);
37    Log.info(`${TAG} getVideoFrameSize size = ${JSON.stringify(platform.mVideoFrameSize[index])}`);
38    return platform.mVideoFrameSize[index];
39  }
40
41  public static convertToResource(resolution: string): Resource {
42    switch (resolution) {
43      case Resolution.RESOLUTION_16_9_720P:
44        return Resolution.RESOURCE_16_9_720P;
45      case Resolution.RESOLUTION_16_9_1080P:
46        return Resolution.RESOURCE_16_9_1080P;
47      default:
48        return Resolution.RESOURCE_16_9_720P;
49    }
50  }
51
52  public static convertToString(res: Resource): string {
53    if (res.id === Resolution.RESOURCE_16_9_720P.id) {
54      return Resolution.RESOLUTION_16_9_720P;
55    } else if (res.id === Resolution.RESOURCE_16_9_1080P.id) {
56      return Resolution.RESOLUTION_16_9_1080P;
57    }
58  }
59
60  private static getIndex(resolution: Resource): number {
61    if (resolution.id === Resolution.RESOURCE_16_9_720P.id) {
62      return 0;
63    } else if (resolution.id === Resolution.RESOURCE_16_9_1080P.id) {
64      return 1;
65    }
66    return 0;
67  }
68}