怎么在java中使用jni调用opencv对图像进行处理?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
1. 建立java文件
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 文件
/* 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
#include "getImageFeatureDll.h" #include <opencv2/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
看完上述内容,你们掌握怎么在java中使用jni调用opencv对图像进行处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。