1# 使用Image_NativeModule完成图片解码 2 3创建ImageSource,获取位图的宽、高信息,以及释放ImageSource实例。 4 5## 开发步骤 6 7### 添加链接库 8 9在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加libimage_source.so、libpixelmap.so以及日志依赖libhilog_ndk.z.so。 10 11```txt 12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so libpixelmap.so) 13``` 14 15### Native接口调用 16 17具体接口说明请参考[API文档](../../reference/apis-image-kit/_image___native_module.md)。 18 19在Deveco Studio新建Native C++应用,默认生成的项目中包含index.ets文件,在entry\src\main\cpp目录下会自动生成一个cpp文件(hello.cpp或napi_init.cpp,本示例以hello.cpp文件名为例)。在hello.cpp中实现C API接口调用逻辑,示例代码如下: 20 21**解码接口使用示例** 22 23在创建ImageSource实例后,进行指定属性值的获取和修改、通过解码参数创建PixelMap对象、获取图像帧数等操作。 24 25 ```c++ 26 27 #include <linux/kd.h> 28 #include <string> 29 30 #include <hilog/log.h> 31 #include <multimedia/image_framework/image/image_source_native.h> 32 #include <multimedia/image_framework/image/pixelmap_native.h> 33 34 #undef LOG_DOMAIN 35 #undef LOG_TAG 36 #define LOG_DOMAIN 0x3200 37 #define LOG_TAG "MY_TAG" 38 39 #define NUM_0 0 40 #define NUM_1 1 41 42 // 处理napi返回值。 43 napi_value getJsResult(napi_env env, int result) { 44 napi_value resultNapi = nullptr; 45 napi_create_int32(env, result, &resultNapi); 46 return resultNapi; 47 } 48 49 static napi_value sourceTest(napi_env env, napi_callback_info info) 50 { 51 napi_value argValue[NUM_1] = {0}; 52 size_t argCount = NUM_1; 53 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 || 54 argValue[NUM_0] == nullptr) { 55 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest napi_get_cb_info failed, argCount: %{public}d.", argCount); 56 return getJsResult(env, IMAGE_BAD_PARAMETER); 57 } 58 char name[1024]; 59 size_t nameSize = 1024; 60 napi_get_value_string_utf8(env, argValue[NUM_0], name, 1024, &nameSize); 61 62 //创建ImageSource实例。 63 OH_ImageSourceNative *source = nullptr; 64 Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source); 65 if (errCode != IMAGE_SUCCESS) { 66 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode); 67 return getJsResult(env, errCode); 68 } 69 70 //创建定义图片信息的结构体对象,并获取图片信息。 71 OH_ImageSource_Info *imageInfo; 72 OH_ImageSourceInfo_Create(&imageInfo); 73 errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo); 74 if (errCode != IMAGE_SUCCESS) { 75 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", errCode); 76 return getJsResult(env, errCode); 77 } 78 79 //获取指定属性键的值。 80 uint32_t width, height; 81 OH_ImageSourceInfo_GetWidth(imageInfo, &width); 82 OH_ImageSourceInfo_GetHeight(imageInfo, &height); 83 OH_ImageSourceInfo_Release(imageInfo); 84 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo success, width: %{public}d, height: %{public}d.", width, height); 85 Image_String getKey; 86 const std::string PIXEL_X_DIMENSION = "PixelXDimension"; 87 getKey.data = (char *)PIXEL_X_DIMENSION.c_str(); 88 getKey.size = PIXEL_X_DIMENSION.length(); 89 Image_String getValue; 90 errCode = OH_ImageSourceNative_GetImageProperty(source, &getKey, &getValue); 91 if (errCode != IMAGE_SUCCESS) { 92 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageProperty failed, errCode: %{public}d.", errCode); 93 return getJsResult(env, errCode); 94 } 95 96 //修改指定属性键的值。 97 Image_String setKey; 98 const std::string ORIENTATION = "Orientation"; 99 setKey.data = (char *)ORIENTATION.c_str(); 100 setKey.size = ORIENTATION.length(); 101 Image_String setValue; 102 setValue.data = (char *)"4"; 103 setValue.size = 1; 104 errCode = OH_ImageSourceNative_ModifyImageProperty(source, &setKey, &setValue); 105 if (errCode != IMAGE_SUCCESS) { 106 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_ModifyImageProperty failed, errCode: %{public}d.", errCode); 107 return getJsResult(env, errCode); 108 } 109 110 //通过图片解码参数创建PixelMap对象。 111 OH_DecodingOptions *ops = nullptr; 112 OH_DecodingOptions_Create(&ops); 113 //设置为AUTO会根据图片资源格式解码,如果图片资源为HDR资源则会解码为HDR的pixelmap。 114 OH_DecodingOptions_SetDesiredDynamicRange(ops, IMAGE_DYNAMIC_RANGE_AUTO); 115 OH_PixelmapNative *resPixMap = nullptr; 116 117 //ops参数支持传入nullptr, 当不需要设置解码参数时,不用创建。 118 errCode = OH_ImageSourceNative_CreatePixelmap(source, ops, &resPixMap); 119 OH_DecodingOptions_Release(ops); 120 if (errCode != IMAGE_SUCCESS) { 121 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 122 return getJsResult(env, errCode); 123 } 124 125 //判断pixelmap是否为hdr内容。 126 OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr; 127 OH_PixelmapImageInfo_Create(&pixelmapImageInfo); 128 OH_PixelmapNative_GetImageInfo(resPixMap, pixelmapImageInfo); 129 bool pixelmapIsHdr; 130 OH_PixelmapImageInfo_GetDynamicRange(pixelmapImageInfo, &pixelmapIsHdr); 131 OH_PixelmapImageInfo_Release(pixelmapImageInfo); 132 133 //获取图像帧数。 134 uint32_t frameCnt = 0; 135 errCode = OH_ImageSourceNative_GetFrameCount(source, &frameCnt); 136 if (errCode != IMAGE_SUCCESS) { 137 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetFrameCount failed, errCode: %{public}d.", errCode); 138 return getJsResult(env, errCode); 139 } 140 141 //通过图片解码参数创建Pixelmap列表。 142 OH_DecodingOptions *opts = nullptr; 143 OH_DecodingOptions_Create(&opts); 144 OH_PixelmapNative **resVecPixMap = new OH_PixelmapNative*[frameCnt]; 145 size_t outSize = frameCnt; 146 errCode = OH_ImageSourceNative_CreatePixelmapList(source, opts, resVecPixMap, outSize); 147 OH_DecodingOptions_Release(opts); 148 delete[] resVecPixMap; 149 if (errCode != IMAGE_SUCCESS) { 150 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmapList failed, errCode: %{public}d.", errCode); 151 return getJsResult(env, errCode); 152 } 153 154 //获取图像延迟时间列表。 155 int32_t *delayTimeList = new int32_t[frameCnt]; 156 size_t size = frameCnt; 157 errCode = OH_ImageSourceNative_GetDelayTimeList(source, delayTimeList, size); 158 delete[] delayTimeList; 159 if (errCode != IMAGE_SUCCESS) { 160 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetDelayTimeList failed, errCode: %{public}d.", errCode); 161 return getJsResult(env, errCode); 162 } 163 164 //释放ImageSource实例。 165 OH_ImageSourceNative_Release(source); 166 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest success."); 167 return getJsResult(env, IMAGE_SUCCESS); 168 } 169 ``` 170