1# 使用Image_NativeModule完成多图对象解码
2
3创建图片源,解码多图对象,以及释放图片源实例。
4
5## 开发步骤
6
7### 添加链接库
8
9在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添libimage_source.so 以及日志依赖libhilog_ndk.z.so10
11```txt
12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so)
13```
14
15### Native接口调用
16
17具体接口说明请参考[API文档](../../reference/apis-image-kit/_image___native_module.md)。
18
19hello.cpp中实现C API接口调用逻辑,示例代码如下:
20
21**解码接口使用示例**
22
23在创建ImageSource实例后,进行指定属性值的获取和修改,通过解码参数创建PixelMap对象,获取图像帧数等操作。
24
25   ```c++
26
27    #include "imagepicture_native.h"
28    #include "common/log_common.h"
29    #include <bits/alltypes.h>
30    #include <unistd.h>
31    #include <sys/types.h>
32    #include <sys/stat.h>
33    #include <fcntl.h>
34    #include <sstream>
35
36    #define AUTO 0
37    #define SDR 1
38
39    class ImagePictureNative {
40    public:
41        Image_ErrorCode errorCode = IMAGE_SUCCESS;
42        OH_DecodingOptionsForPicture *options = nullptr;
43        OH_ImagePackerNative *imagePacker = nullptr;
44        OH_PackingOptions *packerOptions = nullptr;
45        OH_PictureNative *picture = nullptr;
46        OH_ImageSourceNative *source = nullptr;
47        ImagePictureNative() {}
48        ~ImagePictureNative() {}
49    };
50
51	class ImageAuxiliaryPictureNative {
52	public:
53        Image_ErrorCode errorCode = IMAGE_SUCCESS;
54        Image_AuxiliaryPictureType type = AUXILIARY_PICTURE_TYPE_GAINMAP;
55        OH_AuxiliaryPictureNative *auxiliaryPicture = nullptr;
56        size_t BUFF_SIZE = 640 * 480 * 4; 	//辅助图size 长*宽*每像素占用字节数
57        ImageAuxiliaryPictureNative() {}
58        ~ImageAuxiliaryPictureNative() {}
59    };
60
61    static ImagePictureNative *thisPicture = new ImagePictureNative();
62	static ImageAuxiliaryPictureNative *thisAuxiliaryPicture = new ImageAuxiliaryPictureNative();
63
64    // 释放ImageSource
65    Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) {
66        if (source != nullptr) {
67            thisPicture->errorCode = OH_ImageSourceNative_Release(source);
68            source = nullptr;
69            return thisPicture->errorCode;
70        }
71        OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null !");
72        return IMAGE_SUCCESS;
73    }
74
75    // 处理napi返回值
76    napi_value getJsResult(napi_env env, int result) {
77        napi_value resultNapi = nullptr;
78        napi_create_int32(env, result, &resultNapi);
79        return resultNapi;
80    }
81
82    // 创造解码参数
83    static napi_value CreateDecodingOptions(napi_env env, napi_callback_info info) {
84        thisPicture->errorCode = OH_DecodingOptionsForPicture_Create(&thisPicture->options);
85
86        if (thisPicture->errorCode != IMAGE_SUCCESS) {
87            OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_Create failed, errCode: %{public}d.",
88                thisPicture->errorCode);
89            return getJsResult(env, thisPicture->errorCode);
90        } else {
91            OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_Create success !");
92        }
93
94        return getJsResult(env, thisPicture->errorCode);
95    }
96
97    // 配置解码参数 从应用层传入
98    static napi_value SetDesiredAuxiliaryPictures(napi_env env, napi_callback_info info) {
99        size_t argc = 1;
100        napi_value args[1] = {nullptr};
101        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) {
102            OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !");
103            return getJsResult(env, IMAGE_BAD_PARAMETER);
104        }
105
106        uint32_t length = 0;
107        napi_get_array_length(env, args[0], &length);
108        if (length <= 0) {
109            OH_LOG_ERROR(LOG_APP, "napi_get_array_length failed !");
110            return getJsResult(env, IMAGE_UNKNOWN_ERROR);
111        }
112        Image_AuxiliaryPictureType typeList[length];
113        for (int index = 0; index < length; index++) {
114            napi_value element;
115            uint32_t ulType = 0;
116            napi_get_element(env, args[0], index, &element);
117            napi_get_value_uint32(env, element, &ulType);
118            typeList[index] = static_cast<Image_AuxiliaryPictureType>(ulType);
119            OH_LOG_DEBUG(LOG_APP, "ulType is :%{public}d", ulType);
120        }
121
122        thisPicture->errorCode =
123            OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(thisPicture->options, typeList, length);
124        if (thisPicture->errorCode != IMAGE_SUCCESS) {
125            OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures failed,errCode: %{public}d.",
126                thisPicture->errorCode);
127            return getJsResult(env, thisPicture->errorCode);
128        } else {
129            OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures success !");
130        }
131
132        return getJsResult(env, thisPicture->errorCode);
133    }
134
135
136    // 解码
137    static napi_value CreatePictureByImageSource(napi_env env, napi_callback_info info) {
138        size_t argc = 1;
139        napi_value args[1] = {nullptr};
140        napi_value result = nullptr;
141
142        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) {
143            OH_LOG_ERROR(LOG_APP, "CreatePicture_ napi_get_cb_info failed !");
144            return getJsResult(env, IMAGE_BAD_PARAMETER);
145        }
146        char filePath[1024];
147        size_t filePathSize;
148        napi_get_value_string_utf8(env, args[0], filePath, 1024, &filePathSize);
149        ReleaseImageSource(thisPicture->source);
150
151        thisPicture->errorCode = OH_ImageSourceNative_CreateFromUri(filePath, filePathSize, &thisPicture->source);
152        if (thisPicture->errorCode != IMAGE_SUCCESS) {
153            OH_LOG_ERROR(LOG_APP, "OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.",
154                thisPicture->errorCode);
155            return getJsResult(env, thisPicture->errorCode);
156        } else {
157            OH_LOG_DEBUG(LOG_APP, "OH_ImageSourceNative_CreateFromUri success !");
158        }
159
160        thisPicture->errorCode =
161            OH_ImageSourceNative_CreatePicture(thisPicture->source, thisPicture->options, &thisPicture->picture);
162        thisAuxiliaryPicture->errorCode = OH_PictureNative_GetAuxiliaryPicture(thisPicture->picture,
163            thisAuxiliaryPicture->type, &thisAuxiliaryPicture->auxiliaryPicture);
164        if (thisAuxiliaryPicture->errorCode == IMAGE_SUCCESS) {
165            uint8_t* buff = new uint8_t[thisAuxiliaryPicture->BUFF_SIZE];
166        	OH_AuxiliaryPictureNative_ReadPixels(thisAuxiliaryPicture->auxiliaryPicture, buff,
167                &thisAuxiliaryPicture->BUFF_SIZE);
168        	OH_AuxiliaryPictureNative_Release(thisAuxiliaryPicture->auxiliaryPicture);
169        	delete []buff;
170        }
171        if (thisPicture->errorCode != IMAGE_SUCCESS) {
172            OH_LOG_ERROR(LOG_APP, "ImageSourceNative_CreatePicture failed, errCode: %{public}d.", thisPicture->errorCode);
173            return getJsResult(env, thisPicture->errorCode);
174        } else {
175            napi_status status =
176                napi_create_external(env, reinterpret_cast<void *>(thisPicture->picture), nullptr, nullptr, &result);
177            if (status != napi_ok) {
178                napi_throw_error(env, nullptr, "Failed to create external object");
179                return nullptr;
180            }
181            OH_LOG_DEBUG(LOG_APP, "ImageSourceNative_CreatePicture success !");
182        }
183
184        return result;
185    }
186   ```
187