1# Using Image_NativeModule to Encode Pictures 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--SE: @liyang_bryan--> 6<!--TSE: @xchaosioda--> 7 8With Image_NativeModule, you can create and release ImagePacker instances and encode picture objects. 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 Picture 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 <hilog/log.h> 36#include <bits/alltypes.h> 37#include <unistd.h> 38#include <sys/types.h> 39#include <sys/stat.h> 40#include <fcntl.h> 41#include <sstream> 42#include <multimedia/image_framework/image/image_native.h> 43#include <multimedia/image_framework/image/image_packer_native.h> 44#include <multimedia/image_framework/image/image_source_native.h> 45#include <multimedia/image_framework/image/picture_native.h> 46#include <multimedia/image_framework/image/pixelmap_native.h> 47 48#define AUTO 0 49#define SDR 1 50 51class ImagePictureNative { 52public: 53 Image_ErrorCode errorCode = IMAGE_SUCCESS; 54 OH_DecodingOptionsForPicture *options = nullptr; 55 OH_ImagePackerNative *imagePacker = nullptr; 56 OH_PackingOptions *packerOptions = nullptr; 57 OH_PictureNative *picture = nullptr; 58 OH_ImageSourceNative *source = nullptr; 59 ImagePictureNative() {} 60 ~ImagePictureNative() {} 61}; 62 63static ImagePictureNative *thisPicture = new ImagePictureNative(); 64 65// Process the NAPI return value. 66napi_value getJsResult(napi_env env, int result) { 67 napi_value resultNapi = nullptr; 68 napi_create_int32(env, result, &resultNapi); 69 return resultNapi; 70} 71 72// Release the image source. 73Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) { 74 if (source != nullptr) { 75 thisPicture->errorCode = OH_ImageSourceNative_Release(source); 76 source = nullptr; 77 return thisPicture->errorCode; 78 } 79OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null!"); 80 return IMAGE_SUCCESS; 81} 82 83// Set packing parameters. 84void SetPackOptions(OH_PackingOptions *packerOptions, Image_MimeType format, uint32_t quality, bool needsPackProperties, 85 int32_t desiredDynamicRange) { 86 OH_PackingOptions_SetMimeType(packerOptions, &format); 87 OH_PackingOptions_SetQuality(packerOptions, quality); 88 OH_PackingOptions_SetNeedsPackProperties(packerOptions, needsPackProperties); 89 OH_PackingOptions_SetDesiredDynamicRange(packerOptions, desiredDynamicRange); 90} 91 92// Call PackToData. 93static napi_value PackToDataFromPicture(napi_env env, napi_callback_info info) { 94 size_t argc = 2; 95 napi_value args[2] = {nullptr}; 96 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { 97 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed!"); 98 return getJsResult(env, thisPicture->errorCode); 99 } 100 uint32_t fd = 0; 101 napi_get_value_uint32(env, args[0], &fd); 102 size_t outDataSize = 10000 * 10000; 103 uint8_t *outData = new uint8_t[outDataSize]; 104 105 if (thisPicture->packerOptions == nullptr) { 106 thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions); 107 } 108 if (thisPicture->imagePacker == nullptr) { 109 thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker); 110 } 111 112 char strFormat[20]; 113 size_t strFormatSize; 114 napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize); 115 OH_LOG_DEBUG(LOG_APP, "PackToDataFromPicture format: %{public}s", strFormat); 116 117 Image_MimeType format; 118 format.size = strFormatSize; 119 format.data = const_cast<char *>(strFormat); 120 uint32_t quality = 98; 121 bool needsPackProperties = true; 122 int32_t desiredDynamicRange = AUTO; 123 SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange); 124 125 thisPicture->errorCode = OH_ImagePackerNative_PackToDataFromPicture( 126 thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, outData, &outDataSize); 127 if (thisPicture->errorCode != IMAGE_SUCCESS) { 128 OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture failed, errCode: %{public}d.", 129 thisPicture->errorCode); 130 delete[] outData; 131 return getJsResult(env, thisPicture->errorCode); 132 } else { 133 ReleaseImageSource(thisPicture->source); 134 OH_ImageSourceNative_CreateFromData(outData, outDataSize, &thisPicture->source); 135 OH_ImagePackerNative_PackToFileFromImageSource(thisPicture->imagePacker, thisPicture->packerOptions, 136 thisPicture->source, fd); 137 ReleaseImageSource(thisPicture->source); 138 OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture success !"); 139 } 140 delete[] outData; 141 return getJsResult(env, thisPicture->errorCode); 142} 143 144// Call PackToFile. 145static napi_value PackToFileFromPicture(napi_env env, napi_callback_info info) { 146 size_t argc = 2; 147 napi_value args[2] = {nullptr}; 148 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { 149 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed!"); 150 return getJsResult(env, thisPicture->errorCode); 151 } 152 uint32_t fd = 0; 153 napi_get_value_uint32(env, args[0], &fd); 154 155 if (thisPicture->packerOptions == nullptr) { 156 thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions); 157 } 158 if (thisPicture->imagePacker == nullptr) { 159 thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker); 160 } 161 162 char strFormat[20]; 163 size_t strFormatSize; 164 napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize); 165OH_LOG_ERROR(LOG_APP, "PackToFileFromPicture format: %{public}s", strFormat); 166 167 Image_MimeType format; 168 format.size = strFormatSize; 169 format.data = const_cast<char *>(strFormat); 170 uint32_t quality = 98; 171 bool needsPackProperties = false; 172 int32_t desiredDynamicRange = SDR; 173 SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange); 174 175 thisPicture->errorCode = OH_ImagePackerNative_PackToFileFromPicture( 176 thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, fd); 177 178 if (thisPicture->errorCode != IMAGE_SUCCESS) { 179 OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture failed, errCode: %{public}d.", 180 thisPicture->errorCode); 181 182 return getJsResult(env, thisPicture->errorCode); 183 } else { 184 OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture success !"); 185 } 186 187 return getJsResult(env, thisPicture->errorCode); 188} 189``` 190