1e41f4b71Sopenharmony_ci# Using Image_NativeModule for PixelMap Operations
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciYou can use the **Pixelmap** class to create a pixel map, obtain its width, height, pixel format, alpha type, and row stride, operate the pixel map, and release **Pixelmap** instances.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## How to Develop
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci### Adding a Link Library
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciOpen the **src/main/cpp/CMakeLists.txt** file of the native project, add **libpixelmap.so** and **libhilog_ndk.z.so** (on which the log APIs depend) to the **target_link_libraries** dependency.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```txt
12e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libhilog_ndk.z.so libpixelmap.so)
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci### Calling the Native APIs
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciFor details about the APIs, see [Image_NativeModule](../../reference/apis-image-kit/_image___native_module.md).
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ciImplement the C APIs in **hello.cpp**. Refer to the sample code below.
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci**Example of Using the Pixelmap APIs**
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ciCreate a **Pixelmap** instance after parameter initialization, read and write pixel data, and perform operations such as scaling, translating, flipping, rotating, and cropping on the image.
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci   ```c++
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci      #include <linux/kd.h>
28e41f4b71Sopenharmony_ci      #include <string>
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci      #include <hilog/log.h>
31e41f4b71Sopenharmony_ci      #include <multimedia/image_framework/image/pixelmap_native.h>
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci      #undef LOG_DOMAIN
34e41f4b71Sopenharmony_ci      #undef LOG_TAG
35e41f4b71Sopenharmony_ci      #define LOG_DOMAIN 0x3200
36e41f4b71Sopenharmony_ci      #define LOG_TAG "MY_TAG"
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci      Image_ErrorCode PixelmapTest()
39e41f4b71Sopenharmony_ci      {
40e41f4b71Sopenharmony_ci          uint8_t data[96];
41e41f4b71Sopenharmony_ci          size_t dataSize = 96;
42e41f4b71Sopenharmony_ci          for (int i = 0; i < dataSize; i++) {
43e41f4b71Sopenharmony_ci              data[i] = i + 1;
44e41f4b71Sopenharmony_ci          }
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_ci          // Create a parameter structure instance and set parameters.
47e41f4b71Sopenharmony_ci          OH_Pixelmap_InitializationOptions *createOpts;
48e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_Create(&createOpts);
49e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_SetWidth(createOpts, 6);
50e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_SetHeight(createOpts, 4);
51e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
52e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci          // Create a Pixelmap instance.
55e41f4b71Sopenharmony_ci          OH_PixelmapNative *pixelmap = nullptr;
56e41f4b71Sopenharmony_ci          Image_ErrorCode errCode = OH_PixelmapNative_CreatePixelmap(data, dataSize, createOpts, &pixelmap);
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci          // Read the image pixel data and write the data to the array.
59e41f4b71Sopenharmony_ci          uint8_t destination[96];
60e41f4b71Sopenharmony_ci          size_t destinationSize = 96;
61e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_ReadPixels(pixelmap, destination, &destinationSize);
62e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
63e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_ReadPixels failed, errCode: %{public}d.", errCode);
64e41f4b71Sopenharmony_ci              return errCode;
65e41f4b71Sopenharmony_ci          }
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci          // Read the image data in the buffer and write the data to the Pixelmap instance.
68e41f4b71Sopenharmony_ci          uint8_t source[96];
69e41f4b71Sopenharmony_ci          size_t sourceSize = 96;
70e41f4b71Sopenharmony_ci          for (int i = 0; i < sourceSize; i++) {
71e41f4b71Sopenharmony_ci              source[i] = i + 1;
72e41f4b71Sopenharmony_ci          }
73e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_WritePixels(pixelmap, source, sourceSize);
74e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
75e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_WritePixels failed, errCode: %{public}d.", errCode);
76e41f4b71Sopenharmony_ci              return errCode;
77e41f4b71Sopenharmony_ci          }
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci          // Create an image information instance and obtain the pixel information.
80e41f4b71Sopenharmony_ci          OH_Pixelmap_ImageInfo *imageInfo;
81e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_Create(&imageInfo);
82e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_GetImageInfo(pixelmap, imageInfo);
83e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
84e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_GetImageInfo failed, errCode: %{public}d.", errCode);
85e41f4b71Sopenharmony_ci              return errCode;
86e41f4b71Sopenharmony_ci          }
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci          // Obtain the width, height, pixel format, and alpha type of the image.
89e41f4b71Sopenharmony_ci          uint32_t width, height, rowStride;
90e41f4b71Sopenharmony_ci          int32_t pixelFormat, alphaType;
91e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_GetWidth(imageInfo, &width);
92e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_GetHeight(imageInfo, &height);
93e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_GetRowStride(imageInfo, &rowStride);
94e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_GetPixelFormat(imageInfo, &pixelFormat);
95e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_GetAlphaType(imageInfo, &alphaType);
96e41f4b71Sopenharmony_ci          OH_PixelmapImageInfo_Release(imageInfo);
97e41f4b71Sopenharmony_ci          OH_LOG_INFO(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest GetImageInfo success, width: %{public}d, height: %{public}d, rowStride: %{public}d, pixelFormat: %{public}d, alphaType: %{public}d.", width, height, rowStride, pixelFormat, alphaType);
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci          // Set the opacity rate to enable the pixel map to achieve the corresponding opacity effect.
100e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Opacity(pixelmap, 0.5);
101e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
102e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Opacity failed, errCode: %{public}d.", errCode);
103e41f4b71Sopenharmony_ci              return errCode;
104e41f4b71Sopenharmony_ci          }
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci          // Scale the image.
107e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Scale(pixelmap, 2.0, 1.0);
108e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
109e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Scale failed, errCode: %{public}d.", errCode);
110e41f4b71Sopenharmony_ci              return errCode;
111e41f4b71Sopenharmony_ci          }
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ci          // Translate the image.
114e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Translate(pixelmap, 50.0, 10.0);
115e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
116e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Translate failed, errCode: %{public}d.", errCode);
117e41f4b71Sopenharmony_ci              return errCode;
118e41f4b71Sopenharmony_ci          }
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci          // Rotate the image.
121e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Rotate(pixelmap, 90.0);
122e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
123e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Rotate failed, errCode: %{public}d.", errCode);
124e41f4b71Sopenharmony_ci              return errCode;
125e41f4b71Sopenharmony_ci          }
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci          // Flip the image.
128e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Flip(pixelmap, true, true);
129e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
130e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Flip failed, errCode: %{public}d.", errCode);
131e41f4b71Sopenharmony_ci              return errCode;
132e41f4b71Sopenharmony_ci          }
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci          // Crop the image.
135e41f4b71Sopenharmony_ci          Image_Region region;
136e41f4b71Sopenharmony_ci          region.x = 100;
137e41f4b71Sopenharmony_ci          region.y = 100;
138e41f4b71Sopenharmony_ci          region.width = 6;
139e41f4b71Sopenharmony_ci          region.height = 4;
140e41f4b71Sopenharmony_ci          errCode = OH_PixelmapNative_Crop(pixelmap, &region);
141e41f4b71Sopenharmony_ci          if (errCode != IMAGE_SUCCESS) {
142e41f4b71Sopenharmony_ci              OH_LOG_ERROR(LOG_APP, "ImagePixelmapNativeCTest pixelmapTest OH_PixelmapNative_Crop failed, errCode: %{public}d.", errCode);
143e41f4b71Sopenharmony_ci              return errCode;
144e41f4b71Sopenharmony_ci          }
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci          // Release the Pixelmap and InitializationOptions instances.
147e41f4b71Sopenharmony_ci          OH_PixelmapNative_Release(pixelmap);
148e41f4b71Sopenharmony_ci          OH_PixelmapInitializationOptions_Release(createOpts);
149e41f4b71Sopenharmony_ci          return IMAGE_SUCCESS;
150e41f4b71Sopenharmony_ci      }
151e41f4b71Sopenharmony_ci   ```
152