# 使用Image完æˆå›¾åƒå˜æ¢ å¼€å‘者å¯ä»¥é€šè¿‡æœ¬æŒ‡å¯¼äº†è§£å¦‚何使用Native Image的接å£ã€‚ ## 开呿¥éª¤ **æ·»åŠ ä¾èµ–** 在进行应用开å‘之å‰ï¼Œå¼€å‘è€…éœ€è¦æ‰“å¼€native工程的src/main/cpp/CMakeLists.txt,在target_link_librariesä¾èµ–䏿·»åŠ imageçš„libace_napi.z.soã€libpixelmap_ndk.z.soä»¥åŠæ—¥å¿—ä¾èµ–libhilog_ndk.z.so。 ```txt target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so) ``` **æ·»åŠ æŽ¥å£æ˜ å°„** 打开src/main/cpp/hello.cpp文件,在Initå‡½æ•°ä¸æ·»åŠ æŽ¥å£æ˜ 射如下: ```c++ EXTERN_C_START static napi_value Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { { "testGetImageInfo", nullptr, TestGetImageInfo, nullptr, nullptr, nullptr, napi_default, nullptr }, { "testAccessPixels", nullptr, TestAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr }, { "testUnAccessPixels", nullptr, TestUnAccessPixels, nullptr, nullptr, nullptr, napi_default, nullptr }, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; } EXTERN_C_END ``` **Native接å£è°ƒç”¨** 具体接å£è¯´æ˜Žè¯·å‚考[API文档](../../reference/apis-image-kit/image.md)。 在hello.cpp文件ä¸èŽ·å–JS的资æºå¯¹è±¡ï¼Œå¹¶è½¬ä¸ºNative的资æºå¯¹è±¡ï¼Œå³å¯è°ƒç”¨Native接å£ï¼Œè°ƒç”¨æ–¹å¼ç¤ºä¾‹ä»£ç 如下: 打开src/main/cpp/hello.cppï¼Œæ·»åŠ å¼•ç”¨æ–‡ä»¶ã€‚ ```c++ #include<multimedia/image_framework/image_pixel_map_napi.h> ``` 1. 获å–**PixelMap**的信æ¯ï¼Œå¹¶è®°å½•ä¿¡æ¯åˆ°**OhosPixelMapInfo**结构ä¸ã€‚ ```c++ static napi_value TestGetImageInfo(napi_env env, napi_callback_info info) { napi_value result = nullptr; napi_get_undefined(env, &result); napi_value thisVar = nullptr; napi_value argValue[1] = {0}; size_t argCount = 1; napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); OHOS::Media::OhosPixelMapInfo pixelMapInfo; OHOS::Media::OH_GetImageInfo(env, argValue[0], &pixelMapInfo); return result; } ``` 2. 获å–**PixelMap**对象数æ®çš„内å˜åœ°å€ï¼Œå¹¶é”定该内å˜ã€‚ ```c++ static napi_value TestAccessPixels(napi_env env, napi_callback_info info) { napi_value result = nullptr; napi_get_undefined(env, &result); napi_value thisVar = nullptr; napi_value argValue[1] = {0}; size_t argCount = 1; napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); void* addrPtr = nullptr; OHOS::Media::OH_AccessPixels(env, argValue[0], &addrPtr); return result; } ``` 3. 释放**PixelMap**对象数æ®çš„内å˜é”。 ```c++ static napi_value TestUnAccessPixels(napi_env env, napi_callback_info info) { napi_value result = nullptr; napi_get_undefined(env, &result); napi_value thisVar = nullptr; napi_value argValue[1] = {0}; size_t argCount = 1; napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr); OHOS::Media::OH_UnAccessPixels(env, argValue[0]); return result; } ``` **JS侧调用** 1. 打开src\main\cpp\types\libentry\index.d.ts(å…¶ä¸libentryæ ¹æ®å·¥ç¨‹å生æˆ),导入如下引用文件: ```js import { image } from '@kit.ImageKit'; export const add:(a: number, b: number) => image.PixelMap; export const transform: (a: image.PixelMap) => image.PixelMap; export const testGetImageInfo: (a: image.PixelMap) => image.PixelMap; export const testAccessPixels: (a: image.PixelMap) => image.PixelMap; export const testUnAccessPixels: (a: image.PixelMap) => image.PixelMap; ``` 2. 打开src\main\ets\pages\index.ets, 导入"libentry.so"(æ ¹æ®å·¥ç¨‹å生æˆ);调用Native接å£ï¼Œä¼ å…¥JS的资æºå¯¹è±¡ã€‚示例如下: ```js import testNapi from 'libentry.so' import { image } from '@kit.ImageKit'; @Entry @Component struct Index { @State message: string = 'IMAGE' @State _PixelMap : image.PixelMap | undefined = undefined; build() { Row() { Column() { Button(this.message) .fontSize(50) .fontWeight(FontWeight.Bold) .onClick(() => { const color : ArrayBuffer = new ArrayBuffer(96); let opts: image.InitializationOptions = { alphaType: 0, editable: true, pixelFormat: 4, scaleMode: 1, size: { height: 4, width: 6 } } image.createPixelMap(color, opts) .then( (pixelmap : image.PixelMap) => { this._PixelMap = pixelmap; testNapi.testGetImageInfo(this._PixelMap); console.info("Test GetImageInfo success"); testNapi.testAccessPixels(this._PixelMap); console.info("Test AccessPixels success"); testNapi.testUnAccessPixels(this._PixelMap); console.info("Test UnAccessPixels success"); }) }) } .width('100%') } .height('100%') } } ```