目录
一、MAC系统下编译
1 环境准备:
2 安装opencv4.5.5
二、Linux系统下编译
1 环境准备
2 安装 OPENCV4.5.5
三、win10系统下编译
1 环境准备
2 安装 OPENCV4.5.5
四、检验编译是否正确
一、MAC系统下编译
mac版本:macOS Big Sur Apple M1
1 环境准备:
- (1)安装java环境
注意:Architecture类型一定要选择正确,不然使用会报下面的错,这是因为mac系统是基于ARM64,而安装java是x86 x64
Exception in thread "main" java.lang.UnsatisfiedLinkError: /usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib: dlopen(/usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib, 1): no suitable image found. Did find:
/usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib: mach-o, but wrong architecture
/usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib: mach-o, but wrong architecture
需要配置JAVA_HOME,可以通过echo $JAVA_HOME进行检测
- 安装ant
- 安装cmake
使用brew安装:brew install cmake
2 安装opencv4.5.5
#源码下新建build文件夹,进入文件夹
mkdir build
cd bulid
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=安装路径 -DBUILD_EXAMPLES=ON ../
make -j8
make install
执行CMKE命令后检查java相关的是yes,否则需要检查ant,java环境配置,不是yes不会生成java相关依赖
安装后安装目录下 ./share/java/opencv4有你需要的两个包:
opencv-455.jar、libopencv_java455.dylib
二、Linux系统下编译
Linux版本:x86_64 GNU/Linux
1 环境准备
- 安装JAVA,配置JAVA_HOME
- 安装ANT
- 安装CMAKE
基于源码编译安装CMAKE(3.1-3.21)
cd CMake
./bootstrap
make && make install
可能遇到下面问题
gmake: `cmake' is up to date.
loading initial cache file /home/croot/opencv/CMake/Bootstrap.cmk/InitialCacheFlags.cmake
-- Could NOT find OpenSSL, try to set the path to OpenSSL root folder in the system variable OPENSSL_ROOT_DIR (missing: OPENSSL_CRYPTO_LIBRARY OPENSSL_INCLUDE_DIR)
CMake Error at Utilities/cmcurl/CMakeLists.txt:562 (message):
Could not find OpenSSL. Install an OpenSSL development package or
configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.
需要安装OpenSSL ,设置OPENSSL_ROOT_DIR,OPENSSL_INCLUDE_DIR为环境变量
2 安装 OPENCV4.5.5
#源码下新建build文件夹,进入文件夹
mkdir build
cd bulid
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=安装路径 -DBUILD_EXAMPLES=ON ../
make -j8
make install
安装后安装目录下 ./share/java/opencv4有你需要的两个包:
opencv-455.jar、libopencv_java455.so
三、win10系统下编译
win10版本 x86_64
1 环境准备
安装JAVA,配置JAVA_HOME
- 安装ANT
- 安装CMAKE Download | CMake
- 安装MinGw64
MinGw64一定要安装 x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z安装其他版本,编译库时出现没有#include<mutex> 报错的问题.之后记得配置环境变量:配置bin目录下即可
2 安装 OPENCV4.5.5
1 打开cmark-gui ,选择上面选项,完成,进入编译
2 调整参数:BUILD_SHARED_LIBS 不勾选
3 点击Configure
可能出现错误:下载文件失败
FFMPEG: Download: opencv_videoio_ffmpeg.dll
Try 1 failed
CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
FFMPEG: Download failed: 28;"Timeout was reached"
For details please refer to the download log file:
D:/projects/java/github/opencv-4.5.4/build/CMakeDownloadLog.txt
解决方案:
查看CMakeDownloadLog.txt文件,
可以找到类似下面内容:cmake_download "D:/projects/java/github/opencv-4.5.4/.cache/ffmpeg/878a4e8fe5a4d68f18c9cdde543b9ead-opencv_videoio_ffmpeg_64.dll""https://raw.githubusercontent.com/opencv/opencv_3rdparty/40b4666d1aa374205fd61373496e15d92ecd5313/ffmpeg/opencv_videoio_ffmpeg_64.dll"
找到下载地址,通过迅雷等工具下载下载.保存到 源代码目录下的.cache目录下.命名按md4-文件名.后缀来(例如878a4e8fe5a4d68f18c9cdde543b9ead-opencv_videoio_ffmpeg_64.dll),如果遇到cmake文件,无法下载,可以在浏览器右键保存后进行修改,直接复制内容会导致md5校验失败
4 执行 mingw32-make -j 8
5 执行 mingw32-make install
6 java依赖和链接库位置: .\opencv-4.5.5\build\install\java
四、检验编译是否正确
- 安装opencv-455.jar到本地仓库
mvn install:install-file -Dfile=./opencv-455.jar -DgroupId=org.bytedeco -DartifactId=opencv -Dversion=4.5.5 -Dpackaging=jar
- 导入依赖
- 代码检测
public class TestOpencv4 {
static {
//System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 需要指定VM启动参数-Djava.library.path=you_path/libopencv_java455.dylib
System.load("you_path/libopencv_java455.dylib");
}
public static void main(String[] args) {
System.out.println("ok");
}
}
执行展示ok说明搭建完成