1# 使用Image_NativeModule完成多图对象编码 2 3图像打包类,用于创建以及释放ImagePacker实例,并编码多图对象。 4 5## 开发步骤 6 7### 添加链接库 8 9在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添libimage_packer.so 以及日志依赖libhilog_ndk.z.so。 10 11```txt 12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_packer.so) 13``` 14 15### Native接口调用 16 17具体接口说明请参考[API文档](../../reference/apis-image-kit/_image___native_module.md)。 18 19在hello.cpp中实现C API接口调用逻辑,示例代码如下: 20 21**编码接口使用示例** 22 23在创建ImagePacker实例,指定打包参数后将Picture多图对象打包至文件或者缓冲区。 24 25> **说明:** 26> 根据MIME标准,标准编码格式为image/jpeg。当使用image编码时,打包参数中的编码格式image_MimeType设置为image/jpeg,image编码后的文件扩展名可设为.jpg或.jpeg,可在支持image/jpeg解码的平台上使用。 27 28 ```c++ 29 30 #include "imagepicture_native.h" 31 #include "common/log_common.h" 32 #include <bits/alltypes.h> 33 #include <unistd.h> 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <fcntl.h> 37 #include <sstream> 38 39 #define AUTO 0 40 #define SDR 1 41 42 class ImagePictureNative { 43 public: 44 Image_ErrorCode errorCode = IMAGE_SUCCESS; 45 OH_DecodingOptionsForPicture *options = nullptr; 46 OH_ImagePackerNative *imagePacker = nullptr; 47 OH_PackingOptions *packerOptions = nullptr; 48 OH_PictureNative *picture = nullptr; 49 OH_ImageSourceNative *source = nullptr; 50 ImagePictureNative() {} 51 ~ImagePictureNative() {} 52 }; 53 54 static ImagePictureNative *thisPicture = new ImagePictureNative(); 55 56 // 处理napi返回值 57 napi_value getJsResult(napi_env env, int result) { 58 napi_value resultNapi = nullptr; 59 napi_create_int32(env, result, &resultNapi); 60 return resultNapi; 61 } 62 63 // 释放ImageSource 64 Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) { 65 if (source != nullptr) { 66 thisPicture->errorCode = OH_ImageSourceNative_Release(source); 67 source = nullptr; 68 return thisPicture->errorCode; 69 } 70 OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null !"); 71 return IMAGE_SUCCESS; 72 } 73 74 // 设置编码参数 75 void SetPackOptions(OH_PackingOptions *packerOptions, Image_MimeType format, uint32_t quality, bool needsPackProperties, 76 int32_t desiredDynamicRange) { 77 OH_PackingOptions_SetMimeType(packerOptions, &format); 78 OH_PackingOptions_SetQuality(packerOptions, quality); 79 OH_PackingOptions_SetNeedsPackProperties(packerOptions, needsPackProperties); 80 OH_PackingOptions_SetDesiredDynamicRange(packerOptions, desiredDynamicRange); 81 } 82 83 // 编码PackToData 84 static napi_value PackToDataFromPicture(napi_env env, napi_callback_info info) { 85 size_t argc = 2; 86 napi_value args[2] = {nullptr}; 87 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { 88 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !"); 89 return getJsResult(env, thisPicture->errorCode); 90 } 91 uint32_t fd = 0; 92 napi_get_value_uint32(env, args[0], &fd); 93 size_t outDataSize = 10000 * 10000; 94 uint8_t *outData = new uint8_t[10000 * 10000]; 95 96 if (thisPicture->packerOptions == nullptr) { 97 thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions); 98 } 99 if (thisPicture->imagePacker == nullptr) { 100 thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker); 101 } 102 103 char strFormat[20]; 104 size_t strFormatSize; 105 napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize); 106 OH_LOG_DEBUG(LOG_APP, "PackToDataFromPicture format: %{public}s", strFormat); 107 108 Image_MimeType format; 109 format.size = strFormatSize; 110 format.data = const_cast<char *>(strFormat); 111 uint32_t quality = 98; 112 bool needsPackProperties = true; 113 int32_t desiredDynamicRange = AUTO; 114 SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange); 115 116 thisPicture->errorCode = OH_ImagePackerNative_PackToDataFromPicture( 117 thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, outData, &outDataSize); 118 if (thisPicture->errorCode != IMAGE_SUCCESS) { 119 OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture failed, errCode: %{public}d.", 120 thisPicture->errorCode); 121 delete[] outData; 122 return getJsResult(env, thisPicture->errorCode); 123 } else { 124 ReleaseImageSource(thisPicture->source); 125 OH_ImageSourceNative_CreateFromData(outData, outDataSize, &thisPicture->source); 126 OH_ImagePackerNative_PackToFileFromImageSource(thisPicture->imagePacker, thisPicture->packerOptions, 127 thisPicture->source, fd); 128 ReleaseImageSource(thisPicture->source); 129 OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture success !"); 130 } 131 132 return getJsResult(env, thisPicture->errorCode); 133 } 134 135 // 编码PackToFile 136 static napi_value PackToFileFromPicture(napi_env env, napi_callback_info info) { 137 size_t argc = 2; 138 napi_value args[2] = {nullptr}; 139 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) { 140 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !"); 141 return getJsResult(env, thisPicture->errorCode); 142 } 143 uint32_t fd = 0; 144 napi_get_value_uint32(env, args[0], &fd); 145 146 if (thisPicture->packerOptions == nullptr) { 147 thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions); 148 } 149 if (thisPicture->imagePacker == nullptr) { 150 thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker); 151 } 152 153 char strFormat[20]; 154 size_t strFormatSize; 155 napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize); 156 OH_LOG_ERROR(LOG_APP, "PackToFileFromPicture format: %{public}s", strFormat); 157 158 Image_MimeType format; 159 format.size = strFormatSize; 160 format.data = const_cast<char *>(strFormat); 161 uint32_t quality = 98; 162 bool needsPackProperties = false; 163 int32_t desiredDynamicRange = SDR; 164 SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange); 165 166 thisPicture->errorCode = OH_ImagePackerNative_PackToFileFromPicture( 167 thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, fd); 168 169 if (thisPicture->errorCode != IMAGE_SUCCESS) { 170 OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture failed, errCode: %{public}d.", 171 thisPicture->errorCode); 172 173 return getJsResult(env, thisPicture->errorCode); 174 } else { 175 OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture success !"); 176 } 177 178 return getJsResult(env, thisPicture->errorCode); 179 } 180 ``` 181