Java OpenCV from Maven

There is currently no official way to use the official Java bindings for OpenCV as a Maven dependency (as already mentioned in the comments, the Maven artifact was already requested in #4588, but is still unattended). Nevertheless, there are 3 possible approaches to your problem:

  • java.lang.UnsatisfiedLinkError was thrown because you need to install the binding's binaries (that is "opencv_java") separately. Most likely, that unofficial artifact does not include them (or not the ones compatible with your system). In order to build the bindings:

    1. git clone the OpenCV repository.
    2. git checkout the intended version (it appears that you are using version 2.4.9, although more recent versions are available)
    3. Follow the instructions here to build OpenCV and its Java bindings, thus yielding a dynamically linked library ("opencv_java249.dll", "libopencv_java249.so", or something else depending on your OS).
    4. Copy the shared library file to your java.library.path (again, this variable is system-dependent, but can be defined when running your application). At this point you should be ready to use that artifact.
  • An alternative is to use other bindings: the JavaCPP presets for OpenCV seem to work just as nicely as the official ones, and these are registered in maven (binaries for various platforms included!). Just remember that the API may not be exactly the same.

  • This solution may sound too far out, but it has legitimately worked for me in the past. Basically, you can avoid using the bindings: implement your solution in C++, then either link it with the JVM via JNI or make it a separate application, used by the main application via other mechanisms of your system (process spawning, I/O channels, you name it). For instance, I have once made a service component for feature extraction that other programs would connect to via ZeroMQ sockets.


Try this, see if it works:

  • nu.pattern.OpenCV.loadShared();
  • System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);

More info here in API section: https://github.com/patternconsulting/opencv

Also have 2.4.9-7 opencv dependency.


Add the following dependency in your POM file:

<dependency>
    <groupId>org.openpnp</groupId>
    <artifactId>opencv</artifactId>
    <version>3.2.0-0</version>
</dependency>

and replace the following lines:

System.loadLibrary(Core.NATIVE_LIBRARY_NAME)

with

nu.pattern.OpenCV.loadShared();

This should solve the problem in WINDOWS also. Happy Coding.


This worked for me.

nu.pattern.OpenCV.loadLibrary();

I'm using following maven dependency

<dependency>
  <groupId>nu.pattern</groupId>
  <artifactId>opencv</artifactId>
  <version>2.4.9-4</version>
</dependency>

Tags:

Java

Maven

Opencv