Java使用JNI调用C ++方法

时间:2017-03-12 10:05:11

标签: java c++ java-native-interface opencv3.1

我想用Java调用C ++实现图像镶嵌,这是我的Java代码

package com.example.jni;

public class JNITest {
    static {          
        System.loadLibrary("ConsoleApplication123");      
    }   

    // native method : the floder url that has pictures to be mosaic
    public native String hello(String name); 
} 

我把它编译成JNITest.h并以这种方式传递一个文件夹rul有照片, 然后在c + +中继续进行图像拼接。但我不知道如何在这个文件夹中阅读这些照片。如果我能看到这些图片,我可以实现图像拼接,最后我想要返回新的foler url镶嵌图片,这是我的image_mosaicking.cpp

/* DO NOT EDIT THIS FILE - it is machine generated */
#include "jni.h"
#include <iostream>  
#include <fstream>  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/stitching/stitching.hpp"  
using namespace std;
using namespace cv;

bool try_use_gpu = true; //false;  
vector<Mat> imgs;
string result_name = "result.jpg";
/* Header for class com_example_jni_JNITest */

#ifndef _Included_com_example_jni_JNITest
#define _Included_com_example_jni_JNITest
#ifdef __cplusplus
extern "C" {
#endif
    /*
    * Class:     com_example_jni_JNITest
    * Method:    hello
    * Signature: (Ljava/lang/String;)Ljava/lang/String;
    */
    JNIEXPORT jstring JNICALL Java_com_example_jni_JNITest_hello
    (JNIEnv *env, jobject, jstring path) {
        //read the path of this folder
        const char* temp_path = env->GetStringUTFChars(path, false);
        int numberOfPics = 0;
        string pics[100];

        //how to get the number of pictures in this folder 
        //how to get every picture's name into pics[]

        for (int i = 1; i < numberOfPics; ++i) {
            Mat img = imread(pics[i]);  //read every pictures in this folder
            if (img.empty()) {
                cout << "Can't read image '" << argv[i] << "'\n";
                // return  null;
            }
            imgs.push_back(img);
        }
            Mat pano;
            Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
            Stitcher::Status status = stitcher.stitch(imgs, pano);

            if (status != Stitcher::OK) {
                cout << "Can't stitch images, error code = " << int(status) << endl;
                // retrun null;
            }

            imwrite(result_name, pano);
            imshow("show", pano);
            cv::waitKey(0);

        jclass strClass = env->FindClass("Ljava/lang/String;");
        jstring imgName = env->NewStringUTF(temp_path);

        //rteurn the new folder that has mosaicked pictures


    }

#ifdef __cplusplus
}
#endif
#endif

0 个答案:

没有答案