• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Image Encoding (ArkTS)
2
3Image 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.
4
5## How to Develop
6
7Read [Image API Reference](../reference/apis-image-kit/js-apis-image.md#imagepacker) for APIs related to image encoding.
8
9### Encoding Images into File Streams
10
111. Create an **ImagePacker** object.
12
13   ```ts
14   // Import the required module.
15   import image from '@ohos.multimedia.image';
16
17   const imagePackerApi = image.createImagePacker();
18   ```
19
202. Set the encoding output stream and encoding parameters.
21
22   **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.
23
24   ```ts
25   let packOpts : image.PackingOption = { format:"image/jpeg", quality:98 };
26   ```
27
283. [Create a PixelMap object or an ImageSource object](image-decoding.md).
29
304. Encode the image and save the encoded image.
31
32   Method 1: Use **PixelMap** for encoding.
33
34   ```ts
35   import {BusinessError} from '@ohos.base'
36   imagePackerApi.packing(pixelMap, packOpts).then( (data : ArrayBuffer) => {
37     // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
38   }).catch((error : BusinessError) => {
39     console.error('Failed to pack the image. And the error is: ' + error);
40   })
41   ```
42
43   Method 2: Use **ImageSource** for encoding.
44
45   ```ts
46   import {BusinessError} from '@ohos.base'
47   imagePackerApi.packing(imageSource, packOpts).then( (data : ArrayBuffer) => {
48       // data is the file stream obtained after packing. You can write the file and save it to obtain an image.
49   }).catch((error : BusinessError) => {
50     console.error('Failed to pack the image. And the error is: ' + error);
51   })
52   ```
53
54### Encoding Images into Files
55
56During encoding, you can pass in a file path so that the encoded memory data is directly written to the file.
57
58Method 1: Use **PixelMap** to encode the image and pack it into a file.
59
60   ```ts
61   import {BusinessError} from '@ohos.base'
62   import fs from '@ohos.file.fs'
63   const context : Context = getContext(this);
64   const path : string = context.cacheDir + "/pixel_map.jpg";
65   let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
66   imagePackerApi.packToFile(pixelMap, file.fd, packOpts).then(() => {
67       // Pack the image into the file.
68   }).catch((error : BusinessError) => {
69     console.error('Failed to pack the image. And the error is: ' + error);
70   })
71   ```
72
73Method 2: Use **ImageSource** to encode the image and pack it into a file.
74
75   ```ts
76   import {BusinessError} from '@ohos.base'
77   import fs from '@ohos.file.fs'
78   const context : Context = getContext(this);
79   const filePath : string = context.cacheDir + "/image_source.jpg";
80   let file = fs.openSync(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
81   imagePackerApi.packToFile(imageSource, file.fd, packOpts).then(() => {
82       // Pack the image into the file.
83   }).catch((error : BusinessError) => {
84     console.error('Failed to pack the image. And the error is: ' + error);
85   })
86   ```
87