1# Using Image_NativeModule to Decode Images 2 3You can use the **ImageSource** class to create an image source, obtain the width and height of the PixelMap, and release **ImageSource** instances. 4 5## How to Develop 6 7### Adding a Link Library 8 9Open the **src/main/cpp/CMakeLists.txt** file of the native project, add **libimage_source.so** and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency. 10 11```txt 12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so) 13``` 14 15### Calling the Native APIs 16 17For details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md). 18 19Implement the C APIs in **hello.cpp**. Refer to the sample code below. 20 21**Example of Using the Decoding APIs** 22 23After creating an **ImageSource** instance, obtain and modify property values, create a **PixelMap** object by using decoding parameters, and obtain the number of image frames. 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 33 #undef LOG_DOMAIN 34 #undef LOG_TAG 35 #define LOG_DOMAIN 0x3200 36 #define LOG_TAG "MY_TAG" 37 38 #define NUM_0 0 39 #define NUM_1 1 40 41 static napi_value sourceTest(napi_env env, napi_callback_info info) 42 { 43 napi_value argValue[NUM_1] = {0}; 44 size_t argCount = NUM_1; 45 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 || 46 argValue[NUM_0] == nullptr) { 47 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest napi_get_cb_info failed, argCount: %{public}d.", argCount); 48 return getJsResult(env, IMAGE_BAD_PARAMETER); 49 } 50 char name[1024]; 51 size_t nameSize = 1024; 52 napi_get_value_string_utf8(env, argValue[NUM_0], name, 1024, &nameSize); 53 54 // Create an ImageSource instance. 55 OH_ImageSourceNative *source = nullptr; 56 Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source); 57 if (errCode != IMAGE_SUCCESS) { 58 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode); 59 return errCode; 60 } 61 62 // Create a structure object that defines the image information and obtain the image information. 63 OH_ImageSource_Info *imageInfo; 64 OH_ImageSourceInfo_Create(&imageInfo); 65 errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo); 66 if (errCode != IMAGE_SUCCESS) { 67 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", errCode); 68 return errCode; 69 } 70 71 // Obtain the values of the specified properties. 72 uint32_t width, height; 73 OH_ImageSourceInfo_GetWidth(imageInfo, &width); 74 OH_ImageSourceInfo_GetHeight(imageInfo, &height); 75 OH_ImageSourceInfo_Release(imageInfo); 76 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo success, width: %{public}d, height: %{public}d.", width, height); 77 Image_String getKey; 78 const std::string PIXEL_X_DIMENSION = "PixelXDimension"; 79 getKey.data = (char *)PIXEL_X_DIMENSION.c_str(); 80 getKey.size = PIXEL_X_DIMENSION.length(); 81 Image_String getValue; 82 errCode = OH_ImageSourceNative_GetImageProperty(source, &getKey, &getValue); 83 if (errCode != IMAGE_SUCCESS) { 84 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageProperty failed, errCode: %{public}d.", errCode); 85 return errCode; 86 } 87 88 // Modify the values of the specified properties. 89 Image_String setKey; 90 const std::string ORIENTATION = "Orientation"; 91 setKey.data = (char *)ORIENTATION.c_str(); 92 setKey.size = ORIENTATION.length(); 93 Image_String setValue; 94 setValue.data = (char *)"4"; 95 setValue.size = 1; 96 errCode = OH_ImageSourceNative_ModifyImageProperty(source, &setKey, &setValue); 97 if (errCode != IMAGE_SUCCESS) { 98 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_ModifyImageProperty failed, errCode: %{public}d.", errCode); 99 return errCode; 100 } 101 102 // Create a PixelMap object based on image decoding parameters. 103 OH_DecodingOptions *ops = nullptr; 104 OH_DecodingOptions_Create(&ops); 105 // If IMAGE_DYNAMIC_RANGE_AUTO is passed in, decoding is performed based on the image format. If the image is an HDR resource, an HDR PixelMap is obtained after decoding. 106 OH_DecodingOptions_SetDesiredDynamicRange(ops, IMAGE_DYNAMIC_RANGE_AUTO); 107 OH_PixelmapNative *resPixMap = nullptr; 108 109 // A null pointer cannot be passed in to ops. If ops does not need to be set, you do not need to create a PixelMap object. 110 errCode = OH_ImageSourceNative_CreatePixelmap(source, ops, &resPixMap); 111 OH_DecodingOptions_Release(ops); 112 if (errCode != IMAGE_SUCCESS) { 113 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 114 return errCode; 115 } 116 117 // Check whether the PixelMap is the HDR content. 118 OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr; 119 OH_PixelmapImageInfo_Create(&pixelmapImageInfo); 120 OH_PixelmapNative_GetImageInfo(resPixMap, pixelmapImageInfo); 121 bool pixelmapIsHdr; 122 OH_PixelmapImageInfo_GetDynamicRange(pixelmapImageInfo, &pixelmapIsHdr); 123 OH_PixelmapImageInfo_Release(pixelmapImageInfo); 124 125 // Obtain the number of image frames. 126 uint32_t frameCnt = 0; 127 errCode = OH_ImageSourceNative_GetFrameCount(source, &frameCnt); 128 if (errCode != IMAGE_SUCCESS) { 129 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetFrameCount failed, errCode: %{public}d.", errCode); 130 return errCode; 131 } 132 133 // Create a PixelMap list based on image decoding parameters. 134 OH_DecodingOptions *opts = nullptr; 135 OH_DecodingOptions_Create(&opts); 136 OH_PixelmapNative **resVecPixMap = new OH_PixelmapNative*[frameCnt]; 137 size_t outSize = frameCnt; 138 errCode = OH_ImageSourceNative_CreatePixelmapList(source, opts, resVecPixMap, outSize); 139 OH_DecodingOptions_Release(opts); 140 delete[] resVecPixMap; 141 if (errCode != IMAGE_SUCCESS) { 142 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmapList failed, errCode: %{public}d.", errCode); 143 return errCode; 144 } 145 146 // Obtain the image delay time list. 147 int32_t *delayTimeList = new int32_t[frameCnt]; 148 size_t size = frameCnt; 149 errCode = OH_ImageSourceNative_GetDelayTimeList(source, delayTimeList, size); 150 delete[] delayTimeList; 151 if (errCode != IMAGE_SUCCESS) { 152 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetDelayTimeList failed, errCode: %{public}d.", errCode); 153 return errCode; 154 } 155 156 // Release the ImageSource instance. 157 OH_ImageSourceNative_Release(source); 158 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest success."); 159 return IMAGE_SUCCESS; 160 } 161 ``` 162