gpt4 book ai didi

java - android NDK——将C++方法转换为java方法

转载 作者:行者123 更新时间:2023-11-28 03:06:44 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,可以从任何捕获的图像中提取词袋为此,我将使用 opencv 库来实现此 tutorial

现在我有了一个 C++ 方法:void extractTrainingVocabulary(const path& basepath) {...}我在我的 android Activity 中需要它

   void extractTrainingVocabulary(const path& basepath) {
for (directory_iterator iter = directory_iterator(basepath); iter
!= directory_iterator(); iter++) {
directory_entry entry = *iter;

if (is_directory(entry.path())) {

cout << "Processing directory " << entry.path().string() << endl;
extractTrainingVocabulary(entry.path());

} else {

path entryPath = entry.path();
if (entryPath.extension() == ".jpg") {

cout << "Processing file " << entryPath.string() << endl;
Mat img = imread(entryPath.string());
if (!img.empty()) {
vector<KeyPoint> keypoints;
detector->detect(img, keypoints);
if (keypoints.empty()) {
cerr << "Warning: Could not find key points in image: "
<< entryPath.string() << endl;
} else {
Mat features;
extractor->compute(img, keypoints, features);
bowTrainer.add(features);
}
} else {
cerr << "Warning: Could not read image: "
<< entryPath.string() << endl;
}

}
}
}
}

所以按照 android NDK 教程我应该这样声明这个方法:public native void extractTrainingVocabulary () ;

我的问题是如何处理 C++ 参数 const path& basepath?如何在java方法中传递这个参数

我希望我的问题对你来说是清楚的谢谢

最佳答案

第一个问题是,C/C++代码中path的基类型是什么,比如这个是String,那么Java方法需要声明一个String值作为输入。

class Dude{
public native void extractTrainingVocabulary(final String arg);
}

首先用javac Dude.java编译dude类然后你需要头文件,将生成的类文件传递给javah Dude,然后javah会给你一个头文件,像这样。

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Dude */

#ifndef _Included_Dude
#define _Included_Dude
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Dude
* Method: extractTrainingVocabulary
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_Dude_extractTrainingVocabulary
(JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

在上面的代码中,jstring 指向该方法的 Java 输入参数,您可能需要使用那个人。下一步是实现 Java_Dude_extractTrainingVocabulary 函数并调用实际方法。

关于java - android NDK——将C++方法转换为java方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19492187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com