https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
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
下载JDK8,解压后是名为jdk1.8.0_311的文件夹,将该文件夹移动到这个目录下面:/usr/lib/jvm/
打开文件~/.bashrc,添加以下内容:
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
下载ANT,地址是:https://dlcdn.apache.org//ant/binaries/apache-ant-1.9.16-bin.tar.gz
解压后是名为apache-ant-1.9.16的文件夹,将该文件夹移动到这个目录下面:/usr/local/
打开文件~/.bashrc,添加以下内容:
export ANT_HOME=/usr/local/apache-ant-1.9.16 export PATH=$ANT_HOME/bin:$PATH
执行命令source ~/.bashrc
检查java和ANT安装是否完成:
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
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
cmake -D CMAKE_BUILD_TYPE=Release -D BUILD_SHARED_LIBS=OFF -D CMAKE_INSTALL_PREFIX=./install ..
-- 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
make -j6
在执行cmake命令的时候,已经用CMAKE_INSTALL_PREFIX=./install参数指定了安装目录在opencv-4.1.1/build/install,现在执行安装命令就会将OpenCV的库安装到这个目录下
执行安装命令make install,如果控制台没有error相关的信息,就算安装成功了
进入install目录看看,里面有四个目录:
bin include lib share
opencv4/ ├── libopencv_java411.so └── opencv-411.jar
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); } }