android NDK --将C++方法转换为Java MTHOD
android NDK --converting C++ method to java mthod
我正在开发一个安卓应用程序,可以从任何捕获的图像中提取单词袋我将在本教程中使用 opencv 库
来实现此目的现在我有一个C++的方法: void extractTrainingVocabulary(const path& basepath) {...}
我在我的安卓活动中需要它
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,所以你需要用 String 值作为输入来声明 Java 方法。
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
函数并调用实际方法。
相关文章:
- SWIG Java 在使用 -DSWIGWORDSIZE64 时将int64_t转换为 jlong
- 我需要帮助将我的代码从 c++ 转换为 java
- 将C++映射转换为 Java 映射
- 使用 jni 将返回带有模板的对象的 Java 代码转换为 c++
- C++ (U256)*(H256 const*)(char*[] + int) 将重写转换为 Java
- JNA 将 HICON 转换为 Java Image
- 从c++到java的矢量转换
- 如何将通过TCP发送数据的数据从C++转换为Java
- 如何将 Java double[][] 转换为 C++ <vector<vector>> JNI?
- 如何通过 JNI 将 java CharSequence 转换为 c++ char*
- 将 Java LinkedHashMap 转换为 C++ std::map
- 将本机C 代码转换为Java
- 在使用 JNI 将 c++ unordered_map转换为 java hashMap之前将其转换为 java hash
- 将 C++ 中的字符值转换为具有与 Java 中相同值的字符串
- 如何将 GLCM 的实现转换为 Java C++
- 将 C++ 中的结构转换为 Java 中的类
- 将 Java ArrayList 转换为 C++ 向量时出现问题
- FlatBuffers C++从缓冲区转换为对象,仅适用于root_type(而Java具有所有)
- 将 JNI -> jobject(基本上是 java 文件中的映射和/或映射的映射)转换为 std::map(c++)
- 使用正确的数据类型转换java中的c++方法逻辑