JNI+opencv3+.dll+eclipse+vs2015:java调用c++传递String类型参数全过程

首先,在eclipse里建立新的java project。新建包com.cogito。之后新建类TestNative,之后配置Opencv3(详见上一篇博客),代码如下。

注意:声明的public native开头的函数就是你要从.dll中获取的函数。

package com.cogito;  
  
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class TestNative {  
      
    
    public native void sayHello();
    public native void test01(String g);
    public static void main(String[] args) {  
          
       System.loadLibrary("ConsoleApplication2"); //载入dll库    //注意!不要写.dll
       String g;
       g="girl.jpg";
        TestNative test = new TestNative();  
        test.sayHello(); //调用本地方法</span>  
        test.test01(g);
    }  
}  

之后到这个项目文件目录里-bin,在bin的文件夹下按shift右键,进入cmd,输入javah com.cogito.TestNative
,点击enter,之后会发现bin文件夹多了一个com_cogito_TestNative.h。

之后进入vs2015,新建一个.dll文件。之后将你刚刚生成的com_cogito_TestNative.h复制到这个项目文件里,之后将你的java的jdk的文件夹打开,找到目录jdk1.7.0_80\include下的文件jni.h和jdk1.7.0_80\include\win32下的文件jnimd.h将jni和jnimd两个文件也复制到刚刚建立的.dll文件夹里。之后再次打开.dll文件,文件是下图所示


之后配置vs2015的opevcv3(详见上节)

之后source.cpp里可以加入想要实现的内容,例子:我的.dll文件的名字为:ConsoleApplication2             

ConsoleApplication2里的com_cogito_TestNative.h和source.cpp代码如下:

//com_cogito_TestNative.h

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

#ifndef _Included_com_cogito_TestNative
#define _Included_com_cogito_TestNative
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_cogito_TestNative
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_cogito_TestNative_sayHello
  (JNIEnv *, jobject);

/*
 * Class:     com_cogito_TestNative
 * Method:    test01
 * Signature: (Ljava/lang/String;)V
 */
JNIEXPORT void JNICALL Java_com_cogito_TestNative_test01
  (JNIEnv *env, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif

//source.cpp

# include "com_cogito_TestNative.h"  
# include <iostream>  

using namespace std;

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

JNIEXPORT void JNICALL Java_com_cogito_TestNative_sayHello(JNIEnv *, jobject)
{
	cout << "Hello World!" << endl;
}

JNIEXPORT void JNICALL Java_com_cogito_TestNative_test01(JNIEnv *env, jobject, jstring g)
{
	const char* str;
	str = env->GetStringUTFChars(g, false);      //这是将jstring 转成c++能读懂的char*
	if (str == NULL) {
		cout << "error!!!!!!!!!!!!!" << endl; /* OutOfMemoryError already thrown */
	}
	std::cout << str << std::endl;
	Mat girl = imread(str); //载入图像到Mat
	namedWindow("【1】动漫图"); //创建一个名为 "【1】动漫图"的窗口  
	imshow("【1】动漫图", girl);//显示名为 "【1】动漫图"的窗口  
	waitKey();

}

之后重新生成.dll项目,在”输出“中会出现.dll的生成路径,到路径中去找你得.dll


之后到java项目中

(因为代码需要,如果运行这个代码,需要java项目中有一个名为girl.jpg的图片)

之后右键点击java项目-找到属性-Build Path-Source-Native library location  加入刚刚生成的.dll的位置




注意:

JNI编程之如何传递参数——String参数的传递

转载于:
http://blog.csdn.net/wangkr111/article/details/7883461

  1. #include"Prompt.h"  
  2. #include<iostream>  
  3. JNIEXPORT jstringJNICALL Java_Prompt_getLine(JNIEnv *env, jobject obj, jstring prompt)  
  4. {  
  5.    const char* str;  
  6.    str = env->GetStringUTFChars(prompt, false);  
  7.    if(str == NULL) {  
  8.        return NULL; /* OutOfMemoryError already thrown */  
  9.    }  
  10.    std::cout << str << std::endl;  
  11.    env->ReleaseStringUTFChars(prompt, str);  
  12.    char* tmpstr = "return string succeeded";  
  13.    jstring rtstr = env->NewStringUTF(tmpstr);  
  14.    return rtstr;  
  15. }  

在上面的例子中,作为参数的prompt不能直接被C++程序使用,先做了如下转换
str = env->GetStringUTFChars(prompt, false);
将jstring类型变成一个char*类型。

返回的时候,要生成一个jstring类型的对象,也必须通过如下命令,
jstring rtstr = env->NewStringUTF(tmpstr);

这里用到的GetStringUTFChars和NewStringUTF都是JNI提供的处理String类型的函数,还有其他的函数这里就不一一列举了。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值