• 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 */
15import screenManager from '@ohos/base/src/main/ets/manager/ScreenManager';
16import { computeSampleSize } from '@ohos/base/src/main/ets/utils/ImageUtil';
17import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem';
18import { MediaConstants } from '@ohos/base/src/main/ets/constants/MediaConstants';
19
20export async function getThumbnail(mediaItem: MediaDataItem, isCurrent: boolean): Promise<string> {
21    await mediaItem.load(true);
22    let imgWidth = mediaItem.width;
23    let imgHeight = mediaItem.height;
24    let scale = generateSampleSize(imgWidth, imgHeight, isCurrent);
25    mediaItem.imgWidth = Math.ceil(mediaItem.width / scale);
26    mediaItem.imgHeight = Math.ceil(mediaItem.height / scale);
27    imgWidth = Math.ceil(imgWidth / scale);
28    imgHeight = Math.ceil(imgHeight / scale);
29    return mediaItem.getThumbnail(imgWidth, imgHeight);
30}
31
32function generateSampleSize(imageWidth: number, imageHeight: number, isCurrent: boolean): number {
33    let width = vp2px(screenManager.getWinWidth());
34    let height = vp2px(screenManager.getWinHeight());
35    width = width == 0 ? screenManager.DEFAULT_WIDTH : width;
36    height = height == 0 ? screenManager.DEFAULT_HEIGHT : height;
37    let maxNumOfPixels;
38    if (isCurrent) {
39        maxNumOfPixels = 2 * width * height;
40    } else {
41        maxNumOfPixels = width * height;
42    }
43    let minSide = Math.min(width, height);
44    return computeSampleSize(imageWidth, imageHeight, minSide, maxNumOfPixels);
45}