OpenCV with Scala

I started with the official site of OpenCV.

https://opencv.org/get-started/

Then, I clicked on the link: https://docs.opencv.org/4.x/d9/d52/tutorial_java_dev_intro.html

I chose the easy way that was described there and downloaded an archive from SourceForge:

https://sourceforge.net/projects/opencvlibrary/files/

After extracting it, I created a folder named "build" and changed into it.

Next, I set the JAVA_HOME variable and generated a Makefile with CMake, which I already had in my system.

> sudo update-alternatives --config java
There is only one alternative in link group java (providing /usr/bin/java): /usr/lib/jvm/java-17-openjdk-amd64/bin/java
> export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64
> cmake -DBUILD_SHARED_LIBS=OFF ..

In the output of the last command Java was present among the modules to be built.

--   OpenCV modules:
--     To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc java ml objdetect photo stitching ts video videoio

Then, I built the library utilizing all 16 processor cores of my computer.

> make -j16

When the build process was finished, I found a JAR-file in the build/bin directory.

I added it into the lib dirctory of my SBT-project as unmanaged dependency.

I copied the shared library libopencv_java480.so from build/lib into /usr/lib.

I added it into my IDEA IDE as well.

File -> Project Settings -> Modules -> Dependencies -> + -> JAR Files

Now I've got all the functions of OpenCV in Scala.

import org.opencv.core.Core

@main def helloCV: Unit =
  System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

0
Subscribe to my newsletter

Read articles from Aleksandr Novikov directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aleksandr Novikov
Aleksandr Novikov