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 fileio from '@ohos.fileio' 18import prompt from '@ohos.prompt' 19import DateTimeUtil from '../model/DateTimeUtil' 20import Logger from '../model/Logger' 21 22const TAG: string = '[MediaUtils]' 23 24export default class MediaUtils { 25 private tag: string = 'MediaUtils' 26 private mediaTest: mediaLibrary.MediaLibrary = undefined 27 28 constructor(context: any) { 29 this.mediaTest = mediaLibrary.getMediaLibrary(context) 30 } 31 32 async createAndGetFile() { 33 let info = { 34 prefix: 'IMG_', suffix: '.jpg', directory: mediaLibrary.DirectoryType.DIR_IMAGE 35 } 36 let dateTimeUtil = new DateTimeUtil() 37 let name = `${dateTimeUtil.getDate()}_${dateTimeUtil.getTime()}` 38 let displayName = `${info.prefix}${name}${info.suffix}` 39 Logger.info(this.tag, `displayName = ${displayName}`) 40 let publicPath = await this.mediaTest.getPublicDirectory(info.directory) 41 return await this.mediaTest.createAsset(mediaLibrary.MediaType.IMAGE, displayName, publicPath) 42 } 43 44 async queryFile(uri: string) { 45 let fetchOp = { 46 selections: '', 47 selectionArgs: [], 48 uri: uri 49 } 50 51 let fileAssetsResult = await this.mediaTest.getFileAssets(fetchOp) 52 Logger.info(TAG, `queryFile fetchFileResult.getCount() = ${fileAssetsResult.getCount()}`) 53 return await fileAssetsResult.getFirstObject() 54 } 55 56 async getPixelMap(uri: string) { 57 Logger.info(this.tag, `getPixelMap, uri = ${uri}`) 58 let file = await this.queryFile(uri) 59 if (file.mediaType === mediaLibrary.MediaType.IMAGE) { 60 let fd = await file.open('r') 61 Logger.info(this.tag, `getPixelMap, fd = ${fd}`) 62 let imageResource = await image.createImageSource(fd) 63 let pixelMap = await imageResource.createPixelMap() 64 await imageResource.release() 65 await file.close(fd) 66 return pixelMap 67 } 68 return null 69 } 70 71 async savePicture(data: image.PixelMap) { 72 Logger.info(TAG, `savePicture`) 73 let packOpts: image.PackingOption = { 74 format: "image/jpeg", quality: 100 75 } 76 let imagePackerApi = image.createImagePacker() 77 let arrayBuffer = await imagePackerApi.packing(data, packOpts) 78 let fileAsset = await this.createAndGetFile() 79 let fd = await fileAsset.open('Rw') 80 imagePackerApi.release() 81 await fileio.write(fd, arrayBuffer) 82 await fileAsset.close(fd) 83 Logger.info(TAG, `write done`) 84 prompt.showToast({ 85 message: '图片保存成功', duration: 1000 86 }) 87 } 88}