您好,登錄后才能下訂單哦!
怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。