PixelMap数据处理(Native)

PixelMap数据处理(Native)

HarmonyOS
2024-05-21 22:12:45
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Dinkar

本文主要介绍如何使用Native Image的接口,包括创建一个

PixelMap对象和对

PixelMap数据进行处理。

使用的核心API

native Image

核心代码解释

添加依赖

在进行应用开发之前,开发者需要打开native工程的src/main/cpp/CMakeLists.txt,在target_link_libraries依赖中添加image的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[0]), desc); 
  return exports; 
} 
EXTERN_C_END

Native接口调用

在hello.cpp文件中获取JS的资源对象,并转为Native的资源对象,即可调用Native接口,调用方式示例代码如下:

#include <multimedia/image_framework/image_mdk_common.h> 
#include <multimedia/image_framework/image_pixel_map_mdk.h> 
#include <memory>

创建一个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); 
​ 
  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); 
  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[1] = {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[0] == nullptr) { 
      return udfVar; 
  } 
  int32_t res = OH_PixelMap_CreateAlphaPixelMap(env, argValue[0], &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[1] = {0}; 
  size_t argCount = 1; 
​ 
  if (napi_get_cb_info(env, info, &argCount, argValue, &thisVar, nullptr) != napi_ok || argCount < 1 || 
      argValue[0] == nullptr) { 
      return nullptr; 
  } 
  napi_value result = nullptr; 
  napi_get_undefined(env, &result); 
    
  // 初始化PixelMap对象数据。 
  NativePixelMap *native = OH_PixelMap_InitNativePixelMap(env, argValue[0]); 
  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; 
}

实现效果

注明适配的版本信息

DevEco Studio Version: 4.0.1.601

SDK:HarmoneyOS 4.0.10.11

分享
微博
QQ
微信
回复
2024-05-22 20:51:18
相关问题
如何用PixelMap处理图片
426浏览 • 1回复 待解决
Native Image模块API-OH_PixelMap_CreatePixelMap
579浏览 • 1回复 待解决
如何将PixelMap数据存储到数据库中
592浏览 • 1回复 待解决
验证pixelmap数据buffer转base64是否正常
456浏览 • 1回复 待解决
提示数据丢失怎么处理?
3606浏览 • 2回复 待解决
ArkTS和Native互传数组类型数据
492浏览 • 1回复 待解决
Native代码如何直接操作数据
1751浏览 • 1回复 待解决
mysql存储过程中处理多条数据
745浏览 • 1回复 待解决
ArkTS侧与Native侧如何进行map数据交互
758浏览 • 1回复 待解决
关于处理数据库时分层有知道的吗?
1567浏览 • 1回复 待解决