1e41f4b71Sopenharmony_ci# Using Image to Process PixelMap Data
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciYou will learn how to use native image APIs to process images.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci## How to Develop
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Adding Dependencies**
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciOpen the **src/main/cpp/CMakeLists.txt** file of the native project, add **libace_napi.z.so** and **libpixelmap_ndk.z.so** (on both of which the native image APIs depend) and **libhilog_ndk.z.so** (on which the native log APIs depend) to the **target_link_libraries** dependency.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```txt
12e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so)
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci**Adding API Mappings**
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ciOpen the **src/main/cpp/hello.cpp** file and add the following API mappings to the **Init** function:
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci```c++
20e41f4b71Sopenharmony_ciEXTERN_C_START
21e41f4b71Sopenharmony_cistatic napi_value Init(napi_env env, napi_value exports)
22e41f4b71Sopenharmony_ci{
23e41f4b71Sopenharmony_ci    napi_property_descriptor desc[] = {
24e41f4b71Sopenharmony_ci        { "createPixelMapTest", nullptr, CreatePixelMapTest, nullptr, nullptr, nullptr, napi_default, nullptr },
25e41f4b71Sopenharmony_ci        { "createAlphaPixelMap", nullptr, CreateAlphaPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr },
26e41f4b71Sopenharmony_ci        { "transform", nullptr, Transform, nullptr, nullptr, nullptr, napi_default, nullptr },
27e41f4b71Sopenharmony_ci    };
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ci    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
30e41f4b71Sopenharmony_ci    return exports;
31e41f4b71Sopenharmony_ci}
32e41f4b71Sopenharmony_ciEXTERN_C_END
33e41f4b71Sopenharmony_ci```
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci**Calling the Native APIs**
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ciFor details about the APIs, see [Image API Reference](../../reference/apis-image-kit/image.md).
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciObtain the JS resource object from the **hello.cpp** file and convert it to a native resource object. Then you can call native APIs.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci**Adding Reference Files**
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci```c++
44e41f4b71Sopenharmony_ci#include <multimedia/image_framework/image_mdk_common.h>
45e41f4b71Sopenharmony_ci#include <multimedia/image_framework/image_pixel_map_mdk.h>
46e41f4b71Sopenharmony_ci#include <stdlib.h>
47e41f4b71Sopenharmony_ci```
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci1. Create a **PixelMap** object.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ci    ```c++
52e41f4b71Sopenharmony_ci    napi_value CreatePixelMapTest(napi_env env, napi_callback_info info) {
53e41f4b71Sopenharmony_ci        napi_value udfVar = nullptr;
54e41f4b71Sopenharmony_ci        napi_value pixelMap = nullptr;
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ci        struct OhosPixelMapCreateOps createOps;
57e41f4b71Sopenharmony_ci        createOps.width = 4;
58e41f4b71Sopenharmony_ci        createOps.height = 6;
59e41f4b71Sopenharmony_ci        createOps.pixelFormat = 4;
60e41f4b71Sopenharmony_ci        createOps.alphaType = 0;
61e41f4b71Sopenharmony_ci        size_t bufferSize = createOps.width * createOps.height * 4;
62e41f4b71Sopenharmony_ci        void *buff = malloc(bufferSize);
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci        char *cc = (char *)buff;
65e41f4b71Sopenharmony_ci        for (int i = 0; i < 96; i++) {
66e41f4b71Sopenharmony_ci            *(cc++) = (char)i;
67e41f4b71Sopenharmony_ci        }
68e41f4b71Sopenharmony_ci        int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, (uint8_t *)buff, bufferSize, &pixelMap);
69e41f4b71Sopenharmony_ci        if (res != IMAGE_RESULT_SUCCESS || pixelMap == nullptr) {
70e41f4b71Sopenharmony_ci            return udfVar;
71e41f4b71Sopenharmony_ci        }
72e41f4b71Sopenharmony_ci        return pixelMap;
73e41f4b71Sopenharmony_ci    }
74e41f4b71Sopenharmony_ci    ```
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci2. Create a **PixelMap** object that contains only alpha channel information.
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci    ```c++
79e41f4b71Sopenharmony_ci    napi_value CreateAlphaPixelMap(napi_env env, napi_callback_info info) {
80e41f4b71Sopenharmony_ci        napi_value udfVar = nullptr;
81e41f4b71Sopenharmony_ci        napi_value thisVar = nullptr;
82e41f4b71Sopenharmony_ci        napi_value argValue[1] = {0};
83e41f4b71Sopenharmony_ci        size_t argCount = 1;
84e41f4b71Sopenharmony_ci
85e41f4b71Sopenharmony_ci        napi_value alphaPixelMap = nullptr;
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci        napi_get_undefined(env, &udfVar);
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci        if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
90e41f4b71Sopenharmony_ci            argValue[0] == nullptr) {
91e41f4b71Sopenharmony_ci            return udfVar;
92e41f4b71Sopenharmony_ci        }
93e41f4b71Sopenharmony_ci        int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[0], &alphaPixelMap);
94e41f4b71Sopenharmony_ci        if (res != IMAGE_RESULT_SUCCESS || alphaPixelMap == nullptr) {
95e41f4b71Sopenharmony_ci            return udfVar;
96e41f4b71Sopenharmony_ci        }
97e41f4b71Sopenharmony_ci        return alphaPixelMap;
98e41f4b71Sopenharmony_ci    }
99e41f4b71Sopenharmony_ci    ```
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci3. Process the **PixelMap** object.
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci    ```c++
104e41f4b71Sopenharmony_ci    napi_value Transform(napi_env env, napi_callback_info info) {
105e41f4b71Sopenharmony_ci        napi_value thisVar = nullptr;
106e41f4b71Sopenharmony_ci        napi_value argValue[1] = {0};
107e41f4b71Sopenharmony_ci        size_t argCount = 1;
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci        if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
110e41f4b71Sopenharmony_ci            argValue[0] == nullptr) {
111e41f4b71Sopenharmony_ci            return nullptr;
112e41f4b71Sopenharmony_ci        }
113e41f4b71Sopenharmony_ci        napi_value result = nullptr;
114e41f4b71Sopenharmony_ci        napi_get_undefined(env, &result);
115e41f4b71Sopenharmony_ci        
116e41f4b71Sopenharmony_ci        // Initialize the PixelMap object.
117e41f4b71Sopenharmony_ci        NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue[0]);
118e41f4b71Sopenharmony_ci        if (native == nullptr) {
119e41f4b71Sopenharmony_ci            return result;
120e41f4b71Sopenharmony_ci        }
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci        // Obtain image information.
123e41f4b71Sopenharmony_ci        struct OhosPixelMapInfos pixelMapInfo;
124e41f4b71Sopenharmony_ci        OH_PixelMap_GetImageInfo(native, &pixelMapInfo);
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci        // Obtain the number of bytes per row of the PixelMap object.
127e41f4b71Sopenharmony_ci        int32_t rowBytes;
128e41f4b71Sopenharmony_ci        OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes);
129e41f4b71Sopenharmony_ci
130e41f4b71Sopenharmony_ci        // Check whether the PixelMap object is editable.
131e41f4b71Sopenharmony_ci        int32_t editable = 0;
132e41f4b71Sopenharmony_ci        OH_PixelMap_GetIsEditable(native, &editable);
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci        // Check whether the PixelMap object supports alpha channels.
135e41f4b71Sopenharmony_ci        int32_t supportAlpha = 0;
136e41f4b71Sopenharmony_ci        OH_PixelMap_IsSupportAlpha(native, &supportAlpha);
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci        // Set an alpha channel for the PixelMap object.
139e41f4b71Sopenharmony_ci        int32_t alphaAble = 0;
140e41f4b71Sopenharmony_ci        OH_PixelMap_SetAlphaAble(native, alphaAble);
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci        // Obtain the pixel density of the PixelMap object.
143e41f4b71Sopenharmony_ci        int32_t densityG;
144e41f4b71Sopenharmony_ci        OH_PixelMap_GetDensity(native, &densityG);
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci        // Set the pixel density for the PixelMap object.
147e41f4b71Sopenharmony_ci        int32_t densityS = 100;
148e41f4b71Sopenharmony_ci        OH_PixelMap_SetDensity(native, densityS);
149e41f4b71Sopenharmony_ci
150e41f4b71Sopenharmony_ci        // Set the opacity for the PixelMap object.
151e41f4b71Sopenharmony_ci        float opacity = 0.5;
152e41f4b71Sopenharmony_ci        OH_PixelMap_SetOpacity(native, opacity);
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci        // Scale the image.
155e41f4b71Sopenharmony_ci        // scaleX: The width of the image after scaling is 0.5 of the original width.
156e41f4b71Sopenharmony_ci        // scaleY: The height of the image after scaling is 0.5 of the original height.
157e41f4b71Sopenharmony_ci        float scaleX = 0.5;
158e41f4b71Sopenharmony_ci        float scaleY = 0.5;
159e41f4b71Sopenharmony_ci        OH_PixelMap_Scale(native, scaleX, scaleY);
160e41f4b71Sopenharmony_ci
161e41f4b71Sopenharmony_ci        // Translate the image.
162e41f4b71Sopenharmony_ci        // translateX: Translate the image by 50 units downwards.
163e41f4b71Sopenharmony_ci        // translateY: Translate the image by 50 units rightwards.
164e41f4b71Sopenharmony_ci        float translateX = 50;
165e41f4b71Sopenharmony_ci        float translateY = 50;
166e41f4b71Sopenharmony_ci        OH_PixelMap_Translate(native, translateX, translateY);
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci        // Rate the image clockwise by 90°.
169e41f4b71Sopenharmony_ci        float angle = 90;
170e41f4b71Sopenharmony_ci        OH_PixelMap_Rotate(native, angle);
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci        // Flip the image.
173e41f4b71Sopenharmony_ci        // flipX: whether to flip the image horizontally. The value 1 means to flip the image and 0 means the opposite.
174e41f4b71Sopenharmony_ci        // flipY: whether to flip the image vertically. The value 1 means to flip the image and 0 means the opposite.
175e41f4b71Sopenharmony_ci        int32_t flipX = 0;
176e41f4b71Sopenharmony_ci        int32_t flipY = 1;
177e41f4b71Sopenharmony_ci        OH_PixelMap_Flip(native, flipX, flipY);
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci        // Crop the image.
180e41f4b71Sopenharmony_ci        // cropX: x-axis coordinate of the start point for cropping.
181e41f4b71Sopenharmony_ci        // cropY: y-axis coordinate of the start point for cropping.
182e41f4b71Sopenharmony_ci        // cropH: height after cropping (10), cropping from top to bottom.
183e41f4b71Sopenharmony_ci        // cropW: width after cropping (10), cropping from left to right.
184e41f4b71Sopenharmony_ci        int32_t cropX = 1;
185e41f4b71Sopenharmony_ci        int32_t cropY = 1;
186e41f4b71Sopenharmony_ci        int32_t cropW = 10;
187e41f4b71Sopenharmony_ci        int32_t cropH = 10;
188e41f4b71Sopenharmony_ci        OH_PixelMap_Crop(native, cropX, cropY, cropW, cropH);
189e41f4b71Sopenharmony_ci
190e41f4b71Sopenharmony_ci        // Obtain the memory address of the PixelMap object and lock the memory.
191e41f4b71Sopenharmony_ci        void *pixelAddr = nullptr;
192e41f4b71Sopenharmony_ci        OH_PixelMap_AccessPixels(native, &pixelAddr);
193e41f4b71Sopenharmony_ci
194e41f4b71Sopenharmony_ci        // Unlock the memory of the PixelMap object.
195e41f4b71Sopenharmony_ci        OH_PixelMap_UnAccessPixels(native);
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci        return result;
198e41f4b71Sopenharmony_ci    }
199e41f4b71Sopenharmony_ci    ```
200e41f4b71Sopenharmony_ci
201e41f4b71Sopenharmony_ci**Calling APIs on the JS Side**
202e41f4b71Sopenharmony_ci
203e41f4b71Sopenharmony_ci1. Open **src\main\cpp\types\*libentry*\index.d.ts** (where **libentry** varies according to the project name), and import the following files:
204e41f4b71Sopenharmony_ci
205e41f4b71Sopenharmony_ci    ```js
206e41f4b71Sopenharmony_ci    import { image } from '@kit.ImageKit';
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci    export const createPixelMapTest: () => image.PixelMap;
209e41f4b71Sopenharmony_ci    export const transform: (a: image.PixelMap) => image.PixelMap;
210e41f4b71Sopenharmony_ci    ```
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci2. Open **src\main\ets\pages\index.ets**, import ***libentry*.so** (where **libentry** varies according to the project name), call the native APIs, and pass in the JS resource object. The sample code is as follows:
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci    ```js
215e41f4b71Sopenharmony_ci    import testNapi from 'libentry.so'
216e41f4b71Sopenharmony_ci    import { image } from '@kit.ImageKit';
217e41f4b71Sopenharmony_ci
218e41f4b71Sopenharmony_ci    @Entry
219e41f4b71Sopenharmony_ci    @Component
220e41f4b71Sopenharmony_ci    struct Index {
221e41f4b71Sopenharmony_ci    @State _PixelMap : image.PixelMap | undefined = undefined;
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci    build() {
224e41f4b71Sopenharmony_ci        Row() {
225e41f4b71Sopenharmony_ci            Column() {
226e41f4b71Sopenharmony_ci                Button("PixelMap")
227e41f4b71Sopenharmony_ci                .width(100)
228e41f4b71Sopenharmony_ci                .height(100)
229e41f4b71Sopenharmony_ci                .onClick(() => {
230e41f4b71Sopenharmony_ci                    console.log("com.example.native_ndk_api10 button click in");
231e41f4b71Sopenharmony_ci                    this._pixelMap = testNapi.createPixelMapTest();
232e41f4b71Sopenharmony_ci                    testNapi.transform(this._pixelMap);
233e41f4b71Sopenharmony_ci                })
234e41f4b71Sopenharmony_ci                Image(this._pixelMap)
235e41f4b71Sopenharmony_ci                .width(500)
236e41f4b71Sopenharmony_ci                .height(500)
237e41f4b71Sopenharmony_ci                .objectFit(ImageFit.Cover)
238e41f4b71Sopenharmony_ci                .border({width: 1, color: Color.Blue})
239e41f4b71Sopenharmony_ci                }
240e41f4b71Sopenharmony_ci                .width('100%')
241e41f4b71Sopenharmony_ci            }
242e41f4b71Sopenharmony_ci            .height('100%')
243e41f4b71Sopenharmony_ci        }
244e41f4b71Sopenharmony_ci    }
245e41f4b71Sopenharmony_ci    ```
246