【问题标题】:android NDK --converting C++ method to java mthodandroid NDK -- 将 C++ 方法转换为 java 方法
【发布时间】:2013-10-21 10:35:31
【问题描述】:

我正在开发一个 android 应用程序,可以从任何捕获的图像中提取词袋 我将按照tutorial

使用 opencv 库

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

   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&amp; basepath?如何在java方法中传递这个参数

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

【问题讨论】:

  • 我希望这会有所帮助stackoverflow.com/questions/502430/…
  • 我是 C++ 新手,但在谷歌上搜索后这个参数 const path&amp; basepath 是一个常量引用,所以我不知道如何转换它,java 不使用引用
  • @nawara Java 仅对所有内容使用引用(原始类型除外)。但这不是重点,使用 JNI,您只能传递 JNI Types,因此您需要在 JNI 函数中使用一个 jstring,在该 JNI 函数的 c 实现中构造一个 path 对象(无论是什么)。然后使用该对象从 JNI 函数中调用现有的 c 函数。

标签: java android c++ opencv android-ndk


【解决方案1】:

第一个问题是,path在C/C++代码中的基类型是什么,例如如果这是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 函数并调用实际方法。

【讨论】:

  • 现在它不是字符串,路径是 C++ 类我编辑了我的问题以添加 extarctTrainingVocabulary 实现
  • 没问题老兄,Path的类型在这里是class/struct/etc..无关紧要,所以问题是它需要创建什么?!如果它需要两个 int,一个 String 和一个 byte[] 数据,那么您可以更改 java 方法声明来这样做,但请记住只传递真正需要从 java 应用程序传递的数据,没有默认值,常量等等……
  • 如果我理解您的回复,我应该将 C++ 方法需要什么以及 Path (无论其类型)需要什么传递给我的 java 方法?两种方法不需要相同的参数吗?
  • 是的,当然伙计,你需要知道创建Path类型的要求,如果它需要2个整数,那么通过java应用程序将整数传递给c/c++代码,然后在本机代码中使用这两个整数创建实际类型。
猜你喜欢
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 1970-01-01
  • 2016-09-23
相关资源
最近更新 更多