1# 使用Image处理PixelMap数据 2 3开发者可以通过本指导了解如何使用Native Image的接口。 4 5## 开发步骤 6 7**添加依赖** 8 9在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加image的libace_napi.z.so、libpixelmap_ndk.z.so以及日志依赖libhilog_ndk.z.so。 10 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 { "createPixelMapTest", nullptr, CreatePixelMapTest, nullptr, nullptr, nullptr, napi_default, nullptr }, 25 { "createAlphaPixelMap", nullptr, CreateAlphaPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr }, 26 { "transform", nullptr, Transform, 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**Native接口调用** 36 37具体接口说明请参考[API文档](../../reference/apis-image-kit/image.md)。 38 39在hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下: 40 41**添加引用文件** 42 43```c++ 44#include <multimedia/image_framework/image_mdk_common.h> 45#include <multimedia/image_framework/image_pixel_map_mdk.h> 46#include <stdlib.h> 47``` 48 491. 创建一个 **PixelMap** 对象。 50 51 ```c++ 52 napi_value CreatePixelMapTest(napi_env env, napi_callback_info info) { 53 napi_value udfVar = nullptr; 54 napi_value pixelMap = nullptr; 55 56 struct OhosPixelMapCreateOps createOps; 57 createOps.width = 4; 58 createOps.height = 6; 59 createOps.pixelFormat = 4; 60 createOps.alphaType = 0; 61 size_t bufferSize = createOps.width * createOps.height * 4; 62 void *buff = malloc(bufferSize); 63 if (buff == nullptr) { 64 return udfVar; 65 } 66 67 char *cc = (char *)buff; 68 for (int i = 0; i < 96; i++) { 69 *(cc++) = (char)i; 70 } 71 int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, (uint8_t *)buff, bufferSize, &pixelMap); 72 free(buff); 73 if (res != IMAGE_RESULT_SUCCESS || pixelMap == nullptr) { 74 return udfVar; 75 } 76 return pixelMap; 77 } 78 ``` 79 802. 根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的 **PixelMap** 对象。 81 82 ```c++ 83 napi_value CreateAlphaPixelMap(napi_env env, napi_callback_info info) { 84 napi_value udfVar = nullptr; 85 napi_value thisVar = nullptr; 86 napi_value argValue[1] = {0}; 87 size_t argCount = 1; 88 89 napi_value alphaPixelMap = nullptr; 90 91 napi_get_undefined(env, &udfVar); 92 93 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 || 94 argValue[0] == nullptr) { 95 return udfVar; 96 } 97 int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[0], &alphaPixelMap); 98 if (res != IMAGE_RESULT_SUCCESS || alphaPixelMap == nullptr) { 99 return udfVar; 100 } 101 return alphaPixelMap; 102 } 103 ``` 104 1053. 对 **PixelMap** 数据进行处理。 106 107 ```c++ 108 napi_value Transform(napi_env env, napi_callback_info info) { 109 napi_value thisVar = nullptr; 110 napi_value argValue[1] = {0}; 111 size_t argCount = 1; 112 113 if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 || 114 argValue[0] == nullptr) { 115 return nullptr; 116 } 117 napi_value result = nullptr; 118 napi_get_undefined(env, &result); 119 120 // 初始化NativePixelMap对象。 121 NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue[0]); 122 if (native == nullptr) { 123 return result; 124 } 125 126 // 获取图片信息。 127 struct OhosPixelMapInfos pixelMapInfo; 128 OH_PixelMap_GetImageInfo(native, &pixelMapInfo); 129 130 // 获取PixelMap对象每行字节数。 131 int32_t rowBytes; 132 OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes); 133 134 // 获取PixelMap对象是否可编辑的状态。 135 int32_t editable = 0; 136 OH_PixelMap_GetIsEditable(native, &editable); 137 138 // 获取PixelMap对象是否支持Alpha通道。 139 int32_t supportAlpha = 0; 140 OH_PixelMap_IsSupportAlpha(native, &supportAlpha); 141 142 // 设置PixelMap对象的Alpha通道。 143 int32_t alphaAble = 0; 144 OH_PixelMap_SetAlphaAble(native, alphaAble); 145 146 // 获取PixelMap对象像素密度。 147 int32_t densityG; 148 OH_PixelMap_GetDensity(native, &densityG); 149 150 // 设置PixelMap对象像素密度。 151 int32_t densityS = 100; 152 OH_PixelMap_SetDensity(native, densityS); 153 154 // 设置PixelMap对象的透明度。 155 float opacity = 0.5; 156 OH_PixelMap_SetOpacity(native, opacity); 157 158 // 设置缩放比例。 159 // scaleX: 宽为原来的0.5。 160 // scaleY: 高为原来的0.5。 161 float scaleX = 0.5; 162 float scaleY = 0.5; 163 OH_PixelMap_Scale(native, scaleX, scaleY); 164 165 // 设置偏移。 166 // translateX: 向下偏移50。 167 // translateY: 向右偏移50。 168 float translateX = 50; 169 float translateY = 50; 170 OH_PixelMap_Translate(native, translateX, translateY); 171 172 // 设置顺时针旋转90度。 173 float angle = 90; 174 OH_PixelMap_Rotate(native, angle); 175 176 // 设置翻转 177 // flipX: 水平翻转,0为不翻转,1为翻转。 178 // flipY: 垂直翻转,0为不翻转,1为翻转。 179 int32_t flipX = 0; 180 int32_t flipY = 1; 181 OH_PixelMap_Flip(native, flipX, flipY); 182 183 // 设置裁剪区域。 184 // cropX: 裁剪起始点横坐标。 185 // cropY: 裁剪起始点纵坐标。 186 // cropH: 裁剪高度10,方向为从上往下(裁剪后的图片高度为10)。 187 // cropW: 裁剪宽度10,方向为从左到右(裁剪后的图片宽度为10)。 188 int32_t cropX = 1; 189 int32_t cropY = 1; 190 int32_t cropW = 10; 191 int32_t cropH = 10; 192 OH_PixelMap_Crop(native, cropX, cropY, cropW, cropH); 193 194 // 获取PixelMap对象数据的内存地址,并锁定该内存。 195 void *pixelAddr = nullptr; 196 OH_PixelMap_AccessPixels(native, &pixelAddr); 197 198 // 释放PixelMap对象数据的内存锁。 199 OH_PixelMap_UnAccessPixels(native); 200 201 return result; 202 } 203 ``` 204 205**JS侧调用** 206 2071. 打开src\main\cpp\types\libentry\index.d.ts(其中libentry根据工程名生成),导入如下引用文件: 208 209 ```js 210 import { image } from '@kit.ImageKit'; 211 212 export const createPixelMapTest: () => image.PixelMap; 213 export const transform: (a: image.PixelMap) => void; 214 ``` 215 2162. 打开src\main\ets\pages\index.ets, 导入"libentry.so"(根据工程名生成),调用Native接口,传入JS的资源对象。示例如下: 217 218 ```js 219 import testNapi from 'libentry.so'; 220 import { image } from '@kit.ImageKit'; 221 222 @Entry 223 @Component 224 struct Index { 225 @State _pixelMap : image.PixelMap | undefined = undefined; 226 227 build() { 228 Row() { 229 Column() { 230 Button("PixelMap") 231 .width(100) 232 .height(100) 233 .onClick(() => { 234 console.log("com.example.native_ndk_api10 button click in"); 235 this._pixelMap = testNapi.createPixelMapTest(); 236 testNapi.transform(this._pixelMap); 237 }) 238 Image(this._pixelMap) 239 .width(500) 240 .height(500) 241 .objectFit(ImageFit.Cover) 242 .border({width: 1, color: Color.Blue}) 243 } 244 .width('100%') 245 } 246 .height('100%') 247 } 248 } 249 ``` 250