Skip to content

Instantly share code, notes, and snippets.

@skanga
Created March 25, 2020 14:10
Show Gist options
  • Save skanga/547b3afa4235fe4ab159e5cb062f46a7 to your computer and use it in GitHub Desktop.
Save skanga/547b3afa4235fe4ab159e5cb062f46a7 to your computer and use it in GitHub Desktop.
Build OpenCV (with Java) from git sources on Ubuntu Linux
# Install OS tools needed for the build
sudo apt-get -y update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev ant default-jdk
# Detect the default java home and set the env var for it
export JAVA_HOME=$(java -XshowSettings:properties -version 2>&1 > /dev/null | awk -F' ' '/java.home/ {print $3}')
export ANT_HOME=/usr/bin/ant
git clone https://github.com/opencv/opencv
cd opencv/
mkdir build
cd build
cmake -D BUILD_SHARED_LIBS=OFF ..
make -j$(nproc)
# Create a test program in Java
cat << EOF > ./HelloCV.java
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
public class HelloCV
{
public static void main (String[] args)
{
System.loadLibrary (Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye (3, 3, CvType.CV_8UC1);
System.out.println ("mat = " + mat.dump());
}
}
EOF
# Detect the OpenCV jar file name
OPENCV_JAR=$(ls bin/opencv-*.jar)
# Build and run the test
javac -cp .:$OPENCV_JAR HelloCV.java
java -cp .:$OPENCV_JAR -Djava.library.path=lib HelloCV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment