All you need: install jar in your local maven repository with:
mvn install:install-file -Dfile=C:\opencv411\build\java\opencv-411.jar -DgroupId=org -DartifactId=opencv -Dversion=4.1.1 -Dpackaging=jar
create a dependency in pom.xml:
<dependency> <groupId>org</groupId> <artifactId>opencv</artifactId> <version>4.1.1</version> </dependency>
Now that jar is on, we need to somehow add the OpenCV libraries. I did this by adding the lib folder in java.library.path to the maven-surefire plugin:
<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> public static void main(String[] arges) throws MalformedURLException, IOException, Exception { loadLibraries(); // create and print on screen a 3x3 identity matrix System.out.println("Create a 3x3 identity matrix..."); Mat mat = Mat.eye(3, 3, CvType.CV_8UC1); System.out.println("mat = " + mat.dump()); // prepare to convert a RGB image in gray scale String location = "resources/Poli.jpg"; System.out.print("Convert the image at " + location + " in gray scale... "); // get the jpeg image from the internal resource folder Mat image = Imgcodecs.imread(location); // convert the image in gray scale Imgproc.cvtColor(image, image, Imgproc.COLOR_RGB2GRAY); // write the new image on disk Imgcodecs.imwrite("resources/Poli-gray.jpg", image); System.out.println("Done!"); } private static void loadLibraries() { try { InputStream in = null; File fileOut = null; String osName = System.getProperty("os.name"); // String opencvpath = System.getProperty("user.dir"); String opencvpath = "C:\\opencv411\\build\\java\\"; if (osName.startsWith("Windows")) { int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model")); if (bitness == 32) { opencvpath = opencvpath + "\\x86\\"; } else if (bitness == 64) { opencvpath = opencvpath + "\\x64\\"; } else { opencvpath = opencvpath + "\\x86\\"; } } else if (osName.equals("Mac OS X")) { opencvpath = opencvpath + "Your path to .dylib"; } System.out.println(opencvpath); // System.out.println("Core.NATIVE_LIBRARY_NAME = " + Core.NATIVE_LIBRARY_NAME); System.out.println("Core.NATIVE_LIBRARY_NAME = " + "opencv_java411.dll"); // System.load(opencvpath + Core.NATIVE_LIBRARY_NAME + ".dll"); System.load(opencvpath + "opencv_java411.dll"); } catch (Exception e) { throw new RuntimeException("Failed to load opencv native library", e); } }
source share