如何在 Windows 上的 Java 中使用 OpenCV 4.4.0(带有 contrib 模块)? [英] How to use OpenCV 4.4.0 (with contrib modules) in Java on Windows?

查看:75
本文介绍了如何在 Windows 上的 Java 中使用 OpenCV 4.4.0(带有 contrib 模块)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Windows 64 位和 java 绑定上从源代码编译了 OpenCV 4.4.0,我正在尝试编译一个基本测试,但是我遇到了意外错误.

I've compiled OpenCV 4.4.0 from source on Windows 64 bit along with java bindings and I'm trying to compile a basic test, however I'm running into unexpected errors.

以下是我设置 Eclipse 项目的方法:

Here's how I've setup an eclipse project:

这就是 jar 引用本地库的方式:

and this how the jar references the native libraries:

这是基本的测试片段:

import org.opencv.core.*;

public class CVTest {

    public static void main(String[] args) {
        System.load(Core.NATIVE_LIBRARY_NAME);
                
    }

}

抛出这个异常:

     Exception in thread "main" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: opencv_java440
at java.lang.Runtime.load0(Runtime.java:806)
at java.lang.System.load(System.java:1086)
at CVTest.main(CVTest.java:8)

我尝试硬编码绝对路径作为测试:

I've tried hardcoding the absolute path as a test:

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_java440.dll");

但是我遇到了这个异常:

However I run into this exception:

线程main"中的异常java.lang.UnsatisfiedLinkError:

Exception in thread "main" java.lang.UnsatisfiedLinkError:

C:\Users\george.profenza\Documents\eclipse\CVTest\lib\opencv_java440.dll: Can't find dependent libraries
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1934)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1817)
    at java.lang.Runtime.load0(Runtime.java:809)
    at java.lang.System.load(System.java:1086)
    at CVTest.main(CVTest.java:9)

    

我没想到会这样,因为我已经编译了 OpenCV 4 64 位,而且我正在 JVM 1.8 64 位上运行它.

I didn't expect this as I've compiled OpenCV 4 64-bit and I'm running this on JVM 1.8 64-bit.

我尝试一次手动加载一个库并使用 Dependency Walker 并最终成功实例化像这样的 Mat:

I've tried manually loading one library at a time and using Dependency Walker and finally managed to instantiate a Mat like so:

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_core440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_imgproc440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_imgcodecs440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_img_hash440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_videoio_ffmpeg440_64.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_videoio440.dll");      
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_photo440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_xphoto440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_flann440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_features2d440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_calib3d440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_phase_unwrapping440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_structured_light440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_xfeatures2d440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_video440.dll");        
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_ximgproc440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_aruco440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_bgsegm440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_bioinspired440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_objdetect440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_face440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_dnn440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_tracking440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_plot440.dll");

System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_ml440.dll");
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_text440.dll");


// f.finally load the JNI wrapper native lib
System.load("C:\\Users\\george.profenza\\Documents\\eclipse\\CVTest\\lib\\opencv_java440.dll");

这行得通,但按顺序硬编码每个 DLL 感觉就像一个混乱的黑客.有没有其他人遇到过这种情况?

This works but hardcoding every single DLL in that order feels like a messy hack. Has anyone else ran into this scenario ?

在 Windows 上用 Java 加载 OpenCV 4 库的优雅方式是什么?

What is the elegant way of loading the OpenCV 4 library in Java on Windows ?

为了方便测试,我上传了以下内容:

To facilitate testing I've uploaded the following:

  • opencv_440_windows32.zip: 32 bit headers/dynamic libraries (as well as java wrapper)
  • opencv_440_windows64.zip: 64 bit headers/dynamic libraries (as well as java wrapper)

更新

以下是静态库,包括基于 José 出色回答的 java 绑定:

Here are the static libs, including java bindings based on José's excellent anwer:

推荐答案

请尝试设置 java.library.path 属性以指示 JVM 在哪里可以找到本机库(我想您需要在您的情况下配置 C:/Users/george.profenza/Documents/eclipse/CVTest/lib\).

Please, try to set the java.library.path property to indicate the JVM where to find native libraries (I suppose you need to configure C:/Users/george.profenza/Documents/eclipse/CVTest/lib\ in your case).

从命令行(或 Eclipse 运行/调试配置),您可以包含所需的库,如下所示:

From the command line (or Eclipse Run/Debug configurations), you can include the required libraries as follows:

java -Djava.library.path=<path_to_dlls> <main class>

在Java代码中,你可以这样设置属性:

In the Java code, you can set the property like this:

System.setProperty("java.library.path", "/path/to/dlls");

除了修改运行/调试配置以在 Eclipse 中包含 -Djava.library.path 之外,还要在此 IDE 中设置 java.library.path 属性您可以遵循几个指南(查看 this,例如).基本上:

In addition to modifying the Run/Debug configurations to include the -Djava.library.path in Eclipse, to set the java.library.path property in this IDE you can follow several guides (look at this, for instance). Basically:

  1. Package Explorer 中右键单击您的项目.
  2. 选择 Build Path → Configure Build Path... 选项.
  3. 在出现的窗口中,选择 Libraries 标签.
  4. 展开JRE System library选项,然后选择Native library location.
  5. 点击右侧面板上的 Edit... 按钮.
  6. 找到所需的库,然后点击OK.
  1. Right click into your project in the Package Explorer.
  2. Select the Build Path → Configure Build Path... option.
  3. In the window that appears, select the Libraries tab.
  4. Expand the JRE System library option, and select the Native library location.
  5. Click on the Edit... button at the right panel.
  6. Locate the required libraries and then click OK.

可能需要在没有共享库的情况下构建库,以避免 dll 依赖项出现问题.您可以在此处查看深入说明.请注意作者所说的内容:

It could be necessary to build the library without shared libs to avoid problems with dll dependencies. You can see an in depth explanation here. Note where the author says:

当 OpenCV 构建为一组静态库(-DBUILD_SHARED_LIBS=OFF 选项)时,Java 绑定动态库就足够了,即不依赖于其他 OpenCV 库,但包括所有里面的 OpenCV 代码.

When OpenCV is built as a set of static libraries (-DBUILD_SHARED_LIBS=OFF option) the Java bindings dynamic library is all-sufficient, i.e. doesn't depend on other OpenCV libs, but includes all the OpenCV code inside.

这篇关于如何在 Windows 上的 Java 中使用 OpenCV 4.4.0(带有 contrib 模块)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆