• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { Log } from '../utils/Log';
17import deviceInfo from '@ohos.deviceInfo';
18
19export default class DisplayCalculator {
20  private static TAG = '[DisplayCalculator]:'
21
22  public static calcSurfaceDisplaySize(screenWidth: number, screenHeight: number, previewWidth: number,
23    previewHeight: number) {
24    const displaySize = {
25      width: 1920, height: 1080
26    }
27    const ratio = previewWidth / previewHeight
28    if (deviceInfo.deviceType == 'tablet' || screenWidth > screenHeight) {
29      if (screenWidth / screenHeight > ratio) {
30        displaySize.width = Math.floor(screenHeight * ratio)
31        displaySize.height = Math.floor(screenHeight)
32      } else {
33        displaySize.width = Math.floor(screenWidth)
34        displaySize.height = Math.floor(screenWidth / ratio)
35      }
36    } else {
37      if (screenWidth / screenHeight > ratio) {
38        displaySize.width = Math.floor(screenHeight / ratio)
39        displaySize.height = Math.floor(screenHeight)
40      } else {
41        displaySize.width = Math.floor(screenWidth)
42        displaySize.height = Math.floor(screenWidth * ratio)
43      }
44    }
45    Log.info(`${this.TAG} calcSurfaceDisplaySize screenWidth=${screenWidth} screenHeight=${screenHeight} `)
46    Log.info(`${this.TAG} calcSurfaceDisplaySize previewWidth=${previewWidth} previewHeight=${previewHeight} displaySize= ${JSON.stringify(displaySize)}`)
47    return displaySize
48  }
49}