如何实现ArkTS与C/C++的数组转换

如何实现ArkTS与C/C++的数组转换

HarmonyOS
2024-07-21 19:16:17
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
e_lion

TS2NAPI

TS侧

Button("TS2NAPI") 
  .fontSize(50) 
  .fontWeight(FontWeight.Bold) 
  .onClick(() => { 
    let napiArray: Int32Array = new Int32Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 
    console.info("TS2NAPI: JS   " + napiArray) 
    testNapi.TS2NAPI(napiArray) 
  })

NAPI侧

// TS array 传到NAPI层 
static napi_value TS2NAPI(napi_env env, napi_callback_info info) { 
  // 获取TS层传来的参数 
  size_t argc = 1; 
  napi_value args; 
  napi_get_cb_info(env, info, &argc, &args, NULL, NULL); 
  napi_value input_array = args; 
 
  // 获取传入数组typedarray生成input_buffer 
  napi_typedarray_type type; // 数据类型 
  napi_value input_buffer; 
  size_t byte_offset; // 数据偏移 
  size_t i, length;   // 数据字节大小 
  napi_get_typedarray_info(env, input_array, &type, &length, NULL, &input_buffer, &byte_offset); 
 
  // 获取数组数据 
  void *data; 
  size_t byte_length; 
  napi_get_arraybuffer_info(env, input_buffer, &data, &byte_length); 
 
  if (type == napi_int32_array) { 
    int32_t *data_bytes = (int32_t *)(data); 
    int32_t num = length / sizeof(int32_t); 
 
    string str = ""; 
    for (int32_t i = 0; i < num; i++) { 
      int32_t tmp = *((int32_t *)(data_bytes) + i); 
      str += (to_string(tmp) + ' '); 
    } 
    OH_LOG_INFO(LOG_APP, "TS2NAPI: C++  %{public}s", str.c_str()); 
  } 
 
  return NULL; 
}

NAPI2TS

TS侧

Button("NAPI2TS") 
  .fontSize(50) 
  .fontWeight(FontWeight.Bold) 
  .onClick(() => { 
    let napiArray: Int32Array = testNapi.NAPI2TS() 
    console.info("NAPI2TS: JS   " + napiArray) 
  })

NAPI侧

// NAPI层 array 传到TS层 
static napi_value NAPI2TS(napi_env env, napi_callback_info info) { 
  // 数据个数 
  int num = 10; 
 
  // 创建output_buffer 
  napi_value output_buffer; 
  void *output_ptr = NULL; 
  napi_create_arraybuffer(env, num * sizeof(int32_t), &output_ptr, &output_buffer); 
 
  // output_array 
  napi_value output_array; 
  napi_create_typedarray(env, napi_int32_array, num, output_buffer, 0, &output_array); 
 
  // 给output_ptr、output_buffer赋值 
  int32_t *output_bytes = (int32_t *)output_ptr; 
  for (int32_t i = 0; i < num; i++) { 
    output_bytes[i] = i; 
  } 
 
  string str = ""; 
  for (int32_t i = 0; i < num; i++) { 
    int32_t tmp = *((int32_t *)(output_ptr) + i); 
    str += (to_string(tmp) + ' '); 
  } 
  OH_LOG_INFO(LOG_APP, "NAPI2TS: C++  %{public}s", str.c_str()); 
 
  return output_array; 
}

实用场景:

ArkTS侧读取rawfile的图片,传递到NAPI侧进行处理;

然后传回ArkTS进行展示。

Button("Test") 
  .fontSize(50) 
  .fontWeight(FontWeight.Bold) 
  .onClick(() => { 
 
    getContext().resourceManager.getRawFileContent('test.png').then(value => { 
      hilog.info(0x0000, 'getRawFileContent', '%{public}s', 'OK'); 
      const fileData: Uint8Array = value; 
      // 获取图片的ArrayBuffer 
      let buffer = fileData.buffer; 
 
      let input = new Uint8Array(buffer) 
      let output = testNapi.TEST(input); 
 
      let decodingOptions: image.DecodingOptions = { 
        editable: true, 
        desiredPixelFormat: 3, 
      } 
      let imageSource = image.createImageSource(output.buffer); 
      imageSource.createPixelMap(decodingOptions).then(map => { 
        this.imageSrc = map; 
      }) 
    }).catch((err: BusinessError) => { 
      hilog.error(0x0000, 'getRawFileContent', '%{public}s', err.message); 
    }) 
  }) 
 
Image(this.imageSrc) 
  .height(100) 
  .width(100)
分享
微博
QQ
微信
回复
2024-07-22 11:19:37
相关问题
如何实现ArkTSC/C++HashMap转换
672浏览 • 0回复 待解决
如何实现ArkTSC/C++对象传递
173浏览 • 1回复 待解决
HarmonyOS ArkTSC/C++交互
70浏览 • 1回复 待解决
如何C/C++ 创建ArkTS对象
1699浏览 • 1回复 待解决
ArkTSC++互相直接调用
977浏览 • 1回复 待解决
ts给c++传递数组c++如何解析
1411浏览 • 1回复 待解决
C++ 如何获取操作 Arkts 实例
515浏览 • 1回复 待解决
如何修改C++版本?C++支持情况?
551浏览 • 1回复 待解决
ArkTS对象绑定C++对象如何回收?
529浏览 • 1回复 待解决
c/c++层主动调用ArkTS存在问题
66浏览 • 1回复 待解决
C++调用ArkTS 定义方法
1088浏览 • 1回复 待解决
ArkTSC++之间交互
709浏览 • 1回复 待解决
ArkTS调用C++类中成员函数
811浏览 • 1回复 待解决
AVPlayer实现音频播放(c++侧)
664浏览 • 1回复 待解决
C++同步调ArkTS里面的方法
624浏览 • 1回复 待解决
HarmonyOS 如何调用标准C++
70浏览 • 1回复 待解决
通过Native 调用c++实现文本绘制
599浏览 • 1回复 待解决