1# Image Transformation (C/C++) 2 3You will learn how to use native image APIs to process images. 4 5## How to Develop 6 7**Adding Dependencies** 8 9Open the **src/main/cpp/CMakeLists.txt** file of the native project, add **libpixelmap_ndk.z.so** of the image and **libhilog_ndk.z.so** of the log to the **target_link_libraries** dependency. 10 11```txt 12target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so) 13``` 14 15**Adding API Mappings** 16 17Open the **src/main/cpp/hello.cpp** file and add the following API mappings to the **Init** function: 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**Calling the Native APIs** 37 38For details about the APIs, see [Image API Reference](../reference/apis-image-kit/image.md). 39 40Obtain 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: 41 42Open **src/main/cpp/hello.cpp**, and add the reference file. 43```c++ 44#include<multimedia/image_framework/image_pixel_map_napi.h> 45``` 46 471. Obtain the **PixelMap** information and store the information to the **OhosPixelMapInfo** struct. 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. Obtain the memory address of a **PixelMap** object and lock the memory. 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. Unlock the memory of the **PixelMap** object. 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**Calling APIs on the JS Side** 102 1031. Open **src\main\cpp\types\*libentry*\index.d.ts** (where **libentry** varies according to the project name), and import the following files: 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. 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: 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 ``` 154