当前位置:   article > 正文

OPENCV源码编译生成java依赖及遇到问题(linux,mac,win10)_opencv编译java

opencv编译java

源码版本:GitHub - opencv/opencv at 4.5.5

目录

一、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环境

             安装地址 :(Java Download | Java 8, Java 11, Java 13 - Linux, Windows & macOS)

            注意:Architecture类型一定要选择正确,不然使用会报下面的错,这是因为mac系统是基于ARM64,而安装java是x86 x64

  1. 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:
  2. /usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib: mach-o, but wrong architecture
  3. /usr/local/Celler/opencv4.5.3/share/java/opencv4/libopencv_java455.dylib: mach-o, but wrong architecture

               需要配置JAVA_HOME,可以通过echo $JAVA_HOME进行检测

  • 安装ant

    使用brew安装:brew install ant
  • 安装cmake

         使用brew安装:brew install cmake

2  安装opencv4.5.5  

  1. #源码下新建build文件夹,进入文件夹
  2. mkdir build
  3. cd bulid
  4. cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=安装路径 -DBUILD_EXAMPLES=ON ../
  5. make -j8
  6. 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)

          源码:   Download | CMake

  1. cd CMake
  2. ./bootstrap
  3. make && make install

           可能遇到下面问题 

  1. gmake: `cmake' is up to date.
  2.     loading initial cache file /home/croot/opencv/CMake/Bootstrap.cmk/InitialCacheFlags.cmake
  3.     -- 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) 
  4.     CMake Error at Utilities/cmcurl/CMakeLists.txt:562 (message):
  5.     Could not find OpenSSL.  Install an OpenSSL development package or
  6.     configure CMake with -DCMAKE_USE_OPENSSL=OFF to build without OpenSSL.  

    需要安装OpenSSL ,设置OPENSSL_ROOT_DIR,OPENSSL_INCLUDE_DIR为环境变量

2   安装 OPENCV4.5.5                       

  1. #源码下新建build文件夹,进入文件夹
  2. mkdir build
  3. cd bulid
  4. cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX=安装路径 -DBUILD_EXAMPLES=ON ../
  5. make -j8
  6. make install

 安装后安装目录下 ./share/java/opencv4有你需要的两个包:

        opencv-455.jar、libopencv_java455.so

三、win10系统下编译

win10版本 x86_64

1  环境准备

安装JAVA,配置JAVA_HOME

          MinGw64一定要安装  x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z (https://nchc.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/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

    可能出现错误:下载文件失败

  1. FFMPEG: Download: opencv_videoio_ffmpeg.dll
  2. Try 1 failed
  3. CMake Warning at cmake/OpenCVDownload.cmake:202 (message):
  4. FFMPEG: Download failed: 28;"Timeout was reached"
  5. For details please refer to the download log file:
  6. D:/projects/java/github/opencv-4.5.4/build/CMakeDownloadLog.txt
  7. 解决方案:
  8. 查看CMakeDownloadLog.txt文件,
  9. 可以找到类似下面内容: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"
  10. 找到下载地址,通过迅雷等工具下载下载.保存到 源代码目录下的.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
  • 导入依赖                
  • 代码检测    
  1. public class TestOpencv4 {
  2. static {
  3. //System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 需要指定VM启动参数-Djava.library.path=you_path/libopencv_java455.dylib
  4. System.load("you_path/libopencv_java455.dylib");
  5. }
  6. public static void main(String[] args) {
  7. System.out.println("ok");
  8. }
  9. }

        执行展示ok说明搭建完成

动态库下载链接:

基于mac m1的动态链接库:libopencv_java455.dylib-互联网文档类资源-CSDN下载

基于Linux X86X64链接库:libopencv_java455.soopencv4.5.5linux编译_Linuxjavaopencv.so-互联网文档类资源-CSDN下载

基于window10 x86_64链接库:

opencv4.5.5window10x86_64链接库-互联网文档类资源-CSDN下载

Java依赖:opencv-455.jar-互联网文档类资源-CSDN下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/623280
推荐阅读
相关标签
  

闽ICP备14008679号