1# Using Image_NativeModule to Encode Images 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--SE: @liyang_bryan--> 6<!--TSE: @xchaosioda--> 7 8You can use the **ImagePacker** class to create and release ImagePacker instances. 9 10## How to Develop 11 12### Adding a Link Library 13 14Open 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. 15 16```txt 17target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so libimage_packer.so libpixelmap.so) 18``` 19 20### Calling the Native APIs 21 22For details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/capi-image-nativemodule.md). 23 24Create a native C++ application in DevEco Studio. The project created by default contains the **index.ets** file, and a **hello.cpp** or **napi_init.cpp** file is generated in the **entry\src\main\cpp** directory. In this example, the generated file is **hello.cpp**. Implement the C APIs in **hello.cpp**. Refer to the sample code below. 25 26**Example of Using the Encoding APIs** 27 28After an ImagePacker instance is created and encoding parameters are specified, the ImageSource or Pixelmap object is encoded to a file or buffer. 29 30> **NOTE** 31> 32> 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 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. 33 34```c++ 35#include <linux/kd.h> 36#include <string> 37#include <set> 38#include <hilog/log.h> 39#include <multimedia/image_framework/image/image_common.h> 40#include <multimedia/image_framework/image/image_packer_native.h> 41#include <multimedia/image_framework/image/pixelmap_native.h> 42#include <multimedia/image_framework/image/image_source_native.h> 43 44#undef LOG_DOMAIN 45#undef LOG_TAG 46#define LOG_DOMAIN 0x3200 47#define LOG_TAG "MY_TAG" 48 49static std::set<std::string> g_encodeSupportedFormats; 50 51Image_MimeType GetMimeTypeIfEncodable(const char *format) 52{ 53 auto it = g_encodeSupportedFormats.find(format); 54 if (it == g_encodeSupportedFormats.end()) { 55 return {"", 0}; 56 } 57 return {const_cast<char *>(format), strlen(format)}; 58} 59 60Image_ErrorCode packToFileFromImageSourceTest(int fd) 61{ 62 // Create an ImagePacker instance. 63 OH_ImagePackerNative *testPacker = nullptr; 64 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 65 if (errCode != IMAGE_SUCCESS) { 66 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 67 return errCode; 68 } 69 // Obtain the encoding capability range. 70 Image_MimeType* mimeType = nullptr; 71 size_t length = 0; 72 errCode = OH_ImagePackerNative_GetSupportedFormats(&mimeType, &length); 73 if (errCode != IMAGE_SUCCESS) { 74 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_GetSupportedFormats failed, errCode: %{public}d.", errCode); 75 return errCode; 76 } 77 for (size_t count = 0; count < length; count++) { 78 OH_LOG_INFO(LOG_APP, "Encode supportedFormats:%{public}s", mimeType[count].data); 79 if (mimeType[count].data != nullptr) { 80 g_encodeSupportedFormats.insert(std::string(mimeType[count].data)); 81 } 82 } 83 // Create an ImageSource instance. 84 OH_ImageSourceNative* imageSource = nullptr; 85 errCode = OH_ImageSourceNative_CreateFromFd(fd, &imageSource); 86 if (errCode != IMAGE_SUCCESS) { 87 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImageSourceNative_CreateFromFd failed, errCode: %{public}d.", errCode); 88 return errCode; 89 } 90 91 // Specify encoding parameters and encode the ImageSource into a file. 92 OH_PackingOptions *option = nullptr; 93 OH_PackingOptions_Create(&option); 94 Image_MimeType image_MimeType = GetMimeTypeIfEncodable(MIME_TYPE_JPEG); 95 if (image_MimeType.data == nullptr || image_MimeType.size == 0) { 96 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest GetMimeTypeIfEncodable failed, format can't support encode."); 97 return IMAGE_BAD_PARAMETER; 98 } 99 OH_PackingOptions_SetMimeType(option, &image_MimeType); 100 // Encode the content as HDR content. (The resource must be HDR resource and the JPEG format must be supported.) 101 OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO); 102 errCode = OH_ImagePackerNative_PackToFileFromImageSource(testPacker, option, imageSource, fd); 103 if (errCode != IMAGE_SUCCESS) { 104 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromImageSource failed, errCode: %{public}d.", errCode); 105 return errCode; 106 } 107 108 // Release the ImagePacker instance. 109 errCode = OH_ImagePackerNative_Release(testPacker); 110 if (errCode != IMAGE_SUCCESS) 111 { 112 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 113 return errCode; 114 } 115 // Release the ImageSource instance. 116 errCode = OH_ImageSourceNative_Release(imageSource); 117 if (errCode != IMAGE_SUCCESS) 118 { 119 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImageSourceNative_Release failed, errCode: %{public}d.", errCode); 120 return errCode; 121 } 122 123 return IMAGE_SUCCESS; 124} 125 126Image_ErrorCode packToFileFromPixelmapTest(uint8_t *buffer, size_t bufferSize, int fd) 127{ 128 // Create an ImagePacker instance. 129 OH_ImagePackerNative *testPacker = nullptr; 130 Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker); 131 if (errCode != IMAGE_SUCCESS) { 132 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode); 133 return errCode; 134 } 135 136 // Create a Pixelmap instance. 137 OH_Pixelmap_InitializationOptions *createOpts; 138 OH_PixelmapInitializationOptions_Create(&createOpts); 139 OH_PixelmapInitializationOptions_SetWidth(createOpts, 6); 140 OH_PixelmapInitializationOptions_SetHeight(createOpts, 4); 141 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, 3); 142 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, 0); 143 OH_PixelmapNative *pixelmap = nullptr; 144 errCode = OH_PixelmapNative_CreatePixelmap(buffer, bufferSize, createOpts, &pixelmap); 145 if (errCode != IMAGE_SUCCESS) { 146 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_PixelmapNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 147 return errCode; 148 } 149 150 // Specify encoding parameters and encode the PixelMap into a file. 151 OH_PackingOptions *option = nullptr; 152 OH_PackingOptions_Create(&option); 153 char type[] = "image/jpeg"; 154 Image_MimeType image_MimeType = {type, strlen(type)}; 155 OH_PackingOptions_SetMimeType(option, &image_MimeType); 156 errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd); 157 if (errCode != IMAGE_SUCCESS) { 158 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromPixelmap failed, errCode: %{public}d.", errCode); 159 return errCode; 160 } 161 162 // Release the ImagePacker instance. 163 errCode = OH_ImagePackerNative_Release(testPacker); 164 if (errCode != IMAGE_SUCCESS) 165 { 166 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode); 167 return errCode; 168 } 169 170 // Release the Pixelmap instance. 171 errCode = OH_PixelmapNative_Release(pixelmap); 172 if (errCode != IMAGE_SUCCESS) 173 { 174 OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_PixelmapNative_Release failed, errCode: %{public}d.", errCode); 175 return errCode; 176 } 177 178 return IMAGE_SUCCESS; 179} 180``` 181