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

4. 执行java程序

将生成dll复制到java工程src文件夹下,java XX 运行程序

以上这篇java通过jni调用opencv处理图像的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。