• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Using Image_NativeModule to Decode Pictures
2
3In this process, you need to create an image source, decode a picture, and release the image source instance.
4
5## How to Develop
6
7### Adding a Link Library
8
9Open 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.
10
11```txt
12target_link_libraries(entry PUBLIC libhilog_ndk.z.so libimage_source.so)
13```
14
15### Calling the Native APIs
16
17For details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md).
18
19Implement the C APIs in **hello.cpp**. Refer to the sample code below.
20
21**Example of Using the Decoding APIs**
22
23After creating an **ImageSource** instance, obtain and modify property values, create a **PixelMap** object by using decoding parameters, and obtain the number of image frames.
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    static ImagePictureNative *thisPicture = new ImagePictureNative();
52
53    // Release the image source.
54    Image_ErrorCode ReleaseImageSource(OH_ImageSourceNative *&source) {
55        if (source != nullptr) {
56            thisPicture->errorCode = OH_ImageSourceNative_Release(source);
57            source = nullptr;
58            return thisPicture->errorCode;
59        }
60        OH_LOG_DEBUG(LOG_APP, "ReleaseImageSource source is null !");
61        return IMAGE_SUCCESS;
62    }
63
64    // Process the NAPI return value.
65    napi_value getJsResult(napi_env env, int result) {
66        napi_value resultNapi = nullptr;
67        napi_create_int32(env, result, &resultNapi);
68        return resultNapi;
69    }
70
71    // Create decoding parameters.
72    static napi_value CreateDecodingOptions(napi_env env, napi_callback_info info) {
73        thisPicture->errorCode = OH_DecodingOptionsForPicture_Create(&thisPicture->options);
74
75        if (thisPicture->errorCode != IMAGE_SUCCESS) {
76            OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_Create failed, errCode: %{public}d.",
77                thisPicture->errorCode);
78            return getJsResult(env, thisPicture->errorCode);
79        } else {
80            OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_Create success !");
81        }
82
83        return getJsResult(env, thisPicture->errorCode);
84    }
85
86    // Configure decoding parameters, which are passed from the application layer.
87    static napi_value SetDesiredAuxiliaryPictures(napi_env env, napi_callback_info info) {
88        size_t argc = 1;
89        napi_value args[1] = {nullptr};
90        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) {
91            OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !");
92            return getJsResult(env, IMAGE_BAD_PARAMETER);
93        }
94
95        uint32_t length = 0;
96        napi_get_array_length(env, args[0], &length);
97        if (length <= 0) {
98            OH_LOG_ERROR(LOG_APP, "napi_get_array_length failed !");
99            return getJsResult(env, IMAGE_UNKNOWN_ERROR);
100        }
101        Image_AuxiliaryPictureType typeList[length];
102        for (int index = 0; index < length; index++) {
103            napi_value element;
104            uint32_t ulType = 0;
105            napi_get_element(env, args[0], index, &element);
106            napi_get_value_uint32(env, element, &ulType);
107            typeList[index] = static_cast<Image_AuxiliaryPictureType>(ulType);
108            OH_LOG_DEBUG(LOG_APP, "ulType is :%{public}d", ulType);
109        }
110
111        thisPicture->errorCode =
112            OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures(thisPicture->options, typeList, length);
113        if (thisPicture->errorCode != IMAGE_SUCCESS) {
114            OH_LOG_ERROR(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures failed,errCode: %{public}d.",
115                thisPicture->errorCode);
116            return getJsResult(env, thisPicture->errorCode);
117        } else {
118            OH_LOG_DEBUG(LOG_APP, "OH_DecodingOptionsForPicture_SetDesiredAuxiliaryPictures success !");
119        }
120
121        return getJsResult(env, thisPicture->errorCode);
122    }
123
124
125    // Decode data.
126    static napi_value CreatePictureByImageSource(napi_env env, napi_callback_info info) {
127        size_t argc = 1;
128        napi_value args[1] = {nullptr};
129        napi_value result = nullptr;
130
131        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok || argc < 1 || args[0] == nullptr) {
132            OH_LOG_ERROR(LOG_APP, "CreatePicture_ napi_get_cb_info failed !");
133            return getJsResult(env, IMAGE_BAD_PARAMETER);
134        }
135        char filePath[1024];
136        size_t filePathSize;
137        napi_get_value_string_utf8(env, args[0], filePath, 1024, &filePathSize);
138        ReleaseImageSource(thisPicture->source);
139
140        thisPicture->errorCode = OH_ImageSourceNative_CreateFromUri(filePath, filePathSize, &thisPicture->source);
141        if (thisPicture->errorCode != IMAGE_SUCCESS) {
142            OH_LOG_ERROR(LOG_APP, "OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.",
143                thisPicture->errorCode);
144            return getJsResult(env, thisPicture->errorCode);
145        } else {
146            OH_LOG_DEBUG(LOG_APP, "OH_ImageSourceNative_CreateFromUri success !");
147        }
148
149        thisPicture->errorCode =
150            OH_ImageSourceNative_CreatePicture(thisPicture->source, thisPicture->options, &thisPicture->picture);
151        if (thisPicture->errorCode != IMAGE_SUCCESS) {
152            OH_LOG_ERROR(LOG_APP, "ImageSourceNative_CreatePicture failed, errCode: %{public}d.", thisPicture->errorCode);
153            return getJsResult(env, thisPicture->errorCode);
154        } else {
155            napi_status status =
156                napi_create_external(env, reinterpret_cast<void *>(thisPicture->picture), nullptr, nullptr, &result);
157            if (status != napi_ok) {
158                napi_throw_error(env, nullptr, "Failed to create external object");
159                return nullptr;
160            }
161            OH_LOG_DEBUG(LOG_APP, "ImageSourceNative_CreatePicture success !");
162        }
163
164        return result;
165    }
166   ```
167