1# Using Image_NativeModule to Decode Images 2<!--Kit: Image Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @aulight02--> 5<!--SE: @liyang_bryan--> 6<!--TSE: @xchaosioda--> 7 8This topic describes how to create an ImageSource object, obtain the width and height of the PixelMap, and release the ImageSource object. 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_source.so**, **libpixelmap.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 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 Decoding APIs** 27 28After creating an ImageSource instance, obtain and modify property values, create a PixelMap object by using decoding parameters, and obtain the number of image frames. 29 30```c++ 31#include <linux/kd.h> 32#include <string> 33#include <hilog/log.h> 34#include <multimedia/image_framework/image/image_common.h> 35#include <multimedia/image_framework/image/image_source_native.h> 36#include <multimedia/image_framework/image/pixelmap_native.h> 37 38#undef LOG_DOMAIN 39#undef LOG_TAG 40#define LOG_DOMAIN 0x3200 41#define LOG_TAG "MY_TAG" 42 43#define NUM_0 0 44#define NUM_1 1 45 46// Process the NAPI return value. 47napi_value getJsResult(napi_env env, int result) { 48 napi_value resultNapi = nullptr; 49 napi_create_int32(env, result, &resultNapi); 50 return resultNapi; 51} 52 53static napi_value sourceTest(napi_env env, napi_callback_info info) 54{ 55 napi_value argValue[NUM_1] = {0}; 56 size_t argCount = NUM_1; 57 if (napi_get_cb_info(env, info, &argCount, argValue, nullptr, nullptr) != napi_ok || argCount < NUM_1 || 58 argValue[NUM_0] == nullptr) { 59 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest napi_get_cb_info failed, argCount: %{public}d.", argCount); 60 return getJsResult(env, IMAGE_BAD_PARAMETER); 61 } 62 char name[1024]; 63 size_t nameSize = 1024; 64 napi_get_value_string_utf8(env, argValue[NUM_0], name, 1024, &nameSize); 65 // Obtain the decoding capability range. 66 Image_MimeType* mimeType = nullptr; 67 size_t length = 0; 68 Image_ErrorCode errCode = OH_ImageSourceNative_GetSupportedFormats(&mimeType, &length); 69 if (errCode != IMAGE_SUCCESS) { 70 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetSupportedFormats failed, errCode: %{public}d.", errCode); 71 return getJsResult(env, errCode); 72 } 73 for (size_t count = 0; count < length; count++) { 74 OH_LOG_INFO(LOG_APP, "Decode supportedFormats:%{public}s", mimeType[count].data); 75 } 76 // Create an ImageSource instance. 77 OH_ImageSourceNative *source = nullptr; 78 errCode = OH_ImageSourceNative_CreateFromUri(name, nameSize, &source); 79 if (errCode != IMAGE_SUCCESS) { 80 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode); 81 return getJsResult(env, errCode); 82 } 83 84 // Create a structure object that defines the image information and obtain the image information. 85 OH_ImageSource_Info *imageInfo; 86 OH_ImageSourceInfo_Create(&imageInfo); 87 errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo); 88 if (errCode != IMAGE_SUCCESS) { 89 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", errCode); 90 return getJsResult(env, errCode); 91 } 92 93 // Obtain the values of the specified properties. 94 uint32_t width, height; 95 OH_ImageSourceInfo_GetWidth(imageInfo, &width); 96 OH_ImageSourceInfo_GetHeight(imageInfo, &height); 97 OH_ImageSourceInfo_Release(imageInfo); 98 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo success, width: %{public}d, height: %{public}d.", width, height); 99 Image_String getKey; 100 const std::string PIXEL_X_DIMENSION = "PixelXDimension"; 101 getKey.data = (char *)PIXEL_X_DIMENSION.c_str(); 102 getKey.size = PIXEL_X_DIMENSION.length(); 103 Image_String getValue; 104 errCode = OH_ImageSourceNative_GetImageProperty(source, &getKey, &getValue); 105 if (errCode != IMAGE_SUCCESS) { 106 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageProperty failed, errCode: %{public}d.", errCode); 107 return getJsResult(env, errCode); 108 } 109 110 // Modify the values of the specified properties. 111 Image_String setKey; 112 const std::string ORIENTATION = "Orientation"; 113 setKey.data = (char *)ORIENTATION.c_str(); 114 setKey.size = ORIENTATION.length(); 115 Image_String setValue; 116 setValue.data = (char *)"4"; 117 setValue.size = 1; 118 errCode = OH_ImageSourceNative_ModifyImageProperty(source, &setKey, &setValue); 119 if (errCode != IMAGE_SUCCESS) { 120 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_ModifyImageProperty failed, errCode: %{public}d.", errCode); 121 return getJsResult(env, errCode); 122 } 123 124 // Create a PixelMap object based on image decoding parameters. 125 OH_DecodingOptions *ops = nullptr; 126 OH_DecodingOptions_Create(&ops); 127 // 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. 128 OH_DecodingOptions_SetDesiredDynamicRange(ops, IMAGE_DYNAMIC_RANGE_AUTO); 129 OH_PixelmapNative *resPixMap = nullptr; 130 131 // 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. 132 errCode = OH_ImageSourceNative_CreatePixelmap(source, ops, &resPixMap); 133 OH_DecodingOptions_Release(ops); 134 if (errCode != IMAGE_SUCCESS) { 135 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmap failed, errCode: %{public}d.", errCode); 136 return getJsResult(env, errCode); 137 } 138 139 // Check whether the PixelMap is the HDR content. 140 OH_Pixelmap_ImageInfo *pixelmapImageInfo = nullptr; 141 OH_PixelmapImageInfo_Create(&pixelmapImageInfo); 142 OH_PixelmapNative_GetImageInfo(resPixMap, pixelmapImageInfo); 143 bool pixelmapIsHdr; 144 OH_PixelmapImageInfo_GetDynamicRange(pixelmapImageInfo, &pixelmapIsHdr); 145 OH_PixelmapImageInfo_Release(pixelmapImageInfo); 146 147 // Obtain the number of image frames. 148 uint32_t frameCnt = 0; 149 errCode = OH_ImageSourceNative_GetFrameCount(source, &frameCnt); 150 if (errCode != IMAGE_SUCCESS) { 151 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetFrameCount failed, errCode: %{public}d.", errCode); 152 return getJsResult(env, errCode); 153 } 154 155 // Create a PixelMap list based on image decoding parameters. 156 OH_DecodingOptions *opts = nullptr; 157 OH_DecodingOptions_Create(&opts); 158 OH_PixelmapNative **resVecPixMap = new OH_PixelmapNative*[frameCnt]; 159 size_t outSize = frameCnt; 160 errCode = OH_ImageSourceNative_CreatePixelmapList(source, opts, resVecPixMap, outSize); 161 OH_DecodingOptions_Release(opts); 162 delete[] resVecPixMap; 163 if (errCode != IMAGE_SUCCESS) { 164 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreatePixelmapList failed, errCode: %{public}d.", errCode); 165 return getJsResult(env, errCode); 166 } 167 168 // Obtain the image delay time list. 169 int32_t *delayTimeList = new int32_t[frameCnt]; 170 size_t size = frameCnt; 171 errCode = OH_ImageSourceNative_GetDelayTimeList(source, delayTimeList, size); 172 delete[] delayTimeList; 173 if (errCode != IMAGE_SUCCESS) { 174 OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetDelayTimeList failed, errCode: %{public}d.", errCode); 175 return getJsResult(env, errCode); 176 } 177 178 // Release the ImageSource instance. 179 OH_ImageSourceNative_Release(source); 180 OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest success."); 181 return getJsResult(env, IMAGE_SUCCESS); 182} 183``` 184