鸿蒙HarmonyOS实战开发:图片处理>使用Image处理PixelMap数据场景 ...

莱莱  金牌会员 | 2025-1-20 04:32:27 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 876|帖子 876|积分 2628

 鸿蒙NEXT开发实战往期必看文章:
一分钟了解”纯血版!鸿蒙HarmonyOS Next应用开发!
“非常详细的” 鸿蒙HarmonyOS Next应用开发学习路线!(从零基础入门到醒目)
HarmonyOS NEXT应用开发案例实践总结合(持续更新......)
HarmonyOS NEXT应用开发性能优化实践总结(持续更新......)

开发者可以通过本指导了解如何使用Native Image的接口。
开发步骤

添加依赖
在进行应用开发之前,开发者必要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加image的libace_napi.z.so、libpixelmap_ndk.z.so以及日记依赖libhilog_ndk.z.so。
  1. target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so)
复制代码
添加接口映射
打开src/main/cpp/hello.cpp文件,在Init函数中添加接口映射如下:
  1. EXTERN_C_START
  2. static napi_value Init(napi_env env, napi_value exports)
  3. {
  4.     napi_property_descriptor desc[] = {
  5.         { "createPixelMapTest", nullptr, CreatePixelMapTest, nullptr, nullptr, nullptr, napi_default, nullptr },
  6.         { "createAlphaPixelMap", nullptr, CreateAlphaPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr },
  7.         { "transform", nullptr, Transform, nullptr, nullptr, nullptr, napi_default, nullptr },
  8.     };
  9.     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
  10.     return exports;
  11. }
  12. EXTERN_C_END
复制代码
Native接口调用
具体接口阐明请参考API文档。
在hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下:
添加引用文件
  1. #include <multimedia/image_framework/image_mdk_common.h>
  2. #include <multimedia/image_framework/image_pixel_map_mdk.h>
  3. #include <stdlib.h>
复制代码

  • 创建一个 PixelMap 对象。
    1. napi_value CreatePixelMapTest(napi_env env, napi_callback_info info) {
    2.     napi_value udfVar = nullptr;
    3.     napi_value pixelMap = nullptr;
    4.     struct OhosPixelMapCreateOps createOps;
    5.     createOps.width = 4;
    6.     createOps.height = 6;
    7.     createOps.pixelFormat = 4;
    8.     createOps.alphaType = 0;
    9.     size_t bufferSize = createOps.width * createOps.height * 4;
    10.     void *buff = malloc(bufferSize);
    11.     if (buff == nullptr) {
    12.         return udfVar;
    13.     }
    14.     char *cc = (char *)buff;
    15.     for (int i = 0; i < 96; i++) {
    16.         *(cc++) = (char)i;
    17.     }
    18.     int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, (uint8_t *)buff, bufferSize, &pixelMap);
    19.     free(buff);
    20.     if (res != IMAGE_RESULT_SUCCESS || pixelMap == nullptr) {
    21.         return udfVar;
    22.     }
    23.     return pixelMap;
    24. }
    复制代码
  • 根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的 PixelMap 对象。
    1. napi_value CreateAlphaPixelMap(napi_env env, napi_callback_info info) {
    2.     napi_value udfVar = nullptr;
    3.     napi_value thisVar = nullptr;
    4.     napi_value argValue[1] = {0};
    5.     size_t argCount = 1;
    6.     napi_value alphaPixelMap = nullptr;
    7.     napi_get_undefined(env, &udfVar);
    8.     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
    9.         argValue[0] == nullptr) {
    10.         return udfVar;
    11.     }
    12.     int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[0], &alphaPixelMap);
    13.     if (res != IMAGE_RESULT_SUCCESS || alphaPixelMap == nullptr) {
    14.         return udfVar;
    15.     }
    16.     return alphaPixelMap;
    17. }
    复制代码
  • 对 PixelMap 数据进行处理。
    1. napi_value Transform(napi_env env, napi_callback_info info) {
    2.     napi_value thisVar = nullptr;
    3.     napi_value argValue[1] = {0};
    4.     size_t argCount = 1;
    5.     if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
    6.         argValue[0] == nullptr) {
    7.         return nullptr;
    8.     }
    9.     napi_value result = nullptr;
    10.     napi_get_undefined(env, &result);
    11.    
    12.     // 初始化NativePixelMap对象。
    13.     NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue[0]);
    14.     if (native == nullptr) {
    15.         return result;
    16.     }
    17.     // 获取图片信息。
    18.     struct OhosPixelMapInfos pixelMapInfo;
    19.     OH_PixelMap_GetImageInfo(native, &pixelMapInfo);
    20.     // 获取PixelMap对象每行字节数。
    21.     int32_t rowBytes;
    22.     OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes);
    23.     // 获取PixelMap对象是否可编辑的状态。
    24.     int32_t editable = 0;
    25.     OH_PixelMap_GetIsEditable(native, &editable);
    26.     // 获取PixelMap对象是否支持Alpha通道。
    27.     int32_t supportAlpha = 0;
    28.     OH_PixelMap_IsSupportAlpha(native, &supportAlpha);
    29.     // 设置PixelMap对象的Alpha通道。
    30.     int32_t alphaAble = 0;
    31.     OH_PixelMap_SetAlphaAble(native, alphaAble);
    32.     // 获取PixelMap对象像素密度。
    33.     int32_t densityG;
    34.     OH_PixelMap_GetDensity(native, &densityG);
    35.     // 设置PixelMap对象像素密度。
    36.     int32_t densityS = 100;
    37.     OH_PixelMap_SetDensity(native, densityS);
    38.     // 设置PixelMap对象的透明度。
    39.     float opacity = 0.5;
    40.     OH_PixelMap_SetOpacity(native, opacity);
    41.     // 设置缩放比例。
    42.     // scaleX: 宽为原来的0.5。
    43.     // scaleY: 高为原来的0.5。
    44.     float scaleX = 0.5;
    45.     float scaleY = 0.5;
    46.     OH_PixelMap_Scale(native, scaleX, scaleY);
    47.     // 设置偏移。
    48.     // translateX: 向下偏移50。
    49.     // translateY: 向右偏移50。
    50.     float translateX = 50;
    51.     float translateY = 50;
    52.     OH_PixelMap_Translate(native, translateX, translateY);
    53.     // 设置顺时针旋转90度。
    54.     float angle = 90;
    55.     OH_PixelMap_Rotate(native, angle);
    56.     // 设置翻转
    57.     // flipX: 水平翻转,0为不翻转,1为翻转。
    58.     // flipY: 垂直翻转,0为不翻转,1为翻转。
    59.     int32_t flipX = 0;
    60.     int32_t flipY = 1;
    61.     OH_PixelMap_Flip(native, flipX, flipY);
    62.     // 设置裁剪区域。
    63.     // cropX: 裁剪起始点横坐标。
    64.     // cropY: 裁剪起始点纵坐标。
    65.     // cropH: 裁剪高度10,方向为从上往下(裁剪后的图片高度为10)。
    66.     // cropW: 裁剪宽度10,方向为从左到右(裁剪后的图片宽度为10)。
    67.     int32_t cropX = 1;
    68.     int32_t cropY = 1;
    69.     int32_t cropW = 10;
    70.     int32_t cropH = 10;
    71.     OH_PixelMap_Crop(native, cropX, cropY, cropW, cropH);
    72.     // 获取PixelMap对象数据的内存地址,并锁定该内存。
    73.     void *pixelAddr = nullptr;
    74.     OH_PixelMap_AccessPixels(native, &pixelAddr);
    75.     // 释放PixelMap对象数据的内存锁。
    76.     OH_PixelMap_UnAccessPixels(native);
    77.     return result;
    78. }
    复制代码
JS侧调用

  • 打开src\main\cpp\types\libentry\index.d.ts(其中libentry根据工程名生成),导入如下引用文件:
    1. import { image } from '@kit.ImageKit';
    2. export const createPixelMapTest: () => image.PixelMap;
    3. export const transform: (a: image.PixelMap) => void;
    复制代码
  • 打开src\main\ets\pages\index.ets, 导入"libentry.so"(根据工程名生成),调用Native接口,传入JS的资源对象。示比方下:
    1. import testNapi from 'libentry.so';
    2. import { image } from '@kit.ImageKit';
    3. @Entry
    4. @Component
    5. struct Index {
    6. @State _pixelMap : image.PixelMap | undefined = undefined;
    7. build() {
    8.     Row() {
    9.         Column() {
    10.             Button("PixelMap")
    11.             .width(100)
    12.             .height(100)
    13.             .onClick(() => {
    14.                 console.log("com.example.native_ndk_api10 button click in");
    15.                 this._pixelMap = testNapi.createPixelMapTest();
    16.                 testNapi.transform(this._pixelMap);
    17.             })
    18.             Image(this._pixelMap)
    19.             .width(500)
    20.             .height(500)
    21.             .objectFit(ImageFit.Cover)
    22.             .border({width: 1, color: Color.Blue})
    23.             }
    24.             .width('100%')
    25.         }
    26.         .height('100%')
    27.     }
    28. }
    复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莱莱

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表