Compile OpenCV in Linux and Generate opencv_java.so File


1. Introduction

In a Java project using OpenCV, in addition to needing to introduce the relevant POM dependencies, you also need to load the library files:
    * On Windows, it is opencv_java451.dll
    * On Linux, it is opencv_java451.so
This article mainly introduces how to compile OpenCV to obtain the relevant library files;

2. Environment Description
Operating System: Win11/Centos Stream 9
JDK: 1.8.0
OpenCV: 4.5.1 (You can choose the version when downloading related files)

3. Compiling to Obtain opencv_java451.dll on Windows
    3.1. Go to the OpenCV official website to download the corresponding version of the file
https://opencv.org/releases/page/2/

    3.2 Install the Windows version and extract the dependency package
Click to execute opencv-4.5.1-vc14_vc15.exe to extract the files, and get the opencv_java451.dll file in the opencv\build\java\x64 directory


4. Compiling OpenCV in Linux to Obtain the Dependency opencv_java451.so
    4.1. Install Dependencies
sudo yum install -y epel-release sudo yum groupinstall -y "Development Tools" sudo yum install -y cmake3 git gtk2-devel libpng-devel libjpeg-devel libtiff-devel \
jasper-devel openexr-devel webp-devel tbb-devel eigen3-devel python3-devel \
gstreamer-plugins-base-devel freeglut-devel mesa-libGL mesa-libGL-devel ant

    4.2. Download OpenCV Source Code (Download according to the version you need)
wget -O opencv-4.5.1.zip https://github.com/opencv/opencv/archive/4.5.1.zip wget -O opencv_contrib-4.5.1.zip https://github.com/opencv/opencv_contrib/archive/4.5.1.zip unzip opencv-4.5.1.zip unzip opencv_contrib-4.5.1.zip

    4.3. Configure CMake
mkdir -p build && cd build cmake3 -D CMAKE_BUILD_TYPE=RELEASE \
       -D CMAKE_INSTALL_PREFIX=/usr/local \
       -D OPENCV_GENERATE_PKGCONFIG=ON \
       -D OPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.5.1/modules \
       -D BUILD_opencv_java=ON \
       -D BUILD_opencv_java_bindings_generator=ON \
       -D BUILD_SHARED_LIBS=ON \
       -D BUILD_STATIC_LIBS=OFF \
       -D WITH_GTK=ON \
       -D WITH_OPENGL=ON \
       ../opencv-4.5.1

Find the Java-related output, and ensure that ant, java, and other information are configured correctly:
--   Java:                          --     ant:                         /bin/ant (ver 1.10.9)--     JNI:                         /usr/lib/jvm/java/include /usr/lib/jvm/java/include/linux /usr/lib/jvm/java/include--     Java wrappers:               YES--     Java tests:                  YES

    4.4. Compile
# Only compile the Java dependency package
make -j$(nproc) opencv_java
# Compile all (no need to execute)
# make -j$(nproc)

The output below indicates that the compilation was successful:
[100%] Linking CXX shared module ../../../lib/libopencv_java451.so[100%] Built target opencv_java

The dependency library is located in the following directory:
[build]# ll lib/libopencv_java451.so -rwxr-xr-x. 1 root root 2771808  2月 18 14:26 lib/libopencv_java451.so

Leave a Comment