1e41f4b71Sopenharmony_ci# Using Image to Transform Images
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 image APIs depend) 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 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        { "testGetImageInfo", nullptr, TestGetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
25e41f4b71Sopenharmony_ci        { "testAccessPixels", nullptr, TestAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr },
26e41f4b71Sopenharmony_ci        { "testUnAccessPixels", nullptr, TestUnAccessPixels, 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](../../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. The sample code is as follows:
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciOpen **src/main/cpp/hello.cpp**, and add the reference file.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci```c++
44e41f4b71Sopenharmony_ci#include<multimedia/image_framework/image_pixel_map_napi.h>
45e41f4b71Sopenharmony_ci```
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci1. Obtain the **PixelMap** information and store the information to the **OhosPixelMapInfo** struct.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ci   ```c++
50e41f4b71Sopenharmony_ci   static napi_value TestGetImageInfo(napi_env env, napi_callback_info info)
51e41f4b71Sopenharmony_ci    {
52e41f4b71Sopenharmony_ci        napi_value result = nullptr;
53e41f4b71Sopenharmony_ci        napi_get_undefined(env, &result);
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci        napi_value thisVar = nullptr;
56e41f4b71Sopenharmony_ci        napi_value argValue[1] = {0};
57e41f4b71Sopenharmony_ci        size_t argCount = 1;
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
60e41f4b71Sopenharmony_ci        
61e41f4b71Sopenharmony_ci        OHOS::Media::OhosPixelMapInfo pixelMapInfo;
62e41f4b71Sopenharmony_ci        OHOS::Media::OH_GetImageInfo(env, argValue[0], &pixelMapInfo);
63e41f4b71Sopenharmony_ci        return result;
64e41f4b71Sopenharmony_ci    }
65e41f4b71Sopenharmony_ci   ```
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci2. Obtain the memory address of a **PixelMap** object and lock the memory.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci    ```c++
70e41f4b71Sopenharmony_ci    static napi_value TestAccessPixels(napi_env env, napi_callback_info info)
71e41f4b71Sopenharmony_ci    {
72e41f4b71Sopenharmony_ci        napi_value result = nullptr;
73e41f4b71Sopenharmony_ci        napi_get_undefined(env, &result);
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci        napi_value thisVar = nullptr;
76e41f4b71Sopenharmony_ci        napi_value argValue[1] = {0};
77e41f4b71Sopenharmony_ci        size_t argCount = 1;
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci        void* addrPtr = nullptr;
82e41f4b71Sopenharmony_ci        OHOS::Media::OH_AccessPixels(env, argValue[0], &addrPtr);
83e41f4b71Sopenharmony_ci        return result;
84e41f4b71Sopenharmony_ci    }
85e41f4b71Sopenharmony_ci    ```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci3. Unlock the memory of the **PixelMap** object.
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ci    ```c++
90e41f4b71Sopenharmony_ci    static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info)
91e41f4b71Sopenharmony_ci    {
92e41f4b71Sopenharmony_ci        napi_value result = nullptr;
93e41f4b71Sopenharmony_ci        napi_get_undefined(env, &result);
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci        napi_value thisVar = nullptr;
96e41f4b71Sopenharmony_ci        napi_value argValue[1] = {0};
97e41f4b71Sopenharmony_ci        size_t argCount = 1;
98e41f4b71Sopenharmony_ci
99e41f4b71Sopenharmony_ci        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci        OHOS::Media::OH_UnAccessPixels(env, argValue[0]);
102e41f4b71Sopenharmony_ci        return result;
103e41f4b71Sopenharmony_ci    }
104e41f4b71Sopenharmony_ci    ```
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci**Calling APIs on the JS Side**
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci1. Open **src\main\cpp\types\*libentry*\index.d.ts** (where **libentry** varies according to the project name), and import the following files:
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci    ```js
111e41f4b71Sopenharmony_ci    import { image } from '@kit.ImageKit';
112e41f4b71Sopenharmony_ci    export const add:(a: number, b: number) => image.PixelMap;
113e41f4b71Sopenharmony_ci    export const transform: (a: image.PixelMap) => image.PixelMap;
114e41f4b71Sopenharmony_ci    export const testGetImageInfo: (a: image.PixelMap) => image.PixelMap;
115e41f4b71Sopenharmony_ci    export const testAccessPixels: (a: image.PixelMap) => image.PixelMap;
116e41f4b71Sopenharmony_ci    export const testUnAccessPixels: (a: image.PixelMap) => image.PixelMap;
117e41f4b71Sopenharmony_ci    ```
118e41f4b71Sopenharmony_ci
119e41f4b71Sopenharmony_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:
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci    ```js
122e41f4b71Sopenharmony_ci    import testNapi from 'libentry.so'
123e41f4b71Sopenharmony_ci    import { image } from '@kit.ImageKit';
124e41f4b71Sopenharmony_ci
125e41f4b71Sopenharmony_ci    @Entry
126e41f4b71Sopenharmony_ci    @Component
127e41f4b71Sopenharmony_ci    struct Index {
128e41f4b71Sopenharmony_ci    @State message: string = 'IMAGE'
129e41f4b71Sopenharmony_ci    @State _PixelMap : image.PixelMap | undefined = undefined;
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci    build() {
132e41f4b71Sopenharmony_ci        Row() {
133e41f4b71Sopenharmony_ci        Column() {
134e41f4b71Sopenharmony_ci            Button(this.message)
135e41f4b71Sopenharmony_ci            .fontSize(50)
136e41f4b71Sopenharmony_ci            .fontWeight(FontWeight.Bold)
137e41f4b71Sopenharmony_ci            .onClick(() => {
138e41f4b71Sopenharmony_ci                const color : ArrayBuffer = new ArrayBuffer(96);
139e41f4b71Sopenharmony_ci                let opts: image.InitializationOptions = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 4, width: 6 } }
140e41f4b71Sopenharmony_ci                image.createPixelMap(color, opts)
141e41f4b71Sopenharmony_ci                .then( (pixelmap : image.PixelMap) => {
142e41f4b71Sopenharmony_ci                    this._PixelMap = pixelmap;
143e41f4b71Sopenharmony_ci                    testNapi.testGetImageInfo(this._PixelMap);
144e41f4b71Sopenharmony_ci                    console.info("Test GetImageInfo success");
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci                    testNapi.testAccessPixels(this._PixelMap);
147e41f4b71Sopenharmony_ci                    console.info("Test AccessPixels success");
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ci                    testNapi.testUnAccessPixels(this._PixelMap);
150e41f4b71Sopenharmony_ci                    console.info("Test UnAccessPixels success");
151e41f4b71Sopenharmony_ci                })
152e41f4b71Sopenharmony_ci            })
153e41f4b71Sopenharmony_ci        }
154e41f4b71Sopenharmony_ci        .width('100%')
155e41f4b71Sopenharmony_ci        }
156e41f4b71Sopenharmony_ci        .height('100%')
157e41f4b71Sopenharmony_ci    }
158e41f4b71Sopenharmony_ci    }
159e41f4b71Sopenharmony_ci    ```
160