I'm trying to use OpenCV's nonfree functionalities (like SIFT/SURF) in Java by compiling from source, but I'm encountering a DLL loading error in IntelliJ.
Here's my setup:
I'm using windows 11 24h2
Compilation Details: OpenCV Version: 4.12.0
CMake Configurations (the most important I think):
BUILD_SHARED_LIBS = ON
BUILD_JAVA = ON
BUILD_opencv_world = OFF
BUILD_opencv_java = ON
OPENCV_ENABLE_NONFREE = ON
OPENCV_EXTRA_MODULES_PATH = C:/opencv-build/opencv_contrib/modules
Compiler: Visual Studio 2022 (Release mode, x64)
Generated files:
- opencv_java4120.dll (in build/lib/release)
- opencv-4120.jar
- some others .dll for the additional modules and some binary files
The compilation finished normally, I didn't have any errors.
I set the opencv version in pom.xml (I'm using maven)
<dependencies>
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>4.12.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/opencv-4120.jar</systemPath>
</dependency>
</dependencies>
I've also added the dll path to IntelliJ's VM options
-Djava.library.path=.\src\main\resources
I've tried with a simple code
package com.analisis;
import org.opencv.core.*;
public class Main {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
System.out.println("opencv version: " + Core.VERSION);
}
}
This code works, but when I want to import a image using the Imgcodecs module I get this
Exception in thread "main" java.lang.UnsatisfiedLinkError:
C:\Users\jesus\IdeaProjects\analisis_img\src\main\resources\opencv_java4120.dll:
Error in a dynamic link library initialization routine. (DLL)
at java.base/jdk.internal.loader.NativeLibraries.load(Native Method)
...
I Verified that:
The JAR and DLL versions match (both are from the same OpenCV build).
All required DLLs are in the same directory.
- Why does the DLL fail to initialize despite correct paths?
- Are there missing dependencies I should check?
- Is there a precompiled Java-friendly version of OpenCV with nonfree modules?
The project is academic (patents aren't an issue).
I'm open to alternatives for using nonfree algorithms if compilation proves too complex.