1/* 2 * Copyright (c) 2024 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 16// 导入相关模块包 17import { image } from '@kit.ImageKit'; 18import { BusinessError } from '@kit.BasicServicesKit'; 19import { fileIo as fs } from '@kit.CoreFileKit'; 20import Logger from './Logger'; 21 22const context: Context = getContext(this); 23const imagePackerApi = image.createImagePacker(); 24 25/** 26 * 图片编码,通过PixelMap将图片打包成文件流,再将文件流写入文件保存。 27 * 28 * @param pixelMap - 位图操作对象。 29 * @param isHdr - 是否为HDR格式。 30 **/ 31export async function encodeToStreamByPixelMap(pixelMap: image.PixelMap, isHdr:boolean) { 32 const path: string = context.cacheDir + '/pixel_map.jpg'; 33 let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 34 let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 }; 35 if(isHdr == true) { 36 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 37 } 38 imagePackerApi.packing(pixelMap, packOpts).then((data: ArrayBuffer) => { 39 // data 为打包获取到的文件流,写入文件保存即可得到一张图片 40 fs.write(file.fd, data); 41 Logger.info('Succeeded to pack the image and write to the file.'); 42 }).catch((error: BusinessError) => { 43 Logger.error('Failed to pack the image. And the error is: ', String(error)); 44 }) 45} 46 47/** 48 * 图片编码,通过imageSource将图片打包成文件流,再将文件流写入文件保存。 49 * 50 * @param pixelMap - 位图操作对象。 51 * @param isHdr - 是否为HDR格式。 52 **/ 53export async function encodeToStreamByImageSource(imageSource:image.ImageSource,isHdr:boolean){ 54 let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 }; 55 if(isHdr == true) { 56 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 57 } 58 const path: string = context.cacheDir + '/pixel_map.jpg'; 59 let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 60 imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => { 61 // data 为打包获取到的文件流,写入文件保存即可得到一张图片 62 fs.write(file.fd, data); 63 Logger.info('Succeeded to pack the image and write to the file.'); 64 }).catch((error : BusinessError) => { 65 Logger.error('Failed to pack the image. And the error is: ', String(error)); 66 }) 67} 68 69/** 70 * 图片编码,通过pixelMap将图片直接打包成文件保存。 71 * 72 * @param pixelMap - 位图操作对象。 73 * @param isHdr - 是否为HDR格式。 74 **/ 75export async function encodeToFileByPixelMap(pixelMap: image.PixelMap,isHdr:boolean) { 76 let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 }; 77 if(isHdr == true) { 78 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 79 } 80 const path : string = context.cacheDir + '/pixel_map.jpg'; 81 let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 82 await imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => { 83 // 直接打包进文件 84 Logger.info('Succeeded to pack the image and write to the file.'); 85 }).catch((error : BusinessError) => { 86 Logger.error('Failed to pack the image. And the error is: ', String(error)); 87 }) 88} 89 90/** 91 * 图片编码,通过imageSource将图片直接打包成文件保存。 92 * 93 * @param pixelMap - 位图操作对象。 94 * @param isHdr - 是否为HDR格式。 95 **/ 96export async function encodeToFileByImageSource(imageSource:image.ImageSource,isHdr:boolean) { 97 let packOpts: image.PackingOption = { format: 'image/jpeg', quality: 98 }; 98 if(isHdr == true) { 99 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 100 } 101 const filePath : string = context.cacheDir + '/image_source.jpg'; 102 let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 103 imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => { 104 // 直接打包进文件 105 Logger.info('Succeeded to pack the image and write to the file.'); 106 }).catch((error : BusinessError) => { 107 Logger.error('Failed to pack the image. And the error is: ', String(error)); 108 }) 109}