因项目需要,自己做了demo,从中学习很多,所以分享出来,希望有这方面需求的少走一些弯路,opencv怎么安装网上教程多多,这里不加详细说明,我安装的opencv-3.3.0

如上图所示,找到相应的jar包,这里讲一下如何这个jar如何导入Maven仓库

mvn install:install-file -Dfile=D:\opencv-3.0.\opencv\build\java\opencv-.jar 
-DgroupId=com.suibian -DartifactId=opencv- -Dversion=3.3. -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

<groupId>com.suibian</groupId>
<artifactId>opencv-</artifactId>
<version>3.3.</version>

好了,这样java可以通过这个jar操作opencv,那我们看一下,当然你能这样想,说明你很单纯,让我们看一下opencv 中includ中的路径和jar中包的对比

哈哈,全景图的关键stitching并没有,而且面向java的2015以后就一直没有更新,如下图所示一个全景图(傻瓜)合成的关键类没有,但是C++给我提供了

那怎么办呢,如何用java调用C++的代码,我们知道和操作系统打交道,C/C++无疑比Java更合适,opencv图形处理的这方面的应用,大多数是C++开发比较多,java中的一个关键字native大家都不陌生吧,然而我们想点进去,想看看是怎么实现的,怎么也点不进去,因为他就不是java写的,可能是C/C++,native对应都是java中jre下bin目录的XXX.dll文件,所以把我们要使用的C++代码打包成dll格式的文件放到bin目录即可,当然这涉及到方法的参数和返回值,废话不多说,安装visual  studio  2017,安装教程网上教程很多

点击  文件-->新建--->项目--->windows桌面-->动态链接库(Dll),这样项目就新建成功,接下来上代码

这是全景图合成的C++代码

#include "stdafx.h"
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "D:/result1.jpg";
int _tmain(int argc, char * argv[])
{
Mat img1 = imread("D:/quanjingtu/hh/1.jpg");
Mat img2 = imread("D:/quanjingtu/hh/2.jpg");
Mat img3 = imread("D:/quanjingtu/hh/3.jpg");
Mat img4 = imread("D:/quanjingtu/hh/4.jpg");
Mat img5 = imread("D:/quanjingtu/hh/5.jpg");
Mat img6 = imread("D:/quanjingtu/hh/6.jpg");
Mat img7 = imread("D:/quanjingtu/hh/7.jpg");
Mat img8 = imread("D:/quanjingtu/hh/8.jpg");
Mat img9 = imread("D:/quanjingtu/hh/9.jpg");
Mat img10 = imread("D:/quanjingtu/hh/10.jpg"); //Mat img6 = imread("6.jpg");
if (img1.empty() || img2.empty())
{
cout << "Can't read image" << endl;
return -;
}
imgs.push_back(img1);
imgs.push_back(img2);
imgs.push_back(img3);
imgs.push_back(img4);
imgs.push_back(img5);
imgs.push_back(img6);
imgs.push_back(img7);
imgs.push_back(img8);
imgs.push_back(img9);
imgs.push_back(img10); //imgs.push_back(img6);
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
// 使用stitch函数进行拼接
Mat pano;
Stitcher::Status status = stitcher.stitch(imgs, pano);
imwrite(result_name, pano);
Mat pano2 = pano.clone();
// 显示源图像,和结果图像
//imshow("全景图像", pano);
if (waitKey() == )
return ;
//imwrite(result_name, pano);
}

那么java如何和C++进行交互呢,通过java中jni技术

让我们了解一下java中JNI

  Java Native Interface(简称JNI),Java是跨平台语言,有时候需要调用本地代码,Sun公司提供JNI接口,通过这个接口与操作系统本地代码相互调用,如下图所示是Java中native的调用原理

脉络都理通了,那就开始吧

public class OpenCVUtil {
static {
//这里的system.load用来加载C++生成的动态链接库,加载实际自己也可以决定不一定非的是静态的。
System.loadLibrary("OpenCVUtil");
} public static native String changeArrValue(String str);
public static void main(String[] args) throws UnsupportedEncodingException {
String base="D:/quanjingtu/gg"+"/";
int length=;
String url=""; for (int i=;i<=length;i++){
if (i==){
url=url+base+i+".jpg";
}else {
url=url+","+base+i+".jpg";
} }
//System.out.println(url);
String temp =new String(changeArrValue(url).getBytes(),"GBK");
System.out.println(temp); ; }
}

定义native方法,将该java文件编译成.class文件

进入cmd命令窗口,打开对应编译好的class文件目录执行javah命令

javah  -classpath <生成XX.h文件存放位置>  -jni <包路径.类名>

生成对应com_lianxi_securitytest_opencv_OpenCVUtil.h文件

#include <jni.h>
/* Header for class com_lianxi_securitytest_opencv_OpenCVUtil */ #ifndef _Included_com_lianxi_securitytest_opencv_OpenCVUtil
#define _Included_com_lianxi_securitytest_opencv_OpenCVUtil
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_lianxi_securitytest_opencv_OpenCVUtil
* Method: changeArrValue
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_lianxi_securitytest_opencv_OpenCVUtil_changeArrValue
(JNIEnv *, jclass, jstring); #ifdef __cplusplus
}
#endif
#endif

只把native方法处理了,复制该文件,到vs 2017项目所在的位置

#include "stdafx.h"
#include "com_lianxi_securitytest_opencv_OpenCVUtil.h"
#include<iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
using namespace std;
using namespace cv;
bool try_use_gpu = false;
vector<Mat> imgs;
string result_name = "D:/result.jpg"; JNIEXPORT jstring JNICALL Java_com_lianxi_securitytest_opencv_OpenCVUtil_changeArrValue
(JNIEnv *env, jclass obj, jstring prompt) {
//这是接收过来的字符串,多个路径
const char* str;
//.................................................................... //........................... //imgs.push_back(img6);
Stitcher stitcher = Stitcher::createDefault(try_use_gpu);
// 使用stitch函数进行拼接
Mat pano;
Stitcher::Status status = stitcher.stitch(imgs, pano);
imwrite(result_name, pano);
Mat pano2 = pano.clone();
// 显示源图像,和结果图像
//imshow("全景图像", pano);
if (waitKey() == ) if (status != Stitcher::OK) {
return env->NewStringUTF("picture failure!!!"); } //return env->NewStringUTF(result_name.c_str);
string newstr = "picture success!!!URL=" + result_name;
return env->NewStringUTF(const_cast<char*>(newstr.c_str())); }

上面就是对应C++代码

点击生成--->重新生成解决方案    生成对应的dll文件,然后放到JDK的jre/bin目录里

运行java程序,结果如下

看一下合成的全景图吧

最新文章

  1. 简单的maven配置
  2. socket编程热身程序
  3. LoadRunner性能测试巧匠训练营
  4. 【BZOJ】1048: [HAOI2007]分割矩阵
  5. sql主键的一点重要理解
  6. Oracle程序包
  7. OpenGL光源位置
  8. 常用IC封装技术介绍
  9. android 环境搭建 windows, linux
  10. js精要之继承
  11. Unity Shader入门教程(二)最基本的Diffuse和Normal样例
  12. HDOJ2003-求绝对值
  13. 安全终端模拟工具Xshell 5使用密钥认证登录配置详细教程
  14. emqtt 试用(四)emq 的主题访问控制 acl.conf
  15. 【安富莱二代示波器教程】第18章 附件C---波形拟合
  16. java~@Async异步功能
  17. Oracle分组小计、总计示例(grouping sets的使用)
  18. [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [四] JSON数据解析
  19. Storm的部署
  20. macOS 升级后重装命令行工具的问题

热门文章

  1. alpha测试和beta测试的区别
  2. 数据分析--Matplotlib的基本使用
  3. Spring 梳理-传递模型数据到视图
  4. 阿里云服务器CentOS6.9安装Tomcat
  5. maven scope属性说明
  6. 容器技术-----------&gt;Docker
  7. jQuery三级联动效果代码(省、市、区)
  8. SUSE CaaS Platform 4 - 安装技巧
  9. 使用scm-manager搭建git/svn 代码管理仓库
  10. jquery 全选,反选