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