1# 使用Image_NativeModule完成多图对象解码 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--Designer: @liyang_bryan--> 6<!--Tester: @xchaosioda--> 7<!--Adviser: @zengyawen--> 8 9创建ImageSource,解码得到Picture,以及释放ImageSource实例。 10 11## 开发步骤 12 13### 添加链接库 14 15在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加libimage_source.so 以及日志依赖libhilog_ndk.z.so。 16 17```txt 18target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so) 19``` 20 21### Native接口调用 22 23具体接口说明请参考[API文档](../../reference/apis-image-kit/capi-image-nativemodule.md)。 24 25在Deveco Studio新建Native C++应用,默认生成的项目中包含index.ets文件,在entry\src\main\cpp目录下会自动生成一个cpp文件(hello.cpp或napi_init.cpp,本示例以hello.cpp文件名为例)。在hello.cpp中实现C API接口调用逻辑,示例代码如下: 26 27**解码接口使用示例** 28 29在创建ImageSource实例后,进行指定属性值的获取和修改,通过解码参数创建PixelMap对象,获取图像帧数等操作。 30 31```c++ 32#include <hilog/log.h> 33#include <multimedia/image_framework/image/image_native.h> 34#include <multimedia/image_framework/image/image_packer_native.h> 35#include <multimedia/image_framework/image/image_source_native.h> 36#include <multimedia/image_framework/image/picture_native.h> 37#include <multimedia/image_framework/image/pixelmap_native.h> 38 39#define AUTO 0 40#define SDR 1 41 42class ImagePictureNative { 43public: 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 54class ImageAuxiliaryPictureNative { 55public: 56 Image_ErrorCode errorCode = IMAGE_SUCCESS; 57 Image_AuxiliaryPictureType type = AUXILIARY_PICTURE_TYPE_GAINMAP; 58 OH_AuxiliaryPictureNative *auxiliaryPicture = nullptr; 59 size_t BUFF_SIZE = 640 * 480 * 4; //辅助图size 长*宽*每像素占用字节数。 60 ImageAuxiliaryPictureNative() {} 61 ~ImageAuxiliaryPictureNative() {} 62}; 63 64static ImagePictureNative *thisPicture = new ImagePictureNative(); 65static ImageAuxiliaryPictureNative *thisAuxiliaryPicture = new ImageAuxiliaryPictureNative(); 66 67// 释放ImageSource。 68Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) { 69 if (source != nullptr) { 70 thisPicture->errorCode = OH_ImageSourceNative_Release(source); 71 source = nullptr; 72 return thisPicture->errorCode; 73 } 74 OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null !"); 75 return IMAGE_SUCCESS; 76} 77 78// 处理napi返回值。 79napi_value getJsResult(napi_env env, int result) { 80 napi_value resultNapi = nullptr; 81 napi_create_int32(env, result, &resultNapi); 82 return resultNapi; 83} 84 85// 创造解码参数。 86static napi_value CreateDecodingOptions(napi_env env, napi_callback_info info) { 87 thisPicture->errorCode = OH_DecodingOptionsForPicture_Create(&thisPicture->options); 88 89 if (thisPicture->errorCode != IMAGE_SUCCESS) { 90 OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_Create failed, errCode: %{public}d.", 91 thisPicture->errorCode); 92 return getJsResult(env, thisPicture->errorCode); 93 } else { 94 OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_Create success !"); 95 } 96 97 return getJsResult(env, thisPicture->errorCode); 98} 99 100// 配置解码参数 从应用层传入。 101static napi_value SetDesiredAuxiliaryPictures(napi_env env, napi_callback_info info) { 102 size_t argc = 1; 103 napi_value args[1] = {nullptr}; 104 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) { 105 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !"); 106 return getJsResult(env, IMAGE_BAD_PARAMETER); 107 } 108 109 uint32_t length = 0; 110 napi_get_array_length(env, args[0], &length); 111 if (length <= 0) { 112 OH_LOG_ERROR(LOG_APP, "napi_get_array_length failed !"); 113 return getJsResult(env, IMAGE_UNKNOWN_ERROR); 114 } 115 Image_AuxiliaryPictureType typeList[length]; 116 for (int index = 0; index < length; index++) { 117 napi_value element; 118 uint32_t ulType = 0; 119 napi_get_element(env, args[0], index, &element); 120 napi_get_value_uint32(env, element, &ulType); 121 typeList[index] = static_cast<Image_AuxiliaryPictureType>(ulType); 122 OH_LOG_DEBUG(LOG_APP, "ulType is :%{public}d", ulType); 123 } 124 125 thisPicture->errorCode = 126 OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(thisPicture->options, typeList, length); 127 if (thisPicture->errorCode != IMAGE_SUCCESS) { 128 OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures failed,errCode: %{public}d.", 129 thisPicture->errorCode); 130 return getJsResult(env, thisPicture->errorCode); 131 } else { 132 OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures success !"); 133 } 134 135 return getJsResult(env, thisPicture->errorCode); 136} 137 138 139// 解码。 140static napi_value CreatePictureByImageSource(napi_env env, napi_callback_info info) { 141 size_t argc = 1; 142 napi_value args[1] = {nullptr}; 143 napi_value result = nullptr; 144 145 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) { 146 OH_LOG_ERROR(LOG_APP, "CreatePicture_ napi_get_cb_info failed !"); 147 return getJsResult(env, IMAGE_BAD_PARAMETER); 148 } 149 char filePath[1024]; 150 size_t filePathSize; 151 napi_get_value_string_utf8(env, args[0], filePath, 1024, &filePathSize); 152 ReleaseImageSource(thisPicture->source); 153 154 thisPicture->errorCode = OH_ImageSourceNative_CreateFromUri(filePath, filePathSize, &thisPicture->source); 155 if (thisPicture->errorCode != IMAGE_SUCCESS) { 156 OH_LOG_ERROR(LOG_APP, "OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", 157 thisPicture->errorCode); 158 return getJsResult(env, thisPicture->errorCode); 159 } else { 160 OH_LOG_DEBUG(LOG_APP, "OH_ImageSourceNative_CreateFromUri success !"); 161 } 162 163 thisPicture->errorCode = 164 OH_ImageSourceNative_CreatePicture(thisPicture->source, thisPicture->options, &thisPicture->picture); 165 thisAuxiliaryPicture->errorCode = OH_PictureNative_GetAuxiliaryPicture(thisPicture->picture, 166 thisAuxiliaryPicture->type, &thisAuxiliaryPicture->auxiliaryPicture); 167 if (thisAuxiliaryPicture->errorCode == IMAGE_SUCCESS) { 168 uint8_t* buff = new uint8_t[thisAuxiliaryPicture->BUFF_SIZE]; 169 OH_AuxiliaryPictureNative_ReadPixels(thisAuxiliaryPicture->auxiliaryPicture, buff, 170 &thisAuxiliaryPicture->BUFF_SIZE); 171 OH_AuxiliaryPictureNative_Release(thisAuxiliaryPicture->auxiliaryPicture); 172 delete []buff; 173 } 174 if (thisPicture->errorCode != IMAGE_SUCCESS) { 175 OH_LOG_ERROR(LOG_APP, "ImageSourceNative_CreatePicture failed, errCode: %{public}d.", thisPicture->errorCode); 176 return getJsResult(env, thisPicture->errorCode); 177 } else { 178 napi_status status = 179 napi_create_external(env, reinterpret_cast<void *>(thisPicture->picture), nullptr, nullptr, &result); 180 if (status != napi_ok) { 181 napi_throw_error(env, nullptr, "Failed to create external object"); 182 return nullptr; 183 } 184 OH_LOG_DEBUG(LOG_APP, "ImageSourceNative_CreatePicture success !"); 185 } 186 187 return result; 188} 189``` 190