莱莱 发表于 2025-1-20 04:32:27

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

 鸿蒙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。
target_link_libraries(entry PUBLIC libace_napi.z.so libhilog_ndk.z.so libpixelmap_ndk.z.so) 添加接口映射
打开src/main/cpp/hello.cpp文件,在Init函数中添加接口映射如下:
EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
      { "createPixelMapTest", nullptr, CreatePixelMapTest, nullptr, nullptr, nullptr, napi_default, nullptr },
      { "createAlphaPixelMap", nullptr, CreateAlphaPixelMap, nullptr, nullptr, nullptr, napi_default, nullptr },
      { "transform", nullptr, Transform, nullptr, nullptr, nullptr, napi_default, nullptr },
    };

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc), desc);
    return exports;
}
EXTERN_C_END Native接口调用
具体接口阐明请参考API文档。
在hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下:
添加引用文件
#include <multimedia/image_framework/image_mdk_common.h>
#include <multimedia/image_framework/image_pixel_map_mdk.h>
#include <stdlib.h>
[*] 创建一个 PixelMap 对象。
napi_value CreatePixelMapTest(napi_env env, napi_callback_info info) {
    napi_value udfVar = nullptr;
    napi_value pixelMap = nullptr;

    struct OhosPixelMapCreateOps createOps;
    createOps.width = 4;
    createOps.height = 6;
    createOps.pixelFormat = 4;
    createOps.alphaType = 0;
    size_t bufferSize = createOps.width * createOps.height * 4;
    void *buff = malloc(bufferSize);
    if (buff == nullptr) {
      return udfVar;
    }

    char *cc = (char *)buff;
    for (int i = 0; i < 96; i++) {
      *(cc++) = (char)i;
    }
    int32_t res = OH_PixelMap_CreatePixelMap(env, createOps, (uint8_t *)buff, bufferSize, &pixelMap);
    free(buff);
    if (res != IMAGE_RESULT_SUCCESS || pixelMap == nullptr) {
      return udfVar;
    }
    return pixelMap;
}
[*] 根据Alpha通道的信息,来生成一个仅包含Alpha通道信息的 PixelMap 对象。
napi_value CreateAlphaPixelMap(napi_env env, napi_callback_info info) {
    napi_value udfVar = nullptr;
    napi_value thisVar = nullptr;
    napi_value argValue = {0};
    size_t argCount = 1;

    napi_value alphaPixelMap = nullptr;

    napi_get_undefined(env, &udfVar);

    if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
      argValue == nullptr) {
      return udfVar;
    }
    int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue, &alphaPixelMap);
    if (res != IMAGE_RESULT_SUCCESS || alphaPixelMap == nullptr) {
      return udfVar;
    }
    return alphaPixelMap;
}
[*] 对 PixelMap 数据进行处理。
napi_value Transform(napi_env env, napi_callback_info info) {
    napi_value thisVar = nullptr;
    napi_value argValue = {0};
    size_t argCount = 1;

    if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 ||
      argValue == nullptr) {
      return nullptr;
    }
    napi_value result = nullptr;
    napi_get_undefined(env, &result);
   
    // 初始化NativePixelMap对象。
    NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue);
    if (native == nullptr) {
      return result;
    }

    // 获取图片信息。
    struct OhosPixelMapInfos pixelMapInfo;
    OH_PixelMap_GetImageInfo(native, &pixelMapInfo);

    // 获取PixelMap对象每行字节数。
    int32_t rowBytes;
    OH_PixelMap_GetBytesNumberPerRow(native, &rowBytes);

    // 获取PixelMap对象是否可编辑的状态。
    int32_t editable = 0;
    OH_PixelMap_GetIsEditable(native, &editable);

    // 获取PixelMap对象是否支持Alpha通道。
    int32_t supportAlpha = 0;
    OH_PixelMap_IsSupportAlpha(native, &supportAlpha);

    // 设置PixelMap对象的Alpha通道。
    int32_t alphaAble = 0;
    OH_PixelMap_SetAlphaAble(native, alphaAble);

    // 获取PixelMap对象像素密度。
    int32_t densityG;
    OH_PixelMap_GetDensity(native, &densityG);

    // 设置PixelMap对象像素密度。
    int32_t densityS = 100;
    OH_PixelMap_SetDensity(native, densityS);

    // 设置PixelMap对象的透明度。
    float opacity = 0.5;
    OH_PixelMap_SetOpacity(native, opacity);

    // 设置缩放比例。
    // scaleX: 宽为原来的0.5。
    // scaleY: 高为原来的0.5。
    float scaleX = 0.5;
    float scaleY = 0.5;
    OH_PixelMap_Scale(native, scaleX, scaleY);

    // 设置偏移。
    // translateX: 向下偏移50。
    // translateY: 向右偏移50。
    float translateX = 50;
    float translateY = 50;
    OH_PixelMap_Translate(native, translateX, translateY);

    // 设置顺时针旋转90度。
    float angle = 90;
    OH_PixelMap_Rotate(native, angle);

    // 设置翻转
    // flipX: 水平翻转,0为不翻转,1为翻转。
    // flipY: 垂直翻转,0为不翻转,1为翻转。
    int32_t flipX = 0;
    int32_t flipY = 1;
    OH_PixelMap_Flip(native, flipX, flipY);

    // 设置裁剪区域。
    // cropX: 裁剪起始点横坐标。
    // cropY: 裁剪起始点纵坐标。
    // cropH: 裁剪高度10,方向为从上往下(裁剪后的图片高度为10)。
    // cropW: 裁剪宽度10,方向为从左到右(裁剪后的图片宽度为10)。
    int32_t cropX = 1;
    int32_t cropY = 1;
    int32_t cropW = 10;
    int32_t cropH = 10;
    OH_PixelMap_Crop(native, cropX, cropY, cropW, cropH);

    // 获取PixelMap对象数据的内存地址,并锁定该内存。
    void *pixelAddr = nullptr;
    OH_PixelMap_AccessPixels(native, &pixelAddr);

    // 释放PixelMap对象数据的内存锁。
    OH_PixelMap_UnAccessPixels(native);

    return result;
}
JS侧调用

[*] 打开src\main\cpp\types\libentry\index.d.ts(其中libentry根据工程名生成),导入如下引用文件:
import { image } from '@kit.ImageKit';

export const createPixelMapTest: () => image.PixelMap;
export const transform: (a: image.PixelMap) => void;
[*] 打开src\main\ets\pages\index.ets, 导入"libentry.so"(根据工程名生成),调用Native接口,传入JS的资源对象。示比方下:
import testNapi from 'libentry.so';
import { image } from '@kit.ImageKit';

@Entry
@Component
struct Index {
@State _pixelMap : image.PixelMap | undefined = undefined;

build() {
    Row() {
      Column() {
            Button("PixelMap")
            .width(100)
            .height(100)
            .onClick(() => {
                console.log("com.example.native_ndk_api10 button click in");
                this._pixelMap = testNapi.createPixelMapTest();
                testNapi.transform(this._pixelMap);
            })
            Image(this._pixelMap)
            .width(500)
            .height(500)
            .objectFit(ImageFit.Cover)
            .border({width: 1, color: Color.Blue})
            }
            .width('100%')
      }
      .height('100%')
    }
}
https://i-blog.csdnimg.cn/direct/ba7d889920f34dc88b9611853b5806d2.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 鸿蒙HarmonyOS实战开发:图片处理>使用Image处理PixelMap数据场景