SpringBoot uses OpenCV development and deployment

Recently, OpenCV was used for QR code recognition in the background, and other methods were also used, but the recognition was not very accurate. In comparison, OpenCV is the most accurate. Due to the first use, I encountered problems after using IDEA to develop and deploy a Linux server under windows. A lot of questions have come up, so I will make a special record here.

1. Development in the windows environment:

1. Download the installation package from the official website

(1) Opencv official website: https://opencv.org/

insert image description here

(2) Select the windows version

insert image description here

2. Unzip the installation package

The purpose of decompressing the installation package is to obtain the jar package and dll files in it. Many tutorials install the installation package directly, and the effect is the same.
jar package path: opencv-4.6.0-vc14_vc15\opencv\build\java\opencv-460.jar
dll file path: opencv-4.6.0-vc14_vc15\opencv\build\java\x64\opencv_java460.dll
dll file according to your The system is how many bits to select

3. Introduce the jar package into the project

(1) Create a new lib folder in the project to store the imported jar package, just put the jar package in it.

insert image description here

(2) Introduce the jar package in pom.xml, and fill in the path of the newly created lib file in the previous step in systemPath.
<dependency>
    <groupId>opencv</groupId>
    <artifactId>opencv</artifactId>
    <version>460</version>
    <scope>system</scope>
    <systemPath>${
    
    project.basedir}/src/main/lib/opencv-460.jar</systemPath>
</dependency>
(3) If you run it directly at this time, it will prompt Caused by: java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path, which is caused by the lack of dll files.

insert image description here

(4) Add dll files to solve the exception of no opencv_java460 in java.library.path.

Put the dll file in the System32 folder of the computer system, path: C:\Windows\System32
insert image description here

(5) test

test code

// 测试OpenCV是否安装成功
public static void main(String[] args) throws Exception {
    
    
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
    System.out.println("mat = " + mat.dump());
}

operation result
operation result

2. Deploy in Linux environment:

1. Download the installation package from the official website

Opencv official website: https://opencv.org/Select
the Linux compressed package version
insert image description here

2. Install

(1) Installation dependencies
# 缺什么就下什么,其中cmake是最重要的
yum -y install epel-release
yum install gcc gcc-c++  
yum install cmake 
yum install python-devel numpy 
yum install ffmpeg-devel
yum install -y unzip zip
(2) Decompress opencv
# 解压
unzip opencv-4.6.0.zip 
# 进入解压后的目录
cd opencv-4.6.0
# 创建名为build目录存放后续生成的一些东西
mkdir build
# 进入build目录中
cd build
(3) compile

This step takes a long time, please be patient.

cmake -D WITH_TBB=ON -D WITH_EIGEN=ON ..    
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=<你想要安装的路径> .. 
make 
make install

yum install ant
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=<你前面设置的安装路径> -DBUILD_TESTS=OFF ..
make -j8
make install
(4) test

If everything is normal, OpenCV was successfully compiled and generated. Make sure the following files are packaged in the appropriate directories.

/opt/opencv-4.6.0/build/lib/libopencv_java460.so
/opt/opencv-4.6.0/build/bin/opencv-460.jar

3. Problems that arise:

1.Caused by: java.lang.UnsatisfiedLinkError: no opencv_java460 in java.library.path

(1) In the Windows environment

insert image description here
Solution: Put the dll file in the System32 folder of the computer system, path: C:\Windows\System32

(2) Under Linux environment

insert image description here
Solution: put the so file generated by installing opencv (path: /opt/opencv-4.6.0/build/lib/libopencv_java460.so) into the /usr/lib64/ directory

2. The runtime prompts that the class has not been created successfully

(1) In the Windows environment

Workaround: Load the library with a static code block

// 得保证先执行该语句,用于加载库,才能调用其他操作库的语句
static {
    
    
	System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
(2) Under Linux environment

Solution: System.load loads the generated so file

static {
    
    
	System.load("/opt/opencv-4.6.0/build/lib/libopencv_java460.so");
}

Guess you like

Origin blog.csdn.net/qq_37131111/article/details/126588443