1. 建立 java 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public class getimagefeature { static { system.loadlibrary( "getimagefeaturedll" ); } public native int getimagefeaturebyname(string filename); public native int getimagefeaturebymemory();
public static void main(string[] args) { getimagefeature test= new getimagefeature(); string filename = "d:/testpic/6af1399a64d10a399ad3247c01656bb7.jpg" ; system.out.println(test.getimagefeaturebyname(filename)); } } |
2. 切换到工程src文件夹
javac getimagefeature.java
javah getimagefeature
生成 getimagefeature.h 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/* do not edit this file - it is machine generated */ #include < jni .h> /* header for class getimagefeature */
#ifndef _included_getimagefeature #define _included_getimagefeature #ifdef __cplusplus extern "c" { #endif /* * class: getimagefeature * method: getimagefeaturebyname * signature: (ljava/lang/string;)i */ jniexport jint jnicall java_getimagefeature_getimagefeaturebyname (jnienv *, jobject, jstring);
/* * class: getimagefeature * method: getimagefeaturebymemory * signature: ()i */ jniexport jint jnicall java_getimagefeature_getimagefeaturebymemory (jnienv *, jobject);
#ifdef __cplusplus } #endif #endif |
3. vs2013建立dll工程
添加getimagefeature.h 头文件,再添加getimagefeature.cpp文件,实现对应函数(工程属性中需包含jdk下的include目录)
c:\program files\java\jdk1.8.0_20\include
c:\program files\java\jdk1.8.0_20\include\win32
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include "getimagefeaturedll.h" #include < opencv 2/opencv.hpp>
/* * class: getimagefeature * method: getimagefeaturebyname * signature: (ljava/lang/string;)i */ jniexport jint jnicall java_getimagefeature_getimagefeaturebyname (jnienv *env, jobject obj, jstring filename) { const char *str_filename; str_filename = env->getstringutfchars(filename, false);
cv::mat image = cv::imread(str_filename); cv::imshow("test", image); cv::waitkey(20000); return 0;
}
/* * class: getimagefeature * method: getimagefeaturebymemory * signature: ()i */ jniexport jint jnicall java_getimagefeature_getimagefeaturebymemory (jnienv *, jobject) { return 0 ; } |
编译生成对应的dll
4. 执行java程序
将生成dll复制到java工程src文件夹下,java xx 运行程序
以上这篇java通过jni调用opencv处理图像的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
原文链接:https://blog.csdn.net/ZengDong_1991/article/details/54312715
查看更多关于java通过jni调用opencv处理图像的方法的详细内容...