mac 配置 opencv for java 环境【简易篇】
目的:取到:opencv-452.jar libopencv_java452.dylib 这两个文件;
opencv官网没有针对java提供单独的dylib动态库 所以需要我们自行编译
只说源码安装
1.下载源码:
下载opencv 代码:指定4.5.2 分支
git clone --branch 4.5.2 https://github.com/opencv/opencv.git
也可以gitee下载
2.配置环境:
需要:cmake ant jdk8
brew install cmake
brew install ant
jdk自行安装
重点来了:环境变量
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_331.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH
#################opencv dylib##############
export JAVA_AWT_INCLUDE_PATH=$JAVA_HOME
export JAVA_AWT_LIBRARY=$JAVA_HOME
export JAVA_INCLUDE_PATH=$JAVA_HOME/indclude
export JAVA_INCLUDE_PATH2=$JAVA_HOME/include/darwin
export JAVA_JVM_LIBRARY=$JAVA_HOME
###############################
JAVA_HOME 自己修改;
3.进入下载目录:
➜ opencv_github cd opencv
➜ opencv git:(69357b1e88) pwd
/Users/chentl/soft/opencv_github/opencv
➜ opencv git:(69357b1e88) ls
3rdparty LICENSE include
CMakeLists.txt README.md cmake modules
CONTRIBUTING.md SECURITY.md data platforms
COPYRIGHT apps doc samples
➜ opencv git:(69357b1e88) cmake -S . -B build
......[完成后生成build目录]
【
注意检查最后:
-- Java:
-- ant:
-- JNI:
-- Java wrappers:
-- Java tests:
这后面的值有没有,如果没有说明环境变量没有配置成功
删除build,先配置好环境,再执行构建
】
➜ opencv git:(69357b1e88) ls
3rdparty LICENSE build include
CMakeLists.txt README.md cmake modules
CONTRIBUTING.md SECURITY.md data platforms
COPYRIGHT apps doc samples
➜ opencv git:(69357b1e88) cd build/
➜ build git:(69357b1e88) make -j8
......[时间有点久,完成后]
➜ build git:(69357b1e88) cd modules/java
➜ build git:(69357b1e88) make -j8
......[完成后,结束]
生成的两个文件目录:
/Users/chentl/soft/opencv_github/opencv/build/bin/opencv-452.jar
/Users/chentl/soft/opencv_github/opencv/build/lib/libopencv_java452.dylib
对应build/bin跟build/lib下面;
4.项目使用:
jar建议上传私服,maven引用
关于加载动态库:VM 添加:-Djava.library.path={libopencv_java452.dylib所在目录}
懒人是不去想着启动项目还要配置资源路径的
不想配置VM
直接扔到
/Library/Java/Extensions/
目录下
参考:
在 Mac 上编译 OpenCV4 + Idea 使用Java调用 | caijinglong的博客 (kikt.top)