• 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 image from '@ohos.multimedia.image'
16import mediaLibrary from '@ohos.multimedia.mediaLibrary'
17import Logger from './Logger'
18import PictureItem from '../model/PictureItem'
19
20const TAG = '[ImageModel]'
21const SPLIT_COUNT: number = 3 // 图片横竖切割的份数
22export default class ImageModel {
23  private media: mediaLibrary.MediaLibrary = undefined
24
25  constructor(context: any) {
26    this.media = mediaLibrary.getMediaLibrary(context)
27  }
28
29  async getAllImg() {
30    let fileKeyObj = mediaLibrary.FileKey
31    let fetchOp = {
32      selections: fileKeyObj.MEDIA_TYPE + '=?',
33      selectionArgs: [`${mediaLibrary.MediaType.IMAGE}`],
34    }
35    let mediaList: Array<mediaLibrary.FileAsset> = []
36    const fetchFileResult = await this.media.getFileAssets(fetchOp)
37    Logger.info(TAG, `queryFile getFileAssetsFromType fetchFileResult.count = ${fetchFileResult.getCount()}`)
38    if (fetchFileResult.getCount() > 0) {
39      mediaList = await fetchFileResult.getAllObject()
40    }
41    return mediaList
42  }
43
44  async splitPic(index: number) {
45    let imagePixelMap: PictureItem[] = []
46    let imgDatas: Array<mediaLibrary.FileAsset> = await this.getAllImg()
47    let imagePackerApi = image.createImagePacker()
48    let fd = await imgDatas[index].open('r')
49    let imageSource = image.createImageSource(fd)
50    let imageInfo = await imageSource.getImageInfo()
51    Logger.info(TAG, `sizeImg createImageSource ${JSON.stringify(imageSource)}`)
52    let height = imageInfo.size.height / SPLIT_COUNT
53    for (let i = 0; i < SPLIT_COUNT; i++) {
54      for (let j = 0; j < SPLIT_COUNT; j++) {
55        let picItem
56        if (i === SPLIT_COUNT - 1 && j === SPLIT_COUNT - 1) {
57          picItem = new PictureItem(9, undefined)
58          imagePixelMap.push(picItem)
59        } else {
60          Logger.info(TAG, `sizeImg x = ${imageInfo.size.width / SPLIT_COUNT} y = ${height}`)
61          let decodingOptions: image.DecodingOptions = {
62            desiredRegion: {
63              size: {
64                height: height, width: imageInfo.size.width / SPLIT_COUNT
65              }, x: j * imageInfo.size.width / SPLIT_COUNT, y: i * height
66            }
67          }
68          picItem = await imageSource.createPixelMap(decodingOptions)
69          imagePixelMap.push({
70            index: i * SPLIT_COUNT + j, pixelMap: picItem
71          })
72        }
73      }
74    }
75    imagePackerApi.release()
76    await imgDatas[index].close(fd)
77    return imagePixelMap
78  }
79}