1e41f4b71Sopenharmony_ci# Using ImagePacker to Encode Images
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciImage encoding refers to the process of encoding a pixel map into an archived image in different formats (only in JPEG, WebP, and PNG currently) for subsequent processing, such as storage and transmission.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## How to Develop
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciRead [Image API Reference](../../reference/apis-image-kit/js-apis-image.md#imagepacker) for APIs related to image encoding.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Encoding Images into File Streams
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci1. Create an **ImagePacker** object.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci   ```ts
14e41f4b71Sopenharmony_ci   // Import the required module.
15e41f4b71Sopenharmony_ci   import { image } from '@kit.ImageKit';
16e41f4b71Sopenharmony_ci   
17e41f4b71Sopenharmony_ci   const imagePackerApi = image.createImagePacker();
18e41f4b71Sopenharmony_ci   ```
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ci2. Set the encoding output stream and encoding parameters.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci-  **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.
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci      > **NOTE**
25e41f4b71Sopenharmony_ci      >
26e41f4b71Sopenharmony_ci      > 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.
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ci      ```ts
29e41f4b71Sopenharmony_ci      let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
30e41f4b71Sopenharmony_ci      ```
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci-  Encode the content as HDR content. (The resource must be HDR resource and the JPEG format must be supported.)
33e41f4b71Sopenharmony_ci   
34e41f4b71Sopenharmony_ci   ```ts
35e41f4b71Sopenharmony_ci   packOpts.desiredDynamicRange = image.PackingDynamicRange.AUTO;
36e41f4b71Sopenharmony_ci   ```
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci3. [Create a PixelMap object or an ImageSource object](image-decoding.md).
39e41f4b71Sopenharmony_ci
40e41f4b71Sopenharmony_ci4. Encode the image and save the encoded image.
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci-  Method 1: Use **PixelMap** for encoding.
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci   ```ts
45e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
46e41f4b71Sopenharmony_ci   imagePackerApi.packing(pixelMap, packOpts).then( (data : ArrayBuffer) => {
47e41f4b71Sopenharmony_ci     // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
48e41f4b71Sopenharmony_ci   }).catch((error : BusinessError) => { 
49e41f4b71Sopenharmony_ci     console.error('Failed to pack the image. And the error is: ' + error); 
50e41f4b71Sopenharmony_ci   })
51e41f4b71Sopenharmony_ci   ```
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci-  Method 2: Use **ImageSource** for encoding.
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci   ```ts
56e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
57e41f4b71Sopenharmony_ci   imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => {
58e41f4b71Sopenharmony_ci       // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
59e41f4b71Sopenharmony_ci   }).catch((error : BusinessError) => { 
60e41f4b71Sopenharmony_ci     console.error('Failed to pack the image. And the error is: ' + error); 
61e41f4b71Sopenharmony_ci   })
62e41f4b71Sopenharmony_ci   ```
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci### Encoding Images into Files
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ciDuring encoding, you can pass in a file path so that the encoded memory data is directly written to the file.
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci-  Method 1: Use **PixelMap** to encode the image and pack it into a file.
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci   ```ts
71e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
72e41f4b71Sopenharmony_ci   import { fileIo } from '@kit.CoreFileKit';
73e41f4b71Sopenharmony_ci   const context : Context = getContext(this);
74e41f4b71Sopenharmony_ci   const path : string = context.cacheDir + "/pixel_map.jpg";
75e41f4b71Sopenharmony_ci   let file = fileIo.openSync(path, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
76e41f4b71Sopenharmony_ci   imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {
77e41f4b71Sopenharmony_ci       // Pack the image into the file.
78e41f4b71Sopenharmony_ci   }).catch((error : BusinessError) => { 
79e41f4b71Sopenharmony_ci     console.error('Failed to pack the image. And the error is: ' + error); 
80e41f4b71Sopenharmony_ci   })
81e41f4b71Sopenharmony_ci   ```
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci-  Method 2: Use **ImageSource** to encode the image and pack it into a file.
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci   ```ts
86e41f4b71Sopenharmony_ci   import { BusinessError } from '@kit.BasicServicesKit';
87e41f4b71Sopenharmony_ci   import { fileIo } from '@kit.CoreFileKit';
88e41f4b71Sopenharmony_ci   const context : Context = getContext(this);
89e41f4b71Sopenharmony_ci   const filePath : string = context.cacheDir + "/image_source.jpg";
90e41f4b71Sopenharmony_ci   let file = fileIo.openSync(filePath, fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE);
91e41f4b71Sopenharmony_ci   imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => {
92e41f4b71Sopenharmony_ci       // Pack the image into the file.
93e41f4b71Sopenharmony_ci   }).catch((error : BusinessError) => { 
94e41f4b71Sopenharmony_ci     console.error('Failed to pack the image. And the error is: ' + error); 
95e41f4b71Sopenharmony_ci   })
96e41f4b71Sopenharmony_ci   ```
97