溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在java中使用jni調(diào)用opencv對(duì)圖像進(jìn)行處理

發(fā)布時(shí)間:2021-03-10 15:18:15 來源:億速云 閱讀:249 作者:Leah 欄目:編程語言

怎么在java中使用jni調(diào)用opencv對(duì)圖像進(jìn)行處理?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

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文件,實(shí)現(xiàn)對(duì)應(yīng)函數(shù)(工程屬性中需包含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;
}

編譯生成對(duì)應(yīng)的dll

看完上述內(nèi)容,你們掌握怎么在java中使用jni調(diào)用opencv對(duì)圖像進(jìn)行處理的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI