1# Using Image_NativeModule to Encode Pictures
2
3With Image_NativeModule, you can create and release **ImagePacker** instances and encode picture objects.
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_packer.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_packer.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 Encoding APIs**
22
23After an **ImagePacker** instance is created and packing parameters are specified, the Picture object is packed to a file or buffer.
24
25> **NOTE**
26>
27> According to the MIME protocol, the standard encoding format is image/jpeg. When the APIs provided by the image module are used for encoding, **image_MimeType** of the packing parameters must be set to **image/jpeg**. The file name extension of the encoded image file can be .jpg or .jpeg, and the file can be used on platforms that support image/jpeg decoding.
28
29   ```c++
30
31    #include "imagepicture_native.h"
32    #include "common/log_common.h"
33    #include <bits/alltypes.h>
34    #include <unistd.h>
35    #include <sys/types.h>
36    #include <sys/stat.h>
37    #include <fcntl.h>
38    #include <sstream>
39
40    #define AUTO 0
41    #define SDR 1
42
43    class ImagePictureNative {
44    public:
45        Image_ErrorCode errorCode = IMAGE_SUCCESS;
46        OH_DecodingOptionsForPicture *options = nullptr;
47        OH_ImagePackerNative *imagePacker = nullptr;
48        OH_PackingOptions *packerOptions = nullptr;
49        OH_PictureNative *picture = nullptr;
50        OH_ImageSourceNative *source = nullptr;
51        ImagePictureNative() {}
52        ~ImagePictureNative() {}
53    };
54
55    static ImagePictureNative *thisPicture = new ImagePictureNative();
56
57    // Process the NAPI return value.
58    napi_value getJsResult(napi_env env, int result) {
59        napi_value resultNapi = nullptr;
60        napi_create_int32(env, result, &resultNapi);
61        return resultNapi;
62    }
63
64    // Release the image source.
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    // Set packing parameters.
76    void SetPackOptions(OH_PackingOptions *packerOptions, Image_MimeType format, uint32_t quality, bool needsPackProperties,
77                        int32_t desiredDynamicRange) {
78        OH_PackingOptions_SetMimeType(packerOptions, &format);
79        OH_PackingOptions_SetQuality(packerOptions, quality);
80        OH_PackingOptions_SetNeedsPackProperties(packerOptions, needsPackProperties);
81        OH_PackingOptions_SetDesiredDynamicRange(packerOptions, desiredDynamicRange);
82    }
83
84    // Call PackToData.
85    static napi_value PackToDataFromPicture(napi_env env, napi_callback_info info) {
86        size_t argc = 2;
87        napi_value args[2] = {nullptr};
88        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
89            OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !");
90            return getJsResult(env, thisPicture->errorCode);
91        }
92        uint32_t fd = 0;
93        napi_get_value_uint32(env, args[0], &fd);
94        size_t outDataSize = 10000 * 10000;
95        uint8_t *outData = new uint8_t[10000 * 10000];
96
97        if (thisPicture->packerOptions == nullptr) {
98            thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions);
99        }
100        if (thisPicture->imagePacker == nullptr) {
101            thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker);
102        }
103
104        char strFormat[20];
105        size_t strFormatSize;
106        napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize);
107        OH_LOG_DEBUG(LOG_APP, "PackToDataFromPicture format: %{public}s", strFormat);
108
109        Image_MimeType format;
110        format.size = strFormatSize;
111        format.data = const_cast<char *>(strFormat);
112        uint32_t quality = 98;
113        bool needsPackProperties = true;
114        int32_t desiredDynamicRange = AUTO;
115        SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange);
116
117        thisPicture->errorCode = OH_ImagePackerNative_PackToDataFromPicture(
118            thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, outData, &outDataSize);
119        if (thisPicture->errorCode != IMAGE_SUCCESS) {
120        OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture failed, errCode: %{public}d.",
121                thisPicture->errorCode);
122            delete[] outData;
123            return getJsResult(env, thisPicture->errorCode);
124        } else {
125            ReleaseImageSource(thisPicture->source);
126            OH_ImageSourceNative_CreateFromData(outData, outDataSize, &thisPicture->source);
127            OH_ImagePackerNative_PackToFileFromImageSource(thisPicture->imagePacker, thisPicture->packerOptions,
128                                                        thisPicture->source, fd);
129            ReleaseImageSource(thisPicture->source);
130        OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToDataFromPicture success !");
131        }
132
133        return getJsResult(env, thisPicture->errorCode);
134    }
135
136    // Call PackToFile.
137    static napi_value PackToFileFromPicture(napi_env env, napi_callback_info info) {
138        size_t argc = 2;
139        napi_value args[2] = {nullptr};
140        if (napi_get_cb_info(env, info, &argc, args, nullptr, nullptr) != napi_ok) {
141        OH_LOG_ERROR(LOG_APP, "napi_get_cb_info failed !");
142            return getJsResult(env, thisPicture->errorCode);
143        }
144        uint32_t fd = 0;
145        napi_get_value_uint32(env, args[0], &fd);
146
147        if (thisPicture->packerOptions == nullptr) {
148            thisPicture->errorCode = OH_PackingOptions_Create(&thisPicture->packerOptions);
149        }
150        if (thisPicture->imagePacker == nullptr) {
151            thisPicture->errorCode = OH_ImagePackerNative_Create(&thisPicture->imagePacker);
152        }
153
154        char strFormat[20];
155        size_t strFormatSize;
156        napi_get_value_string_utf8(env, args[1], strFormat, 20, &strFormatSize);
157    OH_LOG_ERROR(LOG_APP, "PackToFileFromPicture format: %{public}s", strFormat);
158
159        Image_MimeType format;
160        format.size = strFormatSize;
161        format.data = const_cast<char *>(strFormat);
162        uint32_t quality = 98;
163        bool needsPackProperties = false;
164        int32_t desiredDynamicRange = SDR;
165        SetPackOptions(thisPicture->packerOptions, format, quality, needsPackProperties, desiredDynamicRange);
166
167        thisPicture->errorCode = OH_ImagePackerNative_PackToFileFromPicture(
168            thisPicture->imagePacker, thisPicture->packerOptions, thisPicture->picture, fd);
169
170        if (thisPicture->errorCode != IMAGE_SUCCESS) {
171        OH_LOG_ERROR(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture failed, errCode: %{public}d.",
172            thisPicture->errorCode);
173
174            return getJsResult(env, thisPicture->errorCode);
175        } else {
176        OH_LOG_DEBUG(LOG_APP, "OH_ImagePackerNative_PackToFileFromPicture success !");
177        }
178
179        return getJsResult(env, thisPicture->errorCode);
180    }
181   ```
182