1# Using ImagePacker to Encode Pictures 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--SE: @liyang_bryan--> 6<!--TSE: @xchaosioda--> 7 8Image encoding refers to the process of encoding a picture into an image in different formats (only in JPEG and HEIF currently) for subsequent processing, such as storage and transmission. 9 10## How to Develop 11 12Read the [API reference](../../reference/apis-image-kit/arkts-apis-image-ImagePacker.md) for APIs related to image encoding. 13 14### Encoding Images into File Streams 15 161. Create an ImagePacker object. 17 18 ```ts 19 // Import the required module. 20 import { image } from '@kit.ImageKit'; 21 22 const imagePackerApi = image.createImagePacker(); 23 ``` 24 252. Set the encoding output stream and encoding parameters. 26 27 **format** indicates the image encoding format, and **quality** indicates the image quality. The value ranges from 0 to 100, and the value 100 indicates the optimal quality. 28 29 > **NOTE** 30 > 31 > According to the MIME protocol, the standard encoding format is image/jpeg. When the APIs provided by the image module are used for encoding, **PackingOption.format** must be set to **image/jpeg**. The file name extension of the encoded image file can be .jpg or .jpeg, and the file can be used on platforms that support image/jpeg decoding. 32 33 ```ts 34 let packOpts: image.PackingOption = { 35 format: "image/jpeg", 36 quality: 98, 37 bufferSize: 10, 38 desiredDynamicRange: image.PackingDynamicRange.AUTO, 39 needsPackProperties: true 40 }; 41 ``` 42 433. Encode the image and save the encoded image. Before encoding, you need to obtain a Picture object through decoding. For details, see [Using ImageSource to Decode Pictures](./image-picture-decoding.md). 44 45 ```ts 46 import { BusinessError } from '@kit.BasicServicesKit'; 47 // Ensure that the Picture object is obtained before calling the following encoding APIs. 48 imagePackerApi.packing(picture, packOpts).then( (data : ArrayBuffer) => { 49 console.info('Succeeded in packing the image.'+ data); 50 }).catch((error : BusinessError) => { 51 console.error('Failed to pack the image. And the error is: ' + error); 52 }) 53 ``` 54 55### Encoding Images into Files 56 57During encoding, you can pass in a file path so that the encoded memory data is directly written to the file. Before encoding, you need to obtain a Picture object through decoding. For details, see [Using ImageSource to Decode Pictures](./image-picture-decoding.md). 58 59 ```ts 60 import { common } from '@kit.AbilityKit'; 61 62 // Obtain the context from the component and ensure that the return value of this.getUIContext().getHostContext() is UIAbilityContext. 63 const context = this.getUIContext().getHostContext() as common.UIAbilityContext; 64 const path : string = context.cacheDir + "/picture.jpg"; 65 let file = fileIo.openSync(path, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE); 66 // Ensure that the Picture object is obtained before calling the following encoding APIs. 67 imagePackerApi.packToFile(picture, file.fd, packOpts).then(() => { 68 console.info('Succeeded in packing the image to file.'); 69 }).catch((error : BusinessError) => { 70 console.error('Failed to pack the image. And the error is: ' + error); 71 }) 72 ``` 73