本来openCV用python写最方便的,但实际应用中出于兼容性及维护性的考虑,还是选择了使用JAVA来进行部署,以下为主要配置步骤:
- 安装准备
- 安装openCV+java
- Web项目引用
一、安装准备:
1.1 下载安装Macports
1.2 安装xcode命令行
命令行输入:
xcode-select --install
二、安装openCV+java
2.1安装
安装命令行输入:
sudo port selfupdate
sudo port install opencv +java
2.2错误处理
不断回车,如果图中出现某个组件安装不了,如db48:
Error: Failed to build db48: command execution failed
Error: See /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_macports_release_tarballs_ports_databases_db48/db48/main.log for details.
Error: Follow https://guide.macports.org/#project.tickets to report a bug.
Error: Processing of port opencv failed
则输入以下命令先卸载掉,
sudo port clean db48
2.3安装确认
命令行中输入:
port contents opencv | grep java
显示类似以下内容则表示安装完成
/opt/local/share/OpenCV/java/libopencv_java341.dylib
/opt/local/share/OpenCV/java/opencv-341.jar
三、Web项目引用
3.1添加依赖
点击File ->Project Structure
添加依赖库
然后根据2.3返回的内容选中文件夹
选择jar包文件夹/opt/local/share/OpenCV/java/
3.2代码引用
直接引用即可
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to OpenCV " + Core.VERSION);
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat m = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("m = " + m.dump());
}
}
运行结果:
Welcome to OpenCV 3.4.1
m = [ 1, 0, 0;
0, 1, 0;
0, 0, 1]
3.3Web项目引用
(目前不熟悉后台,有错误请指正)
需要打包成war包上传到服务器的项目,比较麻烦些,除了3.2的本地代码引用外,还需要:
3.3.1 把.jar文件拷贝到"WEB-INF/lib"内,并在pom.xml里引用
3.3.2 拷贝到与类同路径,并获取路径加载
this.getClass().getResource("").getPath()+"/libopencv_java341.dylib";
System.load(path);
加载.dylib文件
网友评论