来自 Maven 的 Java OpenCV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37901505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java OpenCV from Maven
提问by deem
Is there any way to get OpenCV from repository? Which artifact should I add to pom.xml
? Every tutorial I'd found is from '14 and it seems like something changed - they say it is'nt in official Maven repository yet, but I've found entry:
有没有办法从存储库中获取 OpenCV?我应该添加到哪个工件pom.xml
?我发现的每个教程都来自 14 年,似乎有些变化-他们说它还不在官方 Maven 存储库中,但我找到了条目:
<!-- https://mvnrepository.com/artifact/nu.pattern/opencv -->
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-7</version>
</dependency>
Sadly, I get error
可悲的是,我收到错误
Caused by: java.lang.UnsatisfiedLinkError: no opencv_java249 in java.library.path
when I'm using System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
. Can I add this library in a way that would make my project include it and 'forget' about manually adding it to classpath?
当我使用System.loadLibrary(Core.NATIVE_LIBRARY_NAME)
. 我可以以一种方式添加这个库,让我的项目包含它并“忘记”手动将它添加到类路径?
采纳答案by Sachin Aryal
This worked for me.
这对我有用。
nu.pattern.OpenCV.loadLibrary();
I'm using following maven dependency
我正在使用以下 maven 依赖项
<dependency>
<groupId>nu.pattern</groupId>
<artifactId>opencv</artifactId>
<version>2.4.9-4</version>
</dependency>
回答by E_net4 the Rustacean
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:
目前还没有官方方法可以将 OpenCV 的官方 Java 绑定用作 Maven 依赖项(正如评论中已经提到的,Maven 工件已经在#4588 中被请求,但仍然无人看管)。不过,有 3 种可能的方法可以解决您的问题:
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:git clone
the OpenCV repository.git checkout
the intended version (it appears that you are using version 2.4.9, although more recent versions are available)- Follow the instructions hereto 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).
- 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 OpenCVseem 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.
java.lang.UnsatisfiedLinkError
被抛出是因为您需要单独安装绑定的二进制文件(即“opencv_java”)。最有可能的是,该非官方工件不包括它们(或与您的系统不兼容的工件)。为了构建绑定:git clone
OpenCV 存储库。git checkout
预期版本(您似乎使用的是 2.4.9 版,尽管有更新的版本可用)- 按照此处的说明构建 OpenCV 及其 Java 绑定,从而生成动态链接库(“opencv_java249.dll”、“libopencv_java249.so”或其他取决于您的操作系统的内容)。
- 将共享库文件复制到您的
java.library.path
(同样,此变量与系统相关,但可以在运行应用程序时定义)。此时,您应该准备好使用该工件了。
另一种方法是使用其他绑定:OpenCV的JavaCPP 预设似乎和官方的一样好用,并且这些都在 maven 中注册(包括各种平台的二进制文件!)。请记住,API 可能并不完全相同。
这个解决方案可能听起来太过分了,但它在过去对我来说是合法的。基本上,您可以避免使用绑定:在 C++ 中实现您的解决方案,然后通过 JNI 将其与 JVM 链接或使其成为一个单独的应用程序,由主应用程序通过系统的其他机制(进程生成、I/O 通道)使用,你的名字)。例如,我曾经制作了一个用于特征提取的服务组件,其他程序将通过 ZeroMQ 套接字连接到该组件。
回答by Melinda
Try this, see if it works:
试试这个,看看它是否有效:
nu.pattern.OpenCV.loadShared();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
nu.pattern.OpenCV.loadShared();
System.loadLibrary(org.opencv.core.Core.NATIVE_LIBRARY_NAME);
More info here in API section: https://github.com/patternconsulting/opencv
API 部分中的更多信息:https: //github.com/patternconsulting/opencv
Also have 2.4.9-7 opencv dependency.
也有 2.4.9-7 opencv 依赖。
回答by Halil
For those who wants to use OpenCV 3.2 in MacOs environment, you can use following repository definition:
对于那些想在 MacOs 环境中使用 OpenCV 3.2 的人,可以使用以下存储库定义:
<repositories>
<repository>
<id>kodfarki</id>
<url>https://raw.githubusercontent.com/kodfarki/repository/master/</url>
</repository>
</repositories>
There is also an example project in https://github.com/kodfarki/opencv-example.
https://github.com/kodfarki/opencv-example 中还有一个示例项目。
To use this example project, you still need to install OpenCV binaries
要使用此示例项目,您仍然需要安装 OpenCV 二进制文件
brew tap homebrew/science
brew install opencv3 --with-java --with-contrib
brew tap homebrew/science
brew install opencv3 --with-java --with-contrib
回答by Pr4y
Just use it nu.pattern.OpenCV.loadShared();
只需使用它 nu.pattern.OpenCV.loadShared();
write a class with this static void method
用这个静态 void 方法写一个类
class Test {
public static void loadOpenCVNativeLibrary() {
nu.pattern.OpenCV.loadShared();
}
}
and after call it in your application class (with static main) for web application (spring boot for example) like this
并在您的应用程序类(使用静态主)中为 Web 应用程序(例如 spring boot)调用它之后,像这样
static {
Test.loadOpenCVNativeLibrary();
}
...
public static void main(String[] args) throws UnknownHostException {
}
回答by Anirban Chakraborty
Add the following dependency in your POMfile:
在POM文件中添加以下依赖项:
<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 WINDOWSalso. Happy Coding.
这也应该可以解决WINDOWS 中的问题。快乐编码。
回答by user1198289
All you need: install jar in local maven repository with:
您只需要:使用以下命令在本地 maven 存储库中安装 jar:
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:
在 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:
现在 jar 已经打开,我们需要以某种方式添加 OpenCV 库。我通过将 java.library.path 中的 lib 文件夹添加到 maven-surefire 插件来做到这一点:
<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);
}
}