亲宝软件园·资讯

展开

java通过jni调用opencv处理图像 java通过jni调用opencv处理图像的方法

ZengDong_1991 人气:0

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处理图像的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论