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