1# 使用ImagePacker完成图片编码 2 3图片编码指将PixelMap编码成不同格式的存档图片,当前支持打包为JPEG、WebP、png和 HEIF(不同硬件设备支持情况不同) 格式,用于后续处理,如保存、传输等。 4 5## 开发步骤 6 7图片编码相关API的详细介绍请参见:[图片编码接口说明](../../reference/apis-image-kit/js-apis-image.md#imagepacker)。 8 9### 图片编码进文件流 10 111. 创建图像编码ImagePacker对象。 12 13 ```ts 14 // 导入相关模块包 15 import { image } from '@kit.ImageKit'; 16 17 const imagePackerApi = image.createImagePacker(); 18 ``` 19 202. 设置编码输出流和编码参数。 21 22 - format为图像的编码格式;quality为图像质量,范围从0-100,100为最佳质量。 23 24 > **说明:** 25 > 根据MIME标准,标准编码格式为image/jpeg。当使用image编码时,PackingOption.format设置为image/jpeg,image编码后的文件扩展名可设为.jpg或.jpeg,可在支持image/jpeg解码的平台上使用。 26 27 ```ts 28 let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 }; 29 ``` 30 31 - 编码为hdr内容(需要资源本身为hdr,支持jpeg格式)。 32 ```ts 33 packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO; 34 ``` 35 363. [创建PixelMap对象或创建ImageSource对象](image-decoding.md)。 37 384. 进行图片编码,并保存编码后的图片。 39 40 方法一:通过PixelMap进行编码。 41 42 ```ts 43 import { BusinessError } from '@kit.BasicServicesKit'; 44 imagePackerApi.packing(pixelMap, packOpts).then( (data : ArrayBuffer) => { 45 // data 为打包获取到的文件流,写入文件保存即可得到一张图片 46 }).catch((error : BusinessError) => { 47 console.error('Failed to pack the image. And the error is: ' + error); 48 }) 49 ``` 50 51 方法二:通过imageSource进行编码。 52 53 ```ts 54 import { BusinessError } from '@kit.BasicServicesKit'; 55 imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => { 56 // data 为打包获取到的文件流,写入文件保存即可得到一张图片 57 }).catch((error : BusinessError) => { 58 console.error('Failed to pack the image. And the error is: ' + error); 59 }) 60 ``` 61 62### 图片编码进文件 63 64在编码时,开发者可以传入对应的文件路径,编码后的内存数据将直接写入文件。 65 66 方法一:通过PixelMap编码进文件。 67 68 ```ts 69 import { BusinessError } from '@kit.BasicServicesKit'; 70 import { fileIo as fs } from '@kit.CoreFileKit'; 71 const context : Context = getContext(this); 72 const path : string = context.cacheDir + "/pixel_map.jpg"; 73 let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 74 imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => { 75 // 直接打包进文件 76 }).catch((error : BusinessError) => { 77 console.error('Failed to pack the image. And the error is: ' + error); 78 }).finally(()=>{ 79 fs.closeSync(file.fd); 80 }) 81 ``` 82 83 方法二:通过imageSource编码进文件。 84 85 ```ts 86 import { BusinessError } from '@kit.BasicServicesKit'; 87 import { fileIo as fs } from '@kit.CoreFileKit'; 88 const context : Context = getContext(this); 89 const filePath : string = context.cacheDir + "/image_source.jpg"; 90 let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); 91 imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => { 92 // 直接打包进文件 93 }).catch((error : BusinessError) => { 94 console.error('Failed to pack the image. And the error is: ' + error); 95 }).finally(()=>{ 96 fs.closeSync(file.fd); 97 }) 98 ``` 99 100### 图片编码保存进图库 101 102可以将图片编码保存到应用沙箱,然后使用媒体文件管理相关接口[保存媒体库资源](../medialibrary/photoAccessHelper-savebutton.md)。 103 104<!--RP1--> 105<!--RP1End-->