Search code examples
javaopencvjarm2eclipse

How to use OpenCV build (opencv and opencv_contrib) in a Maven project


I compiled openCV and OpenCV_contrib library and had the resulting build/bin/opencv-411.jar and libraries .so in build/lib/. Now I am wondering how can I add these to my Maven project?

I was using that Maven repository but the contrib modules are not available.

I tried including the jar as a dependency like here with:

<dependency>
    <groupId>org.opencv</groupId>
    <artifactId>opencv-411</artifactId>
    <version>4.1.0</version> 
    <scope>system</scope>
    <systemPath>${resourcesfolder}/opencv-411.jar</systemPath>
</dependency>

I get no error when I launch clean javafx:compile but when I launch clean javafx:run a lot of OpenCV errors appear :

[ERROR] COMPILATION ERROR :

[INFO] -------------------------------------------------------------
[ERROR] /home/.../src/main/java/model/VideoModel.java:[3,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[4,23] package org.opencv.core does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[5,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[6,26] package org.opencv.videoio does not exist
[ERROR] /home/.../src/main/java/model/VideoModel.java:[25,17] cannot find symbol
     symbol:   class VideoCapture
     location: class model.VideoModel
[ERROR] /home/.../src/main/java/model/VideoModel.java:[26,17] cannot find symbol
     symbol:   class Mat

I also had a look to that but he deployed his new jar containing the executables and libraries in a remote repository. The interesting part is the jar creation, so I tried to do the same with opencv-411.jar and lib/ folder with:

cp opencv-411.jar opencv-411-new.jar
jar -uvf opencv-411-new.jar lib/

and kept the dependency as above, but raised the same errors...

What should I do?


Solution

  • Thanks to the answers, I came to the solution for including OpenCV jar and libs to Maven:

    • install the jar in the maven local repository with:
    mvn install:install-file -Dfile=/home/.../src/main/resources/opencv.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar
    
    • create the dependency in pom.xml:
    <dependency> 
     <groupId>org</groupId> 
     <artifactId>opencv</artifactId>
     <version>4.1.1</version>  
    </dependency> 
    

    Now that the jar is included, we must add the OpenCV libraries somehow.

    Just load the library in your app with:

    System.load("/path/to/lib/libopencv_java411.so");
    

    For the UI tests, as the maven-surefire-plugin uses a special JVM, you have to specify the lib folder to the java.library.path:

    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.2</version>
     <configuration>
      <argLine>-Djava.library.path=${project.build.outputDirectory}/lib</argLine>
     </configuration>
    </plugin>
    

    Please feel free to edit if you have a better solution or corrections to add.