HarmonyOS napi输入流输出流的示例代码

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

napi_init.cpp

#include "napi/native_api.h"
#include <hilog/log.h>
static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};

    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;
}
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;
}


EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    napi_property_descriptor desc[] = {
        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "Multiply", nullptr, Multiply, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "entry",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
{
    napi_module_register(&demoModule);
}

index.d.ts

export const Multiply: (inputBuff: object, length: number) => object;
分享
微博
QQ
微信
回复
1天前
相关问题
napi里面,相同输入输出不一致
1694浏览 • 1回复 待解决
napi 基本使用场景示例
1603浏览 • 1回复 待解决
HarmonyOS输入dialog示例
38浏览 • 1回复 待解决
鸿蒙有输入输出管理吗
7355浏览 • 2回复 待解决
HarmonyOS 数据库相关示例代码参考
197浏览 • 1回复 待解决
有使用华为支付示例代码
1805浏览 • 1回复 待解决
xComponet示例代码不能使用
893浏览 • 1回复 待解决
使用华为账号服务登录示例代码
1821浏览 • 1回复 待解决
使用hiappevent获取崩溃日志示例代码
1918浏览 • 1回复 待解决
HarmonyOS 能否将jni代码napi表示?
62浏览 • 1回复 待解决