1# Using Image_NativeModule to Encode Images
2
3You can use the **ImagePacker** class to create and release **ImagePacker** instances.
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 ImageSource or Pixelmap image source 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 <linux/kd.h>
32      #include <string>
33
34      #include <hilog/log.h>
35      #include <multimedia/image_framework/image/image_packer_native.h>
36      #include <multimedia/image_framework/image/pixelmap_native.h>
37      #include <multimedia/image_framework/image/image_source_native.h>
38
39      #undef LOG_DOMAIN
40      #undef LOG_TAG
41      #define LOG_DOMAIN 0x3200
42      #define LOG_TAG "MY_TAG"
43
44      Image_ErrorCode packToFileFromImageSourceTest(int fd)
45      {
46          // Create an ImagePacker instance.
47          OH_ImagePackerNative *testPacker = nullptr;
48          Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker);
49          if (errCode != IMAGE_SUCCESS) {
50              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode);
51              return errCode;
52          }
53
54          // Create an ImageSource instance.
55          OH_ImageSourceNative* imageSource = nullptr;
56          errCode = OH_ImageSourceNative_CreateFromFd(fd, &imageSource);
57          if (errCode != IMAGE_SUCCESS) {
58              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImageSourceNative_CreateFromFd  failed, errCode: %{public}d.", errCode);
59              return errCode;
60          }
61
62          // Encode an ImageSource instance and pack it into a file.
63          OH_PackingOptions *option = nullptr;
64          OH_PackingOptions_Create(&option);
65          char type[] = "image/jpeg";
66          Image_MimeType image_MimeType = {type, strlen(type)};
67          OH_PackingOptions_SetMimeType(option, &image_MimeType);
68          // Encode the content as HDR content. (The resource must be HDR resource and the JPEG format must be supported.)
69          OH_PackingOptions_SetDesiredDynamicRange(option, IMAGE_PACKER_DYNAMIC_RANGE_AUTO);
70          errCode = OH_ImagePackerNative_PackToFileFromImageSource(testPacker, option, imageSource, fd);
71          if (errCode != IMAGE_SUCCESS) {
72              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromImageSource failed, errCode: %{public}d.", errCode);
73              return errCode;
74          }
75
76          // Release the ImagePacker instance.
77          errCode = OH_ImagePackerNative_Release(testPacker);
78          if (errCode != IMAGE_SUCCESS)
79          {
80              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode);
81              return errCode;
82          }
83          // Release the ImageSource instance.
84          errCode = OH_ImageSourceNative_Release(imageSource);
85          if (errCode != IMAGE_SUCCESS)
86          {
87              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImageSourceNative_Release failed, errCode: %{public}d.", errCode);
88              return errCode;
89          }
90
91          return IMAGE_SUCCESS;
92      }
93
94      Image_ErrorCode packToFileFromPixelmapTest(uint8_t *buffer, size_t buffSize, int fd)
95      {
96          // Create an ImagePacker instance.
97          OH_ImagePackerNative *testPacker = nullptr;
98          Image_ErrorCode errCode = OH_ImagePackerNative_Create(&testPacker);
99          if (errCode != IMAGE_SUCCESS) {
100              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest CreatePacker OH_ImagePackerNative_Create failed, errCode: %{public}d.", errCode);
101              return errCode;
102          }
103
104          // Create a Pixelmap instance.
105          OH_Pixelmap_InitializationOptions *createOpts;
106          OH_PixelmapInitializationOptions_Create(&createOpts);
107          OH_PixelmapInitializationOptions_SetWidth(createOpts, 6);
108          OH_PixelmapInitializationOptions_SetHeight(createOpts, 4);
109          OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, 3);
110          OH_PixelmapInitializationOptions_SetAlphaType(createOpts, 0);
111          OH_PixelmapNative *pixelmap = nullptr;
112          errCode = OH_PixelmapNative_CreatePixelmap(buffer, bufferSize, createOpts, &pixelmap);
113          if (errCode != IMAGE_SUCCESS) {
114              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_PixelmapNative_CreatePixelmap  failed, errCode: %{public}d.", errCode);
115              return errCode;
116          }
117
118          // Encode the Pixelmap instance and pack it into a file.
119          OH_PackingOptions *option = nullptr;
120          OH_PackingOptions_Create(&option);
121          char type[] = "image/jpeg";
122          Image_MimeType image_MimeType = {type, strlen(type)};
123          OH_PackingOptions_SetMimeType(option, &image_MimeType);
124          errCode = OH_ImagePackerNative_PackToFileFromPixelmap(testPacker, option, pixelmap, fd);
125          if (errCode != IMAGE_SUCCESS) {
126              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest OH_ImagePackerNative_PackToFileFromPixelmap  failed, errCode: %{public}d.", errCode);
127              return errCode;
128          }
129
130          // Release the ImagePacker instance.
131          errCode = OH_ImagePackerNative_Release(testPacker);
132          if (errCode != IMAGE_SUCCESS)
133          {
134              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_ImagePackerNative_Release failed, errCode: %{public}d.", errCode);
135              return errCode;
136          }
137
138          // Release the Pixelmap instance.
139          errCode = OH_PixelmapNative_Release(pixelmap);
140          if (errCode != IMAGE_SUCCESS)
141          {
142              OH_LOG_ERROR(LOG_APP, "ImagePackerNativeCTest ReleasePacker OH_PixelmapNative_Release failed, errCode: %{public}d.", errCode);
143              return errCode;
144          }
145
146          return IMAGE_SUCCESS;
147      }
148   ```
149