Making a desktop app using opencv in java/kotlin on Linux (Ubuntu)

Hi,

I am trying to make a desktop app in kotlin to experiment with various ideas to analyze videos. However I am unable to get opencv on my app.

Things I have tried:

  • importing opencv as a dependency by adding it to my `build.gradle.kts` file (I tried to get it from jitpack,io or maven, and strangely adding `implementation(“org.opencv:opencv:4.10.0”)` works for including opencv to an android app)
  • build opencv myself and then link the jar file to my application. However, when building opecncv, java cannot be found. The command I ran was: `cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_JAVA=ON -D BUILD_opencv_java=ON -D BUILD_opencv_java_bindings_generator=ON ..`, but the output I got was:
    ```

– Java:
– ant: /usr/bin/ant (ver 1.10.14)
– Java: NO
– JNI: /usr/lib/jvm/java-17-openjdk-amd64/include /usr/lib/jvm/java-17-openjdk-amd64/include/linux /usr/lib/jvm/java-17-openjdk-amd64/include
– Java wrappers: YES (ANT)
– Java tests: NO
```

And then after building and obtaining the jar, the kotlin compilter would not recognize any opencv dependency.

I would really appreciate if someone could explain me either how to build opencv in a way that detects java or how to import it.

1 Like

You got to “Use the Force” man. Install Cursor or VS Code or Antigravity editor. Then ask it to create a build script for you to acquire, build, and install a CV2, FFMPEG version for you. It will be an iterative process. Cut and paste the build errors you see into the prompt and it will keep fixing it until the build script is complete. Ask it to check the build flags because sometimes building the library’s are all it does, but it doesn’t build the binaries that use them so it looks like you never get a custom set built when you run ffmpeg -version (it shows the old stale version). Plan on spending 1-2 hours fighting the build scripts with Cursor, but you will make progress and achieve victory.

Are you making a desktop or Android app?

How to import OpenCV depends on your OS and build tool. Desktop, unlike Android, is a broad notion. For example, my Arch Linux already has an OpenCV package with Java bindings. The package contains the Java bindings at /usr/share/java/opencv5/opencv-500.jar. I use Maven.

In installed the package. I installed the Java bindings into the local Maven repository by executing

mvn install:install-file -DgroupId=org.opencv -DartifactId=opencv-local -Dversion=5.0.0 -Dpackaging=jar -Dfile=/usr/share/java/opencv5/opencv-500.jar

I added the following dependency to a Maven pom.xml:

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-local</artifactId>
    <version>5.0.0</version>
</dependency>

Now I can use OpenCV in my Java app.

Are you making a desktop or Android app?

I wanted to make an android app, but doing the computer vision was not easy so I wanted to make a desktop app as a prototype. I know it’s not really a constructive comment, but I did manage to import opencv for a kotlin desktop app by asking an LLM assistant.

I ran into a similar issue while integrating native computer vision into a desktop AI project. In my case, the main challenge wasn’t the Kotlin code but getting the Java bindings and native libraries configured correctly.
If Java: NO appears during the CMake configuration, I’d first verify that JAVA_HOME, javac, and the JDK are correctly detected by CMake. Also check that the generated opencv_java*.so (or .dll on Windows) is being loaded at runtime with System.loadLibrary(). Once the native library is loaded, the JAR alone should be recognized by Kotlin.