服务器之家

服务器之家 > 正文

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

时间:2021-05-23 15:06     来源/作者:ZengDong_1991

1. 建立java文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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处理图像的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/ZengDong_1991/article/details/54312715

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部