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 = '[AspectRatio]:'; 20 21export default class 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 public static getPhotoPreviewSize(platform: CameraPlatformCapability, cameraId: string, aspectRatio: Resource) { 32 const index = AspectRatio.getIndex(aspectRatio); 33 Log.info(`${TAG} getPhotoPreviewSize size = ${JSON.stringify(platform.mPhotoPreviewSize[index])}`); 34 return platform.mPhotoPreviewSize[index]; 35 } 36 37 public static getImageSize(platform: CameraPlatformCapability, cameraId: string, aspectRatio: Resource) { 38 const index = AspectRatio.getIndex(aspectRatio); 39 Log.info(`${TAG} getImageSize size = ${JSON.stringify(platform.mImageSize[index])}`); 40 return platform.mImageSize[index]; 41 } 42 43 public static convertToResource(aspectRatio: string): Resource { 44 switch (aspectRatio) { 45 case AspectRatio.ASPECT_RATIO_4_3: 46 return AspectRatio.RESOURCE_RATIO_4_3; 47 case AspectRatio.ASPECT_RATIO_1_1: 48 return AspectRatio.RESOURCE_RATIO_1_1; 49 case AspectRatio.ASPECT_RATIO_16_9: 50 return AspectRatio.RESOURCE_RATIO_16_9; 51 default: 52 return AspectRatio.DEFAULT_VALUE; 53 } 54 } 55 56 public static convertToString(res: Resource): string { 57 if (res.id === AspectRatio.RESOURCE_RATIO_4_3.id) { 58 return AspectRatio.ASPECT_RATIO_4_3; 59 } else if (res.id == AspectRatio.RESOURCE_RATIO_1_1.id) { 60 return AspectRatio.ASPECT_RATIO_1_1; 61 } else if (res.id == AspectRatio.RESOURCE_RATIO_16_9.id) { 62 return AspectRatio.ASPECT_RATIO_16_9; 63 } 64 } 65 66 private static getIndex(aspectRatio: Resource): number { 67 if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_4_3.id) { 68 return 0; 69 } else if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_1_1.id) { 70 return 1; 71 } else if (aspectRatio.id === AspectRatio.RESOURCE_RATIO_16_9.id) { 72 return 2; 73 } 74 return 0; 75 } 76}