【无标题】

根据官方文档构建了Native侧 代码实现,但是Arkts侧代码错误,预览器不能启动

[Compile Result] ArkTS:WARN File: D:/App/entry/src/main/ets/pages/Index.ets:2:26
[Compile Result]  Currently module for 'libentry.so' is not verified. If you're importing napi, its verification will be enabled in later SDK version. Please make sure the corresponding .d.ts file is provided and the napis are correctly declared.
[Compile Result] ArkTS:ERROR File: D:/App/entry/src/main/ets/pages/Index.ets:16:7
[Compile Result]  The Button component with a label parameter can not have any child.
[Compile Result] ArkTS:ERROR File: D:/App/entry/src/main/ets/pages/Index.ets:27:7
[Compile Result]  The Button component with a label parameter can not have any child.
[Compile Result] Compile error occurred. Fix it based on the above message.

使用Node-API实现跨语言交互开发流程 (openharmony.cn)

这是导入libentry.so后预览器的信息[Compile Result]  Currently module for 'libentry.so' is not verified. If you're importing napi, its verification will be enabled in later SDK version. Please make sure the corresponding .d.ts file is provided and the napis are correctly declared.
 

这是导入libopencv_core.so后预览器的错误信息 [Compile Result]  Currently module for 'libopencv_core.so' is not verified. If you're importing napi, its verification will be enabled in later SDK version. Please make sure the corresponding .d.ts file is provided and the napis are correctly declared.

// entry/src/main/ets/pages/ImagePage.ets
import nativeModule from 'libentry.so';

@Entry
@Component
struct ImagePage {
  @State imagePath: string = '';
  @State resultMessage: string = '';

  build() {
    Column() {
      Text('Image Processing Page')
        .fontSize(30)
        .fontWeight(FontWeight.Bold)

      Button('Load Image') {
        Text('Loading...')
          .fontSize(20)
        // 调用Native侧的imread函数
        .onClick(() => {
          this.resultMessage = 'Image loading result: ';
          this.imagePath = 'path/to/image.jpg'; 
          this.resultMessage += nativeModule.imread(this.imagePath);
        })
      }

      Button('Save Image') {
        Text('Saving...')
          .fontSize(20)
        // 调用Native侧的imwrite函数
        .onClick(() => {
          this.resultMessage = 'Image saving result: ';
          this.imagePath = 'path/to/save/image.jpg';
          // imageData是处理后图像的数据
          let imageData = ''; // 这里是一个有效的图像数据
          this.resultMessage += nativeModule.imwrite(this.imagePath, imageData);
        })
      }

      Text(this.resultMessage)
        .fontSize(20)
    }
    .width('100%')
    .height('100%')
  }
}
//D:\App\entry\src\main\cpp\hello.cpp
#include "napi/native_api.h"
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

// 读取图像
static napi_value ImRead(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1];
    
    // 获取传入的参数并依次放入参数数组中
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    // 确保第一个参数是字符串类型
    napi_valuetype valuetype;
    napi_typeof(env, args[0], &valuetype);
    if (valuetype != napi_string) {
        napi_throw_type_error(env, nullptr, "First argument must be a string");
        return nullptr;
    }

    // 分配内存来存储字符串
    size_t len;
    napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
    char *filename = new char[len + 1];

    // 将字符串参数转换为C字符串
    napi_get_value_string_utf8(env, args[0], filename, len + 1, &len);


    // 使用OpenCV的imread函数读取图像
    Mat img = imread(filename, IMREAD_COLOR);
    if (img.empty()) {
        napi_throw_error(env, nullptr, "Failed to load image");
        return nullptr;
    }

    // 将OpenCV的Mat转换为N-API的Buffer
    void *data;
    size_t bytesPerLine;
    napi_value jsBuffer;
    napi_create_arraybuffer(env, img.total() * img.elemSize(), &data, &jsBuffer);
    memcpy(data, img.data, img.total() * img.elemSize());

    return jsBuffer;
}

// 写入图像
static napi_value ImWrite(napi_env env, napi_callback_info info) {
    size_t argc = 2;
    napi_value args[2];
    
    // 获取传入的参数并依次放入参数数组中
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    // 确保第一个参数是字符串类型
    napi_valuetype valuetype;
    napi_typeof(env, args[0], &valuetype);
    if (valuetype != napi_string) {
        napi_throw_type_error(env, nullptr, "First argument must be a string");
        return nullptr;
    }

    // 分配内存来存储字符串
    size_t len;
    napi_get_value_string_utf8(env, args[0], nullptr, 0, &len);
    char *filename = new char[len + 1];

    // 将字符串参数转换为C字符串
    napi_get_value_string_utf8(env, args[0], filename, len + 1, &len);


    // 确保第二个参数是Buffer类型
    napi_valuetype valuetype2;
    napi_typeof(env, args[1], &valuetype2);
    if (valuetype2 != napi_external) {
        napi_throw_type_error(env, nullptr, "Second argument must be a buffer");
        return nullptr;
    }

    // 从Buffer中读取数据并创建OpenCV的Mat
    void *data;
    size_t length;
    napi_get_buffer_info(env, args[1], &data, &length);
    int width = 640;  // 假设宽度
    int height = 480; // 假设高度
    int channels = 3; // 假设通道数
    Mat img(height, width, CV_8UC(channels), data);

    // 使用OpenCV的imwrite函数写入图像
    bool result = imwrite(filename, img);
    if (!result) {
        napi_throw_error(env, nullptr, "Failed to write image");
        return nullptr;
    }

    // 返回true表示成功
    napi_value success;
    napi_get_boolean(env, true, &success);
    return success;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {
    // 修复:确保ImRead和ImWrite函数被正确调用,这里仅示例定义,实际调用需根据项目情况调整
    napi_property_descriptor desc[] = {
        {"imread", nullptr, ImRead, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"imwrite", nullptr, ImWrite, nullptr, nullptr, nullptr, napi_default, nullptr},
    };

    size_t descriptorCount = sizeof(desc) / sizeof(desc[0]);
    napi_define_properties(env, exports, descriptorCount, 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); }
// entry/src/main/cpp/types/libentry/index.d.ts

// 定义imread方法,接受一个字符串路径,返回Promise,解析为Uint8ClampedArray类型的图像数据
export const imread: (path: string) => Promise<Uint8ClampedArray>;

// 定义imwrite方法,接受一个字符串路径和Uint8ClampedArray类型的图像数据,返回Promise表示操作结果
export const imwrite: (path: string, imageData: Uint8ClampedArray) => Promise<boolean>;
#\App\entry\src\main\cpp\CMakeLists.txt
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.4.1)
project(App)

set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

include_directories(${NATIVERENDER_ROOT_PATH}
                    ${NATIVERENDER_ROOT_PATH}/include)

add_library(entry SHARED hello.cpp)
target_link_libraries(entry PUBLIC libace_napi.z.so libopencv_core.so)
//D:\App\entry\src\main\cpp\types\libentry\oh-package.json5
{
  "name": "libentry.so",
  "types": "./index.d.ts",
  "version": "",
  "description": "Please describe the basic information."
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值