赞
踩
<dependency>
<groupId>opencv</groupId>
<artifactId>opencv</artifactId>
<version>455</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/lib/opencv-455.jar</systemPath>
</dependency>
C:\Windows\System32
中 public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
}
OpenCV官网上Java Api教程,适用于Windows用户的OpenCV包(包括jar文件),但并不适用于Linux和Mac OS用户。
所以 在linux上的项目如果需要使用openCV,需要自己去编译并生成opencv.jar和so文件
例:opencv_java2413.so 和opencv-java2413.jar
window中的opencv.jar是可以直接用的,但是这个自己编译的jar文件才是linux上可以用的额,他两完全不一样
部署时要注意:
复制so文件到/usr/local/lib下,使得在java环境下顺利调用opencv
cp lib/libopencv_java2413.so /usr/local/lib/
- 1
实操:
环境:
centOS 7.2 及以上
cat /etc/redhat-release
java 1.8.311
java -version
opencv-4.5.5
把jar包都放在/srv 下(随便啦)
centOs的版本一定要是7.2及以上
java -version
yum -y install gcc-c++
yum -y install gtk±devel
yum -y install gimp-devel gimp-devel-tools gimp-help-browser
yum -y install zlib-devel
yum -y install libtiff-devel libjpeg-devel libpng-devel
yum -y install gstreamer-devel
yum -y install libavc1394-devel libraw1394-devel libdc1394-devel
yum -y install jasper-devel jasper-utils
yum -y install swig
yum -y install python
yum -y install libtool
yum -y install nasm
yum -y install build-essential
yum -y install ant
下载
[root@localhost srv]# sudo wget https://cmake.org/files/v3.22/cmake-3.22.0-rc1-linux-x86_64.tar.gz
--2022-10-14 10:14:18-- https://cmake.org/files/v3.22/cmake-3.22.0-rc1-linux-x86_64.tar.gz
正在解析主机 cmake.org (cmake.org)... 66.194.253.25
正在连接 cmake.org (cmake.org)|66.194.253.25|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:45028024 (43M) [application/x-gzip]
正在保存至: “cmake-3.22.0-rc1-linux-x86_64.tar.gz”
100%[========================================================================>] 45,028,024 3.93MB/s 用时 19s
2022-10-14 10:14:37 (2.31 MB/s) - 已保存 “cmake-3.22.0-rc1-linux-x86_64.tar.gz” [45028024/45028024])
将会在srv中下载cmake 3.22版本的软件安装包 cmake-3.22.0-rc1-linux-x86_64.tar.gz
解压
[root@localhost srv]# tar -zxvf cmake-3.22.0-rc1-linux-x86_64.tar.gz
解压后,在srv下生成一个cmake-3.22.0-rc1-linux-x86_64
的文件夹
安装gcc工具
[root@localhost srv]# yum install gcc gcc-c++
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
软件包 gcc-4.8.5-44.el7.x86_64 已安装并且是最新版本
软件包 gcc-c++-4.8.5-44.el7.x86_64 已安装并且是最新版本
无须任何处理
软链
[root@localhost srv]# sudo mv cmake-3.22.0-rc1-linux-x86_64 /opt/cmake
[root@localhost srv]# sudo ln -sf /opt/cmake/bin/* /usr/bin/
[root@localhost srv]# cmake --version
cmake version 3.22.0-rc1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
cmake安装成功
**下载openCV(v4.5.5)**的源码包(zip /tar.gz)
下载地址:https://opencv.org/releases/
下载后通过xftp上传
创建openCV的安装目录
[root@localhost /]# mkdir -p /usr/local/opencv_make
[root@localhost opencv_make]# cd /usr/local/opencv_make
[root@localhost opencv_make]# mkdir release
解压openCV软件包
[root@localhost opencv_make]# unzip /srv/opencv-4.5.5.zip
解压成功后创建build
[root@localhost opencv_make]# ls
opencv-4.5.5 release
[root@localhost opencv_make]# cd opencv-4.5.5
[root@localhost opencv-4.5.5]# mkdir build
cmake
[root@localhost build]# cd build
[root@localhost build]# cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv_make/release -DBUILD_TESTS=OFF ..
正确的结尾
-- Java:
-- ant: /bin/ant (ver 1.9.4)
-- JNI: /usr/java/jdk1.8.0_311/include /usr/java/jdk1.8.0_311/include/linux /usr/java/jdk1.8.0_311/include
-- Java wrappers: YES
-- Java tests: NO
--
-- Install to: /usr/local/opencv_make/release
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/opencv_make/opencv-3.4.14/build
错误的结尾:
Java wrappers: NO
这时候要检查一下JAVA_HOMEecho $JAVA_HOME
ant :
为空或NO,则说明没有安装ant,使用yum进行安装
编译
[root@localhost build]# make -j2 // 注意按照自己计算机的核数,我是2核的
[root@localhost build]# make
在build/bin 下生成了jar文件,这个jar就是部署时候要替换的
同时,在build/lib下生成了os文件
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。