Welcome to my GitHub
https://github.com/zq2599/blog_demos
Content: All original articles are categorized and summarized and supporting source code, involving Java, Docker, Kubernetes, DevOPS, etc.;
Overview of this article
- As a java programmer, if you want to use the OpenCV4 service on the Ubuntu16 desktop version, you can download the OpenCV source code of the version you need, and then compile the java library and so library yourself, so that you can use it in the java program
- This article records the download and compilation process of OpenCV4 in detail, and then writes a java program to verify whether the library of OpenCV4 can be successfully called. Generally speaking, it is divided into the following steps:
- Install necessary apps
- Configure the java environment
- Configure the ANT environment
- Download the source code
- Configuration before compilation
- compile
- Install
- verify
- Note: <font color="red"> The operations in this article are all executed with a non-root account </font>
environment and version
- OS: 16.04.7 LTS (Desktop)
- java:1.8.0_311
- ANT:1.9.16
- OpenCV:4.1.1
- Next, start the operation, here is a freshly installed pure version of Ubuntu16
Install the app
- Execute the following commands to install all applications. If there are individual prompts that fail, you can try several times:
sudo apt-get install -y unzip build-essential curl cmake cmake-gui git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Configure the java environment
- Download JDK8, unzip it to a folder named <font color="blue">jdk1.8.0_311</font>, move the folder to this directory: <font color="red">/usr/lib /jvm/</font>
- Open the file <font color="blue">~/.bashrc</font> and add the following:
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_311
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
Configure ANT
- Download ANT at: https://dlcdn.apache.org//ant/binaries/apache-ant-1.9.16-bin.tar.gz
- After decompression is a folder named <font color="blue">apache-ant-1.9.16</font>, move the folder to this directory: <font color="red">/usr/local /</font>
- Open the file <font color="blue">~/.bashrc</font> and add the following:
export ANT_HOME=/usr/local/apache-ant-1.9.16
export PATH=$ANT_HOME/bin:$PATH
- Execute the command <font color="blue">source ~/.bashrc</font>
- Check that the java and ANT installation is complete:
will@hp:~$ java -version
java version "1.8.0_311"
Java(TM) SE Runtime Environment (build 1.8.0_311-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.311-b11, mixed mode)
will@hp:~$ ant -version
Apache Ant(TM) version 1.9.16 compiled on July 10 2021
Download the source code
- Just execute the following command:
curl -fL -o opencv-4.1.1.zip https://codeload.github.com/opencv/opencv/zip/4.1.1; \
unzip opencv-4.1.1.zip; \
rm -rf opencv-4.1.1.zip; \
mkdir opencv-4.1.1/build; \
mkdir opencv-4.1.1/build/install
Configuration before compilation
- Enter the directory <font color="blue"> opencv-4.1.1/build/ </font>
- Execute cmake to generate configuration information:
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=OFF -D CMAKE_INSTALL_PREFIX=./install ..
- It should be noted that the above <font color="blue">-D BUILD_SHARED_LIBS=OFF</font> parameter is very important! The size of libopencv_java411.so generated without this parameter is only 1532128, and the size of libopencv_java411.so with this parameter is 78169672
- After the above command is executed, please check the information output from the console. As shown in the figure below, "java" must appear in the column of <font color="blue">To be build</font>, otherwise it will not be compiled during official compilation. Compile java related libraries:
- The following is some output information of successful configuration for reference:
-- Python (for build): /usr/bin/python2.7
--
-- Java:
-- ant: /usr/local/apache-ant-1.9.16/bin/ant (ver 1.9.16)
-- JNI: /usr/lib/jvm/jdk1.8.0_311/include /usr/lib/jvm/jdk1.8.0_311/include/linux /usr/lib/jvm/jdk1.8.0_311/include
-- Java wrappers: YES
-- Java tests: YES
--
-- Install to: /home/will/temp/202110/30/003/opencv-4.1.1/build/install
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: /home/will/temp/202110/30/003/opencv-4.1.1/build
compile
- Execute the following command in the <font color="blue"> opencv-4.1.1/build/ </font> directory to start compiling the source code, the parameter <font color="red">-j6</font> means six Threads are compiled in parallel (my computer is a 6-core CPU, please handle as appropriate):
make -j6
- The CPU went up quickly:
- I am here in less than 10 minutes to complete the compilation
Install
- When executing the cmake command, the installation directory has been specified with the <font color="blue">CMAKE_INSTALL_PREFIX=./install</font> parameter in <font color="red">opencv-4.1.1/build/install< /font>, now executing the installation command will install the OpenCV library to this directory
- Execute the installation command <font color="blue">make install</font>, if there is no <font color="red">error</font> related information on the console, the installation is successful
- Go to the install directory and see, there are four directories in it:
bin include lib share
- Enter the directory <font color="red">opencv-4.1.1/build/install/share/java/opencv4</font>, which has generated the jar and so library we need:
opencv4/
├── libopencv_java411.so
└── opencv-411.jar
verify
- Finally, the file is ready, then write a java application to verify whether the OpenCV library can be used normally
- I use IDEA here, create a new java project named <font color="blue">opencv-demo</font>
- Depending on the local jar, the setting method is as follows:
- Select the opencv-411.jar just generated
- Create a new Main.java file, as shown below. The function is to create a new window to display the local image. Please prepare the image yourself and modify it to the appropriate location:
package com.company;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import static org.opencv.highgui.HighGui.*;
import static org.opencv.imgcodecs.Imgcodecs.imread;
public class Main {
public static void main(String[] args) {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = imread("/home/will/temp/202110/30/pics/111.png");
if(mat.empty()) {
System.out.println("Image not exists!");
return;
}
namedWindow("src", WINDOW_AUTOSIZE);
imshow("src", mat);
waitKey(0);
// 这一句很重要,否则按下任意键后看不到窗口关闭的效果
System.exit(0);
}
}
- Finally, a very important step is to specify the location of the so library and click on the red box in the following figure:
- Add a VM Options parameter java.library.path, the value is the directory where libopencv_java411.so just created, as shown in the red box below:
- After the setting is completed, run Main.java, and the result is as follows. The left side is the window that displays the local image:
- So far, the generation and verification of OpenCV's java library and so library are completed. If you are also a java programmer using OpenCV, I hope this article can bring you some references;
https://github.com/zq2599/blog_demos
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。