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 */ 15 16import image from '@ohos.multimedia.image'; 17import fileIO from '@ohos.fileio'; 18import { Log } from '@ohos/base/src/main/ets/utils/Log'; 19import mediaModel from '@ohos/base/src/main/ets/model/MediaModel'; 20import { MediaDataItem } from '@ohos/base/src/main/ets/data/MediaDataItem'; 21import { ImageFilterStack } from './ImageFilterStack'; 22import { DateUtil } from './utils/DateUtil'; 23import { Loader } from './Loader'; 24import { PhotoEditorManager } from './PhotoEditorManager'; 25 26const TAG = "Save" 27 28export class Save { 29 private static readonly QUALITY_100: number = 100; 30 31 constructor() { 32 } 33 34 public static async save(item: MediaDataItem, optStack: ImageFilterStack, isReplace: Boolean): Promise<number> { 35 Log.info(TAG, `${JSON.stringify(item)} ${isReplace}`); 36 try { 37 let wrapper = await Loader.loadPixelMapWrapper(item); 38 wrapper = optStack.apply(wrapper); 39 Log.debug(TAG, 'Edit and restore operation execution end.'); 40 41 let options = { 42 format: 'image/jpeg', 43 quality: Save.QUALITY_100 44 }; 45 let packer = image.createImagePacker(); 46 let pixelMap = wrapper.pixelMap as image.PixelMap; 47 let buffer = await packer.packing(pixelMap, options); 48 Log.info(TAG, 'Format pixelMap data to jpg data end.'); 49 50 let fileAsset = await this.createFileAsset(item, isReplace); 51 Log.info(TAG, `create fileAsset succeed`); 52 let fd = await mediaModel.openAsset('RW', fileAsset); 53 if (fd < 0) { 54 Log.warn(TAG, 'open asset failed.'); 55 return -1; 56 } 57 await fileIO.write(fd, buffer); 58 PhotoEditorManager.getInstance().isSaving = false; 59 Log.info(TAG, 'write jpg file end.'); 60 let newId = fileAsset.id; 61 await mediaModel.closeAsset(fd, fileAsset); 62 63 await packer.release(); 64 wrapper && wrapper.release(); 65 Log.info(TAG, `end`); 66 return newId 67 } catch (e) { 68 Log.error(TAG, `save catch error: ${JSON.stringify(e)}`); 69 return -1 70 } 71 } 72 73 private static async createFileAsset(item: MediaDataItem, isReplace: Boolean) { 74 let fileAsset =await item.loadFileAsset(); 75 76 if (!fileAsset) { 77 Log.warn(TAG, 'get file error'); 78 return null; 79 } 80 let title = DateUtil.nameByDate(isReplace, fileAsset.displayName); 81 if (null == title) { 82 Log.warn(TAG, `create picture name failed.`); 83 return null; 84 } 85 let displayName = title + '.jpg'; 86 Log.debug(TAG, `file displayname = ${displayName}, file path = ${fileAsset.relativePath}`); 87 let favorite = false; 88 if (isReplace) { 89 favorite = await fileAsset.isFavorite(); 90 await item.onDelete(); 91 Log.debug(TAG, `trash picture file id ${item.id} end.`); 92 } 93 fileAsset = await mediaModel.createOne(fileAsset.mediaType, displayName, fileAsset.relativePath); 94 if (favorite) { 95 await fileAsset.favorite(favorite); 96 } 97 return fileAsset; 98 } 99}