简体   繁体   中英

android NDK --converting C++ method to java mthod

I'm developing an android app that extract the bag of word from any captured image I will use the opencv library for that purpose folliwing this tutorial

Now I have a C++ method : void extractTrainingVocabulary(const path& basepath) {...} that I need it in my 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;
            }

        }
    }
}
}

so following the android NDK tutorials I should declare this method like this : public native void extractTrainingVocabulary () ;

My problem is how to do with the C++ argument const path& basepath ? how to pass this argument in the java method

I hope that my question is clear for you Thanks

The first question is, what is the base type of path in C/C++ code, for example if this is String, so you need to declare the Java method with a String value as input.

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

first compile the dude class with javac Dude.java then you need the header file, pass the generated class file to javah Dude , then javah will give you a header file, something like this.

/* 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

here in above code, the jstring is pointing to the Java input argument for the method, may you need to use that guy. the next step is, implement the Java_Dude_extractTrainingVocabulary function and call the actual method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM