HarmonyOS 鸿蒙Next napi_get_arraybuffer_info napi_create_arraybuffer 不知道在NDK中如何使用
HarmonyOS 鸿蒙Next napi_get_arraybuffer_info napi_create_arraybuffer 不知道在NDK中如何使用
static napi_value TestArrayBuffer(napi_env env, napi_callback_info info) {
size_t argc = 1;
napi_value args[1] = {nullptr};
napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
void *arrayBufferPtr = nullptr;
size_t compareSize = 0;
napi_status result = napi_get_arraybuffer_info(env, args[0], &arrayBufferPtr, &compareSize);
napi_value arrayBuffer = nullptr;
result = napi_create_arraybuffer(env, compareSize, &arrayBufferPtr, &arrayBuffer);
return arrayBuffer;
}
console.log('testArrayBuffer的结果是=' + uint8ArrayToString(new Uint8Array(testNapi.testArrayBuffer(stringToUint8Array('test').buffer))));
let textEncoder = new util.TextEncoder('utf-8');
let textDecoder = util.TextDecoder.create('utf-8', { ignoreBOM: true });
export function stringToUint8Array(str: string): Uint8Array {
return textEncoder.encodeInto(str);
}
export function uint8ArrayToString(data: Uint8Array) {
return textDecoder.decodeWithStream(data, { stream: false });
}
上面的日志testArrayBuffer的结果是=
是空的,怎么改
更多关于HarmonyOS 鸿蒙Next napi_get_arraybuffer_info napi_create_arraybuffer 不知道在NDK中如何使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
// TS array 传到NAPI层参考
static napi_value TS2NAPI(napi_env env, napi_callback_info info) { size_t argc = 1; napi_value args[1]; napi_get_cb_info(env, info, &argc, args, NULL, NULL); napi_value input_array = args[0];
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);
napi_value output_buffer;
void * output_ptr = NULL;
napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer);
napi_value output_array;
napi_create_typedarray(env, type, length, output_buffer, byte_offset, &output_array);
if (type == napi_uint8_array) {
uint8_t * input_bytes = (uint8_t *)(data) + byte_offset;
uint8_t * output_bytes = (uint8_t *)(output_ptr);
for (i = 0; i < length; i++) {
output_bytes[i] = (uint8_t)(input_bytes[i]);
}
} else if (type == napi_float64_array) {
double * input_doubles = (double *)((uint8_t *)(data) + byte_offset);
double * output_doubles = (double *)(output_ptr);
for (i = 0; i < length; i++) {
output_doubles[i] = input_doubles[i];
}
} else {
napi_throw_error(env, NULL, "Typed array was of a type not expected by test.");
return NULL;
}
return output_array;
}
接口说明官网:https://nodejs.cn/api/n-api.html#napi_get_arraybuffer_info_x000D_ /ets侧构造一个array
public toArrayBuffer() { let num = [49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57]; let inputBuffer = new Uint8Array(num); hilog.info(0x0000, TAG, 'inputBuffer: ’ + inputBuffer.length); //20
let outPutBuffer = testNapi.arrayBufferToNative(inputBuffer, 2)
hilog.info(0x0000, TAG, 'outPutBuffer: ' + outPutBuffer);
}
// index.d.ts声明 export const arrayBufferToNative: (inputBuff: object, length: number) => object; /对传入的buff做计算,数值乘以2返回
static napi_value Multiply(napi_env env, napi_callback_info info) { size_t argc = 2; napi_value args[2]; napi_get_cb_info(env, info, &argc, args, NULL, NULL); napi_value input_array = args[0];
double multiplier;
napi_get_value_double(env, args[1], &multiplier);
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);
napi_value output_buffer;
void * output_ptr = NULL;
napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer);
napi_value output_array;
napi_create_typedarray(env, type, length, output_buffer, byte_offset, &output_array);
if (type == napi_uint8_array) {
uint8_t * input_bytes = (uint8_t *)(data) + byte_offset;
uint8_t * output_bytes = (uint8_t *)(output_ptr);
for (i = 0; i < length; i++) {
OH_LOG_INFO(LOG_APP, "Multiply_for_arraybuffer input_bytes % {public} d", input_bytes[i]);
output_bytes[i] = (uint8_t)(input_bytes[i] * multiplier);
OH_LOG_INFO(LOG_APP, "Multiply_for_arraybuffer output_bytes % {public} d", input_bytes[i]);
}
} else if (type == napi_float64_array) {
double * input_doubles = (double *)((uint8_t *)(data) + byte_offset);
double * output_doubles = (double *)(output_ptr);
for (i = 0; i < length; i++) {
output_doubles[i] = input_doubles[i] * multiplier;
}
} else {
napi_throw_error(env, NULL, "Typed array was of a type not expected by test.");
return NULL;
}
return output_array;
}
更多关于HarmonyOS 鸿蒙Next napi_get_arraybuffer_info napi_create_arraybuffer 不知道在NDK中如何使用的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙系统的NDK(Native Development Kit)中,napi_get_arraybuffer_info
和 napi_create_arraybuffer
是与N-API(Native Abstraction Interface)相关的函数,用于在原生代码中操作JavaScript的ArrayBuffer对象。以下是这两个函数在NDK中的基本使用方法:
-
napi_get_arraybuffer_info
用于获取ArrayBuffer的信息,包括其数据指针和字节长度。
napi_status status = napi_get_arraybuffer_info(env, arraybuffer, &data, &byte_length);
其中,
env
是N-API环境句柄,arraybuffer
是要查询的ArrayBuffer对象,data
是指向数据指针的指针,byte_length
是指向字节长度的指针。 -
napi_create_arraybuffer
用于创建一个新的ArrayBuffer对象。
napi_status status = napi_create_arraybuffer(env, byte_length, &data, &arraybuffer);
其中,
env
是N-API环境句柄,byte_length
是ArrayBuffer的字节长度,data
是指向新创建的ArrayBuffer数据指针的指针,arraybuffer
是指向新创建的ArrayBuffer对象的指针。
请注意,使用这些函数时,需要确保已正确初始化N-API环境,并遵循其生命周期管理规则。
如果问题依旧没法解决请联系官网客服,官网地址是 https://www.itying.com/category-93-b0.html,