• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 图像变换(C/C++)
2
3开发者可以通过本指导了解如何使用Native Image的接口。
4
5## 开发步骤
6
7**添加依赖**
8
9在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加image的libpixelmap_ndk.z.so以及日志依赖libhilog_ndk.z.so10
11```txt
12target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so)
13```
14
15**添加接口映射**
16
17打开src/main/cpp/hello.cpp文件,在Init函数中添加接口映射如下:
18
19```c++
20EXTERN_C_START
21static napi_value Init(napi_env env, napi_value exports)
22{
23    napi_property_descriptor desc[] = {
24        { "testGetImageInfo", nullptr, TestGetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr },
25        { "testAccessPixels", nullptr, TestAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr },
26        { "testUnAccessPixels", nullptr, TestUnAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr },
27    };
28
29    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
30    return exports;
31}
32EXTERN_C_END
33```
34
35
36**Native接口调用**
37
38具体接口说明请参考[API文档](../../reference/apis-image-kit/image.md)
39
40hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下:
41
42打开src/main/cpp/hello.cpp,添加引用文件
43```c++
44#include<multimedia/image_framework/image_pixel_map_napi.h>
45```
46
471. 获取**PixelMap**的信息,并记录信息到**OhosPixelMapInfo**结构中。
48   ```c++
49   static napi_value TestGetImageInfo(napi_env env, napi_callback_info info)
50    {
51        napi_value result = nullptr;
52        napi_get_undefined(env, &result);
53
54        napi_value thisVar = nullptr;
55        napi_value argValue[1] = {0};
56        size_t argCount = 1;
57
58        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
59
60        OHOS::Media::OhosPixelMapInfo pixelMapInfo;
61        OHOS::Media::OH_GetImageInfo(env, argValue[0], &pixelMapInfo);
62        return result;
63    }
64    ```
652. 获取**PixelMap**对象数据的内存地址,并锁定该内存。
66    ```c++
67    static napi_value TestAccessPixels(napi_env env, napi_callback_info info)
68    {
69        napi_value result = nullptr;
70        napi_get_undefined(env, &result);
71
72        napi_value thisVar = nullptr;
73        napi_value argValue[1] = {0};
74        size_t argCount = 1;
75
76        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
77
78        void* addrPtr = nullptr;
79        OHOS::Media::OH_AccessPixels(env, argValue[0], &addrPtr);
80        return result;
81    }
82    ```
833. 释放**PixelMap**对象数据的内存锁。
84    ```c++
85    static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info)
86    {
87        napi_value result = nullptr;
88        napi_get_undefined(env, &result);
89
90        napi_value thisVar = nullptr;
91        napi_value argValue[1] = {0};
92        size_t argCount = 1;
93
94        napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr);
95
96        OHOS::Media::OH_UnAccessPixels(env, argValue[0]);
97        return result;
98    }
99    ```
100
101**JS侧调用**
102
1031. 打开src\main\cpp\types\libentry\index.d.ts(其中libentry根据工程名生成),导入如下引用文件:
104    ```js
105    import image from '@ohos.multimedia.image'
106    export const add:(a: number, b: number) => image.PixelMap;
107    export const transform: (a: image.PixelMap) => image.PixelMap;
108    export const testGetImageInfo: (a: image.PixelMap) => image.PixelMap;
109    export const testAccessPixels: (a: image.PixelMap) => image.PixelMap;
110    export const testUnAccessPixels: (a: image.PixelMap) => image.PixelMap;
111    ```
112
1132. 打开src\main\ets\pages\index.ets, 导入"libentry.so"(根据工程名生成);调用Native接口,传入JS的资源对象。示例如下:
114
115    ```js
116    import testNapi from 'libentry.so'
117    import image from '@ohos.multimedia.image';
118
119    @Entry
120    @Component
121    struct Index {
122    @State message: string = 'IMAGE'
123    @State _PixelMap : image.PixelMap | undefined = undefined;
124
125    build() {
126        Row() {
127        Column() {
128            Button(this.message)
129            .fontSize(50)
130            .fontWeight(FontWeight.Bold)
131            .onClick(() => {
132                const color : ArrayBuffer = new ArrayBuffer(96);
133                let opts: image.InitializationOptions = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 4, width: 6 } }
134                image.createPixelMap(color, opts)
135                .then( (pixelmap : image.PixelMap) => {
136                    this._PixelMap = pixelmap;
137                    testNapi.testGetImageInfo(this._PixelMap);
138                    console.info("Test GetImageInfo success");
139
140                    testNapi.testAccessPixels(this._PixelMap);
141                    console.info("Test AccessPixels success");
142
143                    testNapi.testUnAccessPixels(this._PixelMap);
144                    console.info("Test UnAccessPixels success");
145                })
146            })
147        }
148        .width('100%')
149        }
150        .height('100%')
151    }
152    }
153    ```