欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-23 17:01:04
1. 建立java文件 public class getimagefeature { static{ system.loadlibrary("geti...

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