1e41f4b71Sopenharmony_ci# Using Image_NativeModule to Encode Images 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciYou can use the **ImagePacker** class to create and release **ImagePacker** instances. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci### Adding a Link Library 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciOpen the **src/main/cpp/CMakeLists.txt** file of the native project, add **libimage_packer.so** and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci```txt 12e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_packer.so) 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci### Calling the Native APIs 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ciFor details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciImplement the C APIs in **hello.cpp**. Refer to the sample code below. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**Example of Using the Encoding APIs** 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciAfter an **ImagePacker** instance is created and packing parameters are specified, the ImageSource or Pixelmap image source is packed to a file or buffer. 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci> **NOTE** 26e41f4b71Sopenharmony_ci> 27e41f4b71Sopenharmony_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, **image_MimeType** of the packing 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. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci ```c++ 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci #include <linux/kd.h> 32e41f4b71Sopenharmony_ci #include <string> 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci #include <hilog/log.h> 35e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image/image_packer_native.h> 36e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image/pixelmap_native.h> 37e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image/image_source_native.h> 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci #undef LOG_DOMAIN 40e41f4b71Sopenharmony_ci #undef LOG_TAG 41e41f4b71Sopenharmony_ci #define LOG_DOMAIN 0x3200 42e41f4b71Sopenharmony_ci #define LOG_TAG "MY_TAG" 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci Image_ErrorCode packToFileFromImageSourceTest(int fd) 45e41f4b71Sopenharmony_ci { 46e41f4b71Sopenharmony_ci // Create an ImagePacker instance. 47e41f4b71Sopenharmony_ci OH_ImagePackerNative *testPacker = nullptr; 48e41f4b71Sopenharmony_ci Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 49e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 50e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 51e41f4b71Sopenharmony_ci return errCode; 52e41f4b71Sopenharmony_ci } 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ci // Create an ImageSource instance. 55e41f4b71Sopenharmony_ci OH_ImageSourceNative* imageSource = nullptr; 56e41f4b71Sopenharmony_ci errCode = OH_ImageSourceNative_CreateFromFd(fd, &imageSource); 57e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 58e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImageSourceNative_CreateFromFd failed, errCode: %{public}d.", errCode); 59e41f4b71Sopenharmony_ci return errCode; 60e41f4b71Sopenharmony_ci } 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci // Encode an ImageSource instance and pack it into a file. 63e41f4b71Sopenharmony_ci OH_PackingOptions *option = nullptr; 64e41f4b71Sopenharmony_ci OH_PackingOptions_Create(&option); 65e41f4b71Sopenharmony_ci char type[] = "image/jpeg"; 66e41f4b71Sopenharmony_ci Image_MimeType image_MimeType = {type, strlen(type)}; 67e41f4b71Sopenharmony_ci OH_PackingOptions_SetMimeType(option, &image_MimeType); 68e41f4b71Sopenharmony_ci // Encode the content as HDR content. (The resource must be HDR resource and the JPEG format must be supported.) 69e41f4b71Sopenharmony_ci OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO); 70e41f4b71Sopenharmony_ci errCode = OH_ImagePackerNative_PackToFileFromImageSource(testPacker, option, imageSource, fd); 71e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 72e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromImageSource failed, errCode: %{public}d.", errCode); 73e41f4b71Sopenharmony_ci return errCode; 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci // Release the ImagePacker instance. 77e41f4b71Sopenharmony_ci errCode = OH_ImagePackerNative_Release(testPacker); 78e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) 79e41f4b71Sopenharmony_ci { 80e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 81e41f4b71Sopenharmony_ci return errCode; 82e41f4b71Sopenharmony_ci } 83e41f4b71Sopenharmony_ci // Release the ImageSource instance. 84e41f4b71Sopenharmony_ci errCode = OH_ImageSourceNative_Release(imageSource); 85e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) 86e41f4b71Sopenharmony_ci { 87e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImageSourceNative_Release failed, errCode: %{public}d.", errCode); 88e41f4b71Sopenharmony_ci return errCode; 89e41f4b71Sopenharmony_ci } 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci return IMAGE_SUCCESS; 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci Image_ErrorCode packToFileFromPixelmapTest(uint8_t *buffer, size_t buffSize, int fd) 95e41f4b71Sopenharmony_ci { 96e41f4b71Sopenharmony_ci // Create an ImagePacker instance. 97e41f4b71Sopenharmony_ci OH_ImagePackerNative *testPacker = nullptr; 98e41f4b71Sopenharmony_ci Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 99e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 100e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 101e41f4b71Sopenharmony_ci return errCode; 102e41f4b71Sopenharmony_ci } 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci // Create a Pixelmap instance. 105e41f4b71Sopenharmony_ci OH_Pixelmap_InitializationOptions *createOpts; 106e41f4b71Sopenharmony_ci OH_PixelmapInitializationOptions_Create(&createOpts); 107e41f4b71Sopenharmony_ci OH_PixelmapInitializationOptions_SetWidth(createOpts, 6); 108e41f4b71Sopenharmony_ci OH_PixelmapInitializationOptions_SetHeight(createOpts, 4); 109e41f4b71Sopenharmony_ci OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, 3); 110e41f4b71Sopenharmony_ci OH_PixelmapInitializationOptions_SetAlphaType(createOpts, 0); 111e41f4b71Sopenharmony_ci OH_PixelmapNative *pixelmap = nullptr; 112e41f4b71Sopenharmony_ci errCode = OH_PixelmapNative_CreatePixelmap(buffer, bufferSize, createOpts, &pixelmap); 113e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 114e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_PixelmapNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 115e41f4b71Sopenharmony_ci return errCode; 116e41f4b71Sopenharmony_ci } 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci // Encode a Pixelmap instance and pack it into a file. 119e41f4b71Sopenharmony_ci OH_PackingOptions *option = nullptr; 120e41f4b71Sopenharmony_ci OH_PackingOptions_Create(&option); 121e41f4b71Sopenharmony_ci char type[] = "image/jpeg"; 122e41f4b71Sopenharmony_ci Image_MimeType image_MimeType = {type, strlen(type)}; 123e41f4b71Sopenharmony_ci OH_PackingOptions_SetMimeType(option, &image_MimeType); 124e41f4b71Sopenharmony_ci errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd); 125e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) { 126e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromPixelmap failed, errCode: %{public}d.", errCode); 127e41f4b71Sopenharmony_ci return errCode; 128e41f4b71Sopenharmony_ci } 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci // Release the ImagePacker instance. 131e41f4b71Sopenharmony_ci errCode = OH_ImagePackerNative_Release(testPacker); 132e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) 133e41f4b71Sopenharmony_ci { 134e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 135e41f4b71Sopenharmony_ci return errCode; 136e41f4b71Sopenharmony_ci } 137e41f4b71Sopenharmony_ci 138e41f4b71Sopenharmony_ci // Release the Pixelmap instance. 139e41f4b71Sopenharmony_ci errCode = OH_PixelmapNative_Release(pixelmap); 140e41f4b71Sopenharmony_ci if (errCode != IMAGE_SUCCESS) 141e41f4b71Sopenharmony_ci { 142e41f4b71Sopenharmony_ci OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_PixelmapNative_Release failed, errCode: %{public}d.", errCode); 143e41f4b71Sopenharmony_ci return errCode; 144e41f4b71Sopenharmony_ci } 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci return IMAGE_SUCCESS; 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci ``` 149