重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
怎么在java中使用jni调用opencv对图像进行处理?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联是一家专业提供颍上企业网站建设,专注与成都网站制作、网站设计、H5响应式网站、小程序制作等业务。10年已为颍上众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
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/* 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/* * 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对图像进行处理的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!