1e41f4b71Sopenharmony_ci# Using Image to Decode Images 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciImage decoding refers to the process of decoding an archived image in a supported format into a [pixel map](image-overview.md) for image display or [processing](image-transformation.md). Currently, the following image formats are supported: JPEG, PNG, GIF, WebP, BMP, SVG, ICO, and DNG. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci## How to Develop 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciRead [Image](../../reference/apis-image-kit/js-apis-image.md) for APIs related to image decoding. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### Adding Dependencies 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciOpen the **src/main/cpp/CMakeLists.txt** file of the native project, add **libace_napi.z.so**, **libpixelmap_ndk.z.so**, **libimage_source_ndk.z.so**, **librawfile.z.so**, and **libhilog_ndk.z.so** (on which the native log APIs depend) to the **target_link_libraries** dependency. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```txt 14e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so libimage_source_ndk.z.so librawfile.z.so) 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci### Adding API Mappings 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ciOpen the **src/main/cpp/hello.cpp** file, and add the mapping of the **getSyncPixelMap** function to the **Init** function. The **getSyncPixelMap** function is used to generate a pixel map in synchronous mode. The code is as follows: 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci```c++ 22e41f4b71Sopenharmony_ciEXTERN_C_START 23e41f4b71Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports) 24e41f4b71Sopenharmony_ci{ 25e41f4b71Sopenharmony_ci napi_property_descriptor desc[] = { 26e41f4b71Sopenharmony_ci { "getSyncPixelMap", nullptr, getSyncPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr }, 27e41f4b71Sopenharmony_ci }; 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); 30e41f4b71Sopenharmony_ci return exports; 31e41f4b71Sopenharmony_ci} 32e41f4b71Sopenharmony_ciEXTERN_C_END 33e41f4b71Sopenharmony_ci``` 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci### Calling APIs on the JS Side 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci1. Open **src\main\cpp\types\*libentry*\index.d.ts** (where **libentry** varies according to the project name), and import the following files: 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci ```js 40e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 41e41f4b71Sopenharmony_ci import { resourceManager } from '@kit.LocalizationKit'; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci // Synchronous call. The input parameters are the resource manager and image resource name, and a pixel map is returned. 44e41f4b71Sopenharmony_ci export const getSyncPixelMap: (resMgr: resourceManager.ResourceManager, src: string) => image.PixelMap; 45e41f4b71Sopenharmony_ci ``` 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci2. Prepare an image resource file, named **example.jpg** in this example, and import it to **src\main\resources\rawfile\**. 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci3. Open **src\main\ets\pages\index.ets**, import ***libentry*.so** (where **libentry** varies according to the project name), call the native APIs, and pass in the JS resource object. The following is an example: 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci ```js 52e41f4b71Sopenharmony_ci import testNapi from 'libentry.so' 53e41f4b71Sopenharmony_ci import { image } from '@kit.ImageKit'; 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci @Entry 56e41f4b71Sopenharmony_ci @Component 57e41f4b71Sopenharmony_ci struct Index { 58e41f4b71Sopenharmony_ci @State pixelMap : PixelMap | undefined = undefined; 59e41f4b71Sopenharmony_ci aboutToAppear() { 60e41f4b71Sopenharmony_ci // Call the custom getSyncPixelMap function to obtain a pixel map. 61e41f4b71Sopenharmony_ci this.pixelMap = testNapi.getSyncPixelMap(getContext(this).resourceManager, "example.jpg") 62e41f4b71Sopenharmony_ci } 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci build() { 65e41f4b71Sopenharmony_ci Row() { 66e41f4b71Sopenharmony_ci Column() { 67e41f4b71Sopenharmony_ci Image(this.pixelMap) 68e41f4b71Sopenharmony_ci .width(100) 69e41f4b71Sopenharmony_ci .height(100) 70e41f4b71Sopenharmony_ci } 71e41f4b71Sopenharmony_ci .width('100%') 72e41f4b71Sopenharmony_ci } 73e41f4b71Sopenharmony_ci .height('100%') 74e41f4b71Sopenharmony_ci } 75e41f4b71Sopenharmony_ci } 76e41f4b71Sopenharmony_ci ``` 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci### Calling the Native APIs 79e41f4b71Sopenharmony_ci 80e41f4b71Sopenharmony_ciFor details about the APIs, see [Image API Reference](../../reference/apis-image-kit/image.md). 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciObtain the JS resource object from the **hello.cpp** file and convert it to a native resource object. Then you can call native APIs. 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci**Adding Reference Files** 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci ```c++ 87e41f4b71Sopenharmony_ci // Include the image framework, raw file, raw file management, and log print header files. 88e41f4b71Sopenharmony_ci #include <cstdlib> 89e41f4b71Sopenharmony_ci #include <cstring> 90e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_source_mdk.h> 91e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_pixel_map_mdk.h> 92e41f4b71Sopenharmony_ci #include <rawfile/raw_file.h> 93e41f4b71Sopenharmony_ci #include <rawfile/raw_file_manager.h> 94e41f4b71Sopenharmony_ci #include <hilog/log.h> 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci static napi_value getSyncPixelMap(napi_env env, napi_callback_info info) 97e41f4b71Sopenharmony_ci { 98e41f4b71Sopenharmony_ci size_t argc = 2; 99e41f4b71Sopenharmony_ci napi_value args[2] = {nullptr}; 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci napi_valuetype srcType; 104e41f4b71Sopenharmony_ci napi_typeof(env, args[0], &srcType); 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci // The input parameter args[0] indicates the resource manager, which is used to initialize the resource manager at the native layer. 107e41f4b71Sopenharmony_ci NativeResourceManager *mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, args[0]); 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci size_t strSize; 110e41f4b71Sopenharmony_ci char srcBuf[2048]; 111e41f4b71Sopenharmony_ci // The input parameter args[1] indicates the file name. 112e41f4b71Sopenharmony_ci napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize); 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci // Use the resource manager to open the raw file. 115e41f4b71Sopenharmony_ci RawFile * rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf); 116e41f4b71Sopenharmony_ci if (rawFile != NULL) { 117e41f4b71Sopenharmony_ci // Obtain the file size and read data. 118e41f4b71Sopenharmony_ci long len = OH_ResourceManager_GetRawFileSize(rawFile); 119e41f4b71Sopenharmony_ci uint8_t * data = static_cast<uint8_t *>(malloc(len)); 120e41f4b71Sopenharmony_ci int res = OH_ResourceManager_ReadRawFile(rawFile, data, len); 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci OhosImageSource imageSource_c; 123e41f4b71Sopenharmony_ci imageSource_c.buffer = data; 124e41f4b71Sopenharmony_ci imageSource_c.bufferSize = len; 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci OhosImageSourceOps ops{}; 127e41f4b71Sopenharmony_ci napi_value imageSource; 128e41f4b71Sopenharmony_ci napi_value pixelMap; 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci // Create an ImageSource object using the read raw data. 131e41f4b71Sopenharmony_ci int32_t ret = OH_ImageSource_Create(env, &imageSource_c, &ops, &imageSource); 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci // Initialize the ImageSource object at the native layer. 134e41f4b71Sopenharmony_ci ImageSourceNative * imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource); 135e41f4b71Sopenharmony_ci OhosImageDecodingOps decodingOps{}; 136e41f4b71Sopenharmony_ci // Create a pixel map. 137e41f4b71Sopenharmony_ci OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap); 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci // The following APIs are used for the GIF format. 140e41f4b71Sopenharmony_ci // napi_value pixelMapList; 141e41f4b71Sopenharmony_ci // OH_ImageSource_CreatePixelMapList(imageSourceNative_c, &decodingOps, &pixelMapList); 142e41f4b71Sopenharmony_ci // OhosImageSourceDelayTimeList list{}; 143e41f4b71Sopenharmony_ci // OH_ImageSource_GetDelayTime(imageSourceNative_c, &list); 144e41f4b71Sopenharmony_ci // uint32_t count; 145e41f4b71Sopenharmony_ci // OH_ImageSource_GetFrameCount(imageSourceNative_c, &count); 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci OhosImageSourceInfo info{}; 148e41f4b71Sopenharmony_ci // Read the image width and height. 149e41f4b71Sopenharmony_ci OH_ImageSource_GetImageInfo(imageSourceNative_c, 0, &info); 150e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[decode]", "imageInfo width:%{public}d , height:%{public}d", info.size.width, info.size.height); 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci // Read the ImageWidth configuration of the image source and print logs. 153e41f4b71Sopenharmony_ci OhosImageSourceProperty target; 154e41f4b71Sopenharmony_ci char exifKey_c[] = "ImageWidth"; 155e41f4b71Sopenharmony_ci target.size = strlen(exifKey_c); 156e41f4b71Sopenharmony_ci target.value = exifKey_c; 157e41f4b71Sopenharmony_ci 158e41f4b71Sopenharmony_ci OhosImageSourceProperty response{}; 159e41f4b71Sopenharmony_ci response.size = 20; 160e41f4b71Sopenharmony_ci response.value = static_cast<char *>(malloc(20)); 161e41f4b71Sopenharmony_ci OH_ImageSource_GetImageProperty(imageSourceNative_c, &target, &response); 162e41f4b71Sopenharmony_ci OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "[decode]", "ImageProperty width after modify:%{public}s", response.value); 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci // After the processing is complete, release resources at the native layer. 165e41f4b71Sopenharmony_ci OH_ImageSource_Release(imageSourceNative_c); 166e41f4b71Sopenharmony_ci OH_ResourceManager_CloseRawFile(rawFile); 167e41f4b71Sopenharmony_ci return pixelMap; 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); 170e41f4b71Sopenharmony_ci return nullptr; 171e41f4b71Sopenharmony_ci } 172e41f4b71Sopenharmony_ci ``` 173e41f4b71Sopenharmony_ci 174e41f4b71Sopenharmony_ciThe image framework supports incremental decoding. The method is as follows: 175e41f4b71Sopenharmony_ci 176e41f4b71Sopenharmony_ci ```c++ 177e41f4b71Sopenharmony_ci // Include the image framework, raw file, raw file management, and log print header files. 178e41f4b71Sopenharmony_ci #include <cstdlib> 179e41f4b71Sopenharmony_ci #include <cstring> 180e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_source_mdk.h> 181e41f4b71Sopenharmony_ci #include <multimedia/image_framework/image_pixel_map_mdk.h> 182e41f4b71Sopenharmony_ci #include <rawfile/raw_file.h> 183e41f4b71Sopenharmony_ci #include <rawfile/raw_file_manager.h> 184e41f4b71Sopenharmony_ci #include <hilog/log.h> 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci static napi_value getSyncPixelMap(napi_env env, napi_callback_info info) 187e41f4b71Sopenharmony_ci { 188e41f4b71Sopenharmony_ci size_t argc = 2; 189e41f4b71Sopenharmony_ci napi_value args[2] = {nullptr}; 190e41f4b71Sopenharmony_ci 191e41f4b71Sopenharmony_ci napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci napi_valuetype srcType; 194e41f4b71Sopenharmony_ci napi_typeof(env, args[0], &srcType); 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci // The input parameter args[0] indicates the resource manager, which is used to initialize the resource manager at the native layer. 197e41f4b71Sopenharmony_ci NativeResourceManager * mNativeResMgr = OH_ResourceManager_InitNativeResourceManager(env, args[0]); 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ci size_t strSize; 200e41f4b71Sopenharmony_ci char srcBuf[2048]; 201e41f4b71Sopenharmony_ci // The input parameter args[1] indicates the file name. 202e41f4b71Sopenharmony_ci napi_get_value_string_utf8(env, args[1], srcBuf, sizeof(srcBuf), &strSize); 203e41f4b71Sopenharmony_ci 204e41f4b71Sopenharmony_ci // Use the resource manager to open the raw file. 205e41f4b71Sopenharmony_ci RawFile * rawFile = OH_ResourceManager_OpenRawFile(mNativeResMgr, srcBuf); 206e41f4b71Sopenharmony_ci if (rawFile != NULL) { 207e41f4b71Sopenharmony_ci // Obtain the file size. If the file size is greater than 2048 bytes, incremental decoding is performed. Otherwise, full decoding is performed. 208e41f4b71Sopenharmony_ci long len = OH_ResourceManager_GetRawFileSize(rawFile); 209e41f4b71Sopenharmony_ci if (len > 2048) { 210e41f4b71Sopenharmony_ci uint8_t * data = static_cast<uint8_t *>(malloc(len)); 211e41f4b71Sopenharmony_ci // Read all data in the file. 212e41f4b71Sopenharmony_ci int res = OH_ResourceManager_ReadRawFile(rawFile, data, len); 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci uint8_t * holderdata = static_cast<uint8_t *>(malloc(len)); 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci OhosImageSource imageSource_c; 217e41f4b71Sopenharmony_ci // A buffer of imageSource_c is allocated, but no data is filled in. 218e41f4b71Sopenharmony_ci imageSource_c.buffer = holderdata; 219e41f4b71Sopenharmony_ci imageSource_c.bufferSize = len; 220e41f4b71Sopenharmony_ci OhosImageSourceOps ops{}; 221e41f4b71Sopenharmony_ci napi_value imageSource; 222e41f4b71Sopenharmony_ci // Initialize the incremental ImageSource object. 223e41f4b71Sopenharmony_ci OH_ImageSource_CreateIncremental(env, &imageSource_c, &ops, &imageSource); 224e41f4b71Sopenharmony_ci 225e41f4b71Sopenharmony_ci // Initialize the ImageSource object at the native layer. 226e41f4b71Sopenharmony_ci ImageSourceNative * imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource); 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci // The following simulates segment loading. Segments are loaded twice. 2048 bytes of data are loaded for the first time, and the remaining data is loaded for the second time. 229e41f4b71Sopenharmony_ci OhosImageSourceUpdateData firstData{}; 230e41f4b71Sopenharmony_ci firstData.buffer = data; // Image data. 231e41f4b71Sopenharmony_ci firstData.bufferSize = len; // Total size of the image data. 232e41f4b71Sopenharmony_ci firstData.isCompleted = false; 233e41f4b71Sopenharmony_ci firstData.offset = 0; // The first loading starts from the very beginning. 234e41f4b71Sopenharmony_ci firstData.updateLength = 2048; // 2048 bytes are loaded for the first time. 235e41f4b71Sopenharmony_ci OH_ImageSource_UpdateData(imageSourceNative_c, &firstData); 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci OhosImageSourceUpdateData secondData{}; 238e41f4b71Sopenharmony_ci secondData.buffer = data; 239e41f4b71Sopenharmony_ci secondData.bufferSize = len; 240e41f4b71Sopenharmony_ci secondData.isCompleted = true; // Last loading, to indicating that loading is complete. 241e41f4b71Sopenharmony_ci secondData.offset = 2048; // 2048 bytes of data have been loaded. Offset the data to load for the second time. 242e41f4b71Sopenharmony_ci secondData.updateLength = len - 2048; // Load the remaining data for the second time. 243e41f4b71Sopenharmony_ci OH_ImageSource_UpdateData(imageSourceNative_c, &secondData); 244e41f4b71Sopenharmony_ci 245e41f4b71Sopenharmony_ci napi_value pixelMap; 246e41f4b71Sopenharmony_ci OhosImageDecodingOps decodingOps{}; 247e41f4b71Sopenharmony_ci decodingOps.index = 0; 248e41f4b71Sopenharmony_ci // Create a pixel map. 249e41f4b71Sopenharmony_ci OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap); 250e41f4b71Sopenharmony_ci 251e41f4b71Sopenharmony_ci // After the processing is complete, release resources at the native layer. 252e41f4b71Sopenharmony_ci OH_ImageSource_Release(imageSourceNative_c); 253e41f4b71Sopenharmony_ci OH_ResourceManager_CloseRawFile(rawFile); 254e41f4b71Sopenharmony_ci return pixelMap; 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci // Read all data in the raw file. 257e41f4b71Sopenharmony_ci uint8_t * data = static_cast<uint8_t *>(malloc(len)); 258e41f4b71Sopenharmony_ci int res = OH_ResourceManager_ReadRawFile(rawFile, data, len); 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci OhosImageSource imageSource_c; 261e41f4b71Sopenharmony_ci imageSource_c.buffer = data; 262e41f4b71Sopenharmony_ci imageSource_c.bufferSize = len; 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci OhosImageSourceOps ops{}; 265e41f4b71Sopenharmony_ci napi_value imageSource; 266e41f4b71Sopenharmony_ci napi_value pixelMap; 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci // Create an ImageSource object using the read raw data. 269e41f4b71Sopenharmony_ci int32_t ret = OH_ImageSource_Create(env, &imageSource_c, &ops, &imageSource); 270e41f4b71Sopenharmony_ci 271e41f4b71Sopenharmony_ci // Initialize the ImageSource object at the native layer. 272e41f4b71Sopenharmony_ci ImageSourceNative * imageSourceNative_c = OH_ImageSource_InitNative(env, imageSource); 273e41f4b71Sopenharmony_ci OhosImageDecodingOps decodingOps{}; 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci // Create a pixel map. 276e41f4b71Sopenharmony_ci OH_ImageSource_CreatePixelMap(imageSourceNative_c, &decodingOps, &pixelMap); 277e41f4b71Sopenharmony_ci 278e41f4b71Sopenharmony_ci // After the processing is complete, release resources at the native layer. 279e41f4b71Sopenharmony_ci OH_ImageSource_Release(imageSourceNative_c); 280e41f4b71Sopenharmony_ci OH_ResourceManager_CloseRawFile(rawFile); 281e41f4b71Sopenharmony_ci return pixelMap; 282e41f4b71Sopenharmony_ci } 283e41f4b71Sopenharmony_ci OH_ResourceManager_ReleaseNativeResourceManager(mNativeResMgr); 284e41f4b71Sopenharmony_ci return nullptr; 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci ``` 287