1# Using Image to Encode Images
2
3You can call the native APIs provided by the **ImagePacker** module to encode images, that is, to compress a **PixelMap** object into an image in the desired format.
4
5Currently, images can be encoded only into the JPEG, WebP, PNG, or HEIF format (depending on the hardware).
6
7**Usage Scenario**
8
9- Image codec conversion
10
11  Import an image source, and encapsulate it into the desired format.
12- Image editing
13
14  Edit a **PixelMap** object, and export an image in the desired format.
15
16## How to Develop
17
18Read [ImagePacker](../../reference/apis-image-kit/image__packer__mdk_8h.md) for the API reference.
19
20Refer to the code snippet below to complete the entire image encoding process, including creating an encoder, initializing resources, performing encoding, and destroying the encoder.
21
22During application development, you must call the APIs in the defined sequence. Otherwise, an exception or undefined behavior may occur.
23
24The figure below shows the call relationship of image encoding.
25
26![Call relationship of image encoding](figures/image-encode-native.png)
27
28### Linking the Dynamic Library in the CMake Script
29
30``` cmake
31target_link_libraries(sample PUBLIC libimage_packer_ndk.z.so)
32```
33
34### How to Develop
35
361. Add the header file **image_packer_mdk.h**.
37
38   ```cpp
39   // Add the header file image_packer_mdk.h.
40   #include "multimedia/image_framework/image_packer_mdk.h"
41   ```
42
432. Create an encoder instance.
44
45   You must use napi_env to create an encoder.
46
47   ```cpp
48   // Use napi_value to undertake the created encoder instance.
49   napi_value packer;
50   // Use napi_env to create an encoder. If result is IMAGE_RESULT_SUCCESS, the encoder is created.
51   int32_t result = OH_ImagePacker_Create(env, &packer);
52   ```
53
543. Initialize resources.
55
56   Call **OH_ImagePacker_InitNative** to initialize the native encoder instance.
57
58   ```cpp
59   // Initialize the native instance through napi_env and the created encoder instance.
60   ImagePacker_Native* nativePacker = OH_ImagePacker_InitNative(env, packer);
61   ```
62
634. Perform encoding.
64
65   The following input parameters are provided for the encoding APIs:
66
67   - **ImagePacker_Native** instance obtained
68
69   - Image source (napi_value), **PixelMap** object, or **ImageSource** object (when **CreatePixelMap** is not called yet) to be encoded
70
71   - Encoding parameters, including the encoding format and encoding quality
72
73      > **NOTE**
74      >
75      > According to the MIME protocol, the standard encoding format is image/jpeg. When the APIs provided by the image module are used for encoding, **format** of the encoding parameters 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.
76
77   The encoding APIs can output data to the buffer (memory) or a file. They have the same input parameters, as described previously. You can select either of them as required.
78
79   Example: output data to the buffer (memory)
80
81   ```cpp
82   // Encoding parameters
83   struct ImagePacker_Opts_ opts;
84   // (Mandatory) Configure the encoding format.
85   opts.format = "image/jpeg";
86   // (Mandatory) Configure the encoding quality.
87   opts.quality = 100;
88   // Set the output buffer size, for example, to 4 KB.
89   size_t bufferSize = 4*1024;
90   // Apply for a buffer for image encoding.
91   uint8_t* outData = (uint8_t *)(malloc(bufferSize));
92   // Start to encode the input source. If IMAGE_RESULT_SUCCESS is returned, the encoding is successful. In this case, bufferSize indicates the size of the buffer used for encoding.
93   int32_t result = OH_ImagePacker_PackToData(nativePacker, source, &opts, outData, &bufferSize);
94   ```
95
96   Example: output data to a file
97
98   ```cpp
99   // Encoding parameters
100   struct ImagePacker_Opts_ opts;
101   // (Mandatory) Configure the encoding format.
102   opts.format = "image/jpeg";
103   // (Mandatory) Configure the encoding quality.
104   opts.quality = 100;
105   // Open the file to which the data will be written. (Ensure that the application has the permission to access the file path.)
106   int fd = open("/data/test.jpg", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
107   if (fd >= 0) {
108      // Start to encode the input source. If IMAGE_RESULT_SUCCESS is returned, the encoding is successful.
109      int32_t result = OH_ImagePacker_PackToFile(nativePacker, source, &opts, fd);
110      // Close the file.
111      close(fd);
112   }
113   ```
114
1155. Destroy the encoder instance and release resources.
116
117   > **NOTE**
118   >
119   > You only need to call the API once.
120
121   ```c++
122   // Call OH_ImagePacker_Release to destroy the encoder.
123   int32_t ret = OH_ImagePacker_Release(nativePacker);
124   if (result != IMAGE_RESULT_SUCCESS) {
125       // Exception handling.
126   } else {
127       nativePacker = NULL; // The encoder cannot be destroyed repeatedly.
128   }
129   ```
130