1# Using Image_NativeModule to Decode Pictures 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, decode it to obtain a Picture object, 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** 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) 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 <hilog/log.h> 32#include <bits/alltypes.h> 33#include <unistd.h> 34#include <sys/types.h> 35#include <sys/stat.h> 36#include <fcntl.h> 37#include <sstream> 38#include <multimedia/image_framework/image/image_native.h> 39#include <multimedia/image_framework/image/image_packer_native.h> 40#include <multimedia/image_framework/image/image_source_native.h> 41#include <multimedia/image_framework/image/picture_native.h> 42#include <multimedia/image_framework/image/pixelmap_native.h> 43 44#define AUTO 0 45#define SDR 1 46 47class ImagePictureNative { 48public: 49 Image_ErrorCode errorCode = IMAGE_SUCCESS; 50 OH_DecodingOptionsForPicture *options = nullptr; 51 OH_ImagePackerNative *imagePacker = nullptr; 52 OH_PackingOptions *packerOptions = nullptr; 53 OH_PictureNative *picture = nullptr; 54 OH_ImageSourceNative *source = nullptr; 55 ImagePictureNative() {} 56 ~ImagePictureNative() {} 57}; 58 59class ImageAuxiliaryPictureNative { 60public: 61 Image_ErrorCode errorCode = IMAGE_SUCCESS; 62 Image_AuxiliaryPictureType type = AUXILIARY_PICTURE_TYPE_GAINMAP; 63 OH_AuxiliaryPictureNative *auxiliaryPicture = nullptr; 64 size_t BUFF_SIZE = 640 * 480 * 4; // Size of the auxiliary picture (width * height * number of bytes per pixel). 65 ImageAuxiliaryPictureNative() {} 66 ~ImageAuxiliaryPictureNative() {} 67}; 68 69static ImagePictureNative *thisPicture = new ImagePictureNative(); 70static ImageAuxiliaryPictureNative *thisAuxiliaryPicture = new ImageAuxiliaryPictureNative(); 71 72// Release the image source. 73Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) { 74 if (source != nullptr) { 75 thisPicture->errorCode = OH_ImageSourceNative_Release(source); 76 source = nullptr; 77 return thisPicture->errorCode; 78 } 79 OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null !"); 80 return IMAGE_SUCCESS; 81} 82 83// Process the NAPI return value. 84napi_value getJsResult(napi_env env, int result) { 85 napi_value resultNapi = nullptr; 86 napi_create_int32(env, result, &resultNapi); 87 return resultNapi; 88} 89 90// Create decoding parameters. 91static napi_value CreateDecodingOptions(napi_env env, napi_callback_info info) { 92 thisPicture->errorCode = OH_DecodingOptionsForPicture_Create(&thisPicture->options); 93 94 if (thisPicture->errorCode != IMAGE_SUCCESS) { 95 OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_Create failed, errCode: %{public}d.", 96 thisPicture->errorCode); 97 return getJsResult(env, thisPicture->errorCode); 98 } else { 99 OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_Create success !"); 100 } 101 102 return getJsResult(env, thisPicture->errorCode); 103} 104 105// Configure decoding parameters, which are passed from the application layer. 106static napi_value SetDesiredAuxiliaryPictures(napi_env env, napi_callback_info info) { 107 size_t argc = 1; 108 napi_value args[1] = {nullptr}; 109 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) { 110 OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !"); 111 return getJsResult(env, IMAGE_BAD_PARAMETER); 112 } 113 114 uint32_t length = 0; 115 napi_get_array_length(env, args[0], &length); 116 if (length <= 0) { 117 OH_LOG_ERROR(LOG_APP, "napi_get_array_length failed !"); 118 return getJsResult(env, IMAGE_UNKNOWN_ERROR); 119 } 120 Image_AuxiliaryPictureType typeList[length]; 121 for (int index = 0; index < length; index++) { 122 napi_value element; 123 uint32_t ulType = 0; 124 napi_get_element(env, args[0], index, &element); 125 napi_get_value_uint32(env, element, &ulType); 126 typeList[index] = static_cast<Image_AuxiliaryPictureType>(ulType); 127 OH_LOG_DEBUG(LOG_APP, "ulType is :%{public}d", ulType); 128 } 129 130 thisPicture->errorCode = 131 OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(thisPicture->options, typeList, length); 132 if (thisPicture->errorCode != IMAGE_SUCCESS) { 133 OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures failed,errCode: %{public}d.", 134 thisPicture->errorCode); 135 return getJsResult(env, thisPicture->errorCode); 136 } else { 137 OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures success !"); 138 } 139 140 return getJsResult(env, thisPicture->errorCode); 141} 142 143 144// Decode data. 145static napi_value CreatePictureByImageSource(napi_env env, napi_callback_info info) { 146 size_t argc = 1; 147 napi_value args[1] = {nullptr}; 148 napi_value result = nullptr; 149 150 if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) { 151 OH_LOG_ERROR(LOG_APP, "CreatePicture_ napi_get_cb_info failed !"); 152 return getJsResult(env, IMAGE_BAD_PARAMETER); 153 } 154 char filePath[1024]; 155 size_t filePathSize; 156 napi_get_value_string_utf8(env, args[0], filePath, 1024, &filePathSize); 157 ReleaseImageSource(thisPicture->source); 158 159 thisPicture->errorCode = OH_ImageSourceNative_CreateFromUri(filePath, filePathSize, &thisPicture->source); 160 if (thisPicture->errorCode != IMAGE_SUCCESS) { 161 OH_LOG_ERROR(LOG_APP, "OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", 162 thisPicture->errorCode); 163 return getJsResult(env, thisPicture->errorCode); 164 } else { 165 OH_LOG_DEBUG(LOG_APP, "OH_ImageSourceNative_CreateFromUri success !"); 166 } 167 168 thisPicture->errorCode = 169 OH_ImageSourceNative_CreatePicture(thisPicture->source, thisPicture->options, &thisPicture->picture); 170 thisAuxiliaryPicture->errorCode = OH_PictureNative_GetAuxiliaryPicture(thisPicture->picture, 171 thisAuxiliaryPicture->type, &thisAuxiliaryPicture->auxiliaryPicture); 172 if (thisAuxiliaryPicture->errorCode == IMAGE_SUCCESS) { 173 uint8_t* buff = new uint8_t[thisAuxiliaryPicture->BUFF_SIZE]; 174 OH_AuxiliaryPictureNative_ReadPixels(thisAuxiliaryPicture->auxiliaryPicture, buff, 175 &thisAuxiliaryPicture->BUFF_SIZE); 176 OH_AuxiliaryPictureNative_Release(thisAuxiliaryPicture->auxiliaryPicture); 177 delete []buff; 178 } 179 if (thisPicture->errorCode != IMAGE_SUCCESS) { 180 OH_LOG_ERROR(LOG_APP, "ImageSourceNative_CreatePicture failed, errCode: %{public}d.", thisPicture->errorCode); 181 return getJsResult(env, thisPicture->errorCode); 182 } else { 183 napi_status status = 184 napi_create_external(env, reinterpret_cast<void *>(thisPicture->picture), nullptr, nullptr, &result); 185 if (status != napi_ok) { 186 napi_throw_error(env, nullptr, "Failed to create external object"); 187 return nullptr; 188 } 189 OH_LOG_DEBUG(LOG_APP, "ImageSourceNative_CreatePicture success !"); 190 } 191 192 return result; 193} 194``` 195