【问题标题】:How to use OpenCV 4.4.0 (with contrib modules) in Java on Windows?如何在 Windows 上的 Java 中使用 OpenCV 4.4.0(带有 contrib 模块)?
【发布时间】:2020-06-21 20:13:35
【问题描述】:

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

这是我设置 Eclipse 项目的方式:

这是 jar 引用本机库的方式:

这是基本的测试sn-p:

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)

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

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

但是我遇到了这个异常:

线程“主”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 位上运行它。

我尝试一次手动加载一个库并使用Dependency Walker,最后设法实例化Mat,如下所示:

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 感觉就像是一个混乱的 hack。 有没有其他人遇到过这种情况?

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

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

更新

这里是静态库,包括基于 José 出色答案的 java 绑定:

【问题讨论】:

    标签: java opencv shared-libraries


    【解决方案1】:

    尝试预编译的库应该更容易。

    <!-- https://mvnrepository.com/artifact/org.openpnp/opencv -->
    <dependency>
        <groupId>org.openpnp</groupId>
        <artifactId>opencv</artifactId>
        <version>4.3.0-2</version>
    </dependency>
    

    几年前我尝试过这个库,它可以工作。

    【讨论】:

    • 这不是一个坏主意 (+1),但是,我需要使用版本 4.4.0-precontrib 模块(如果它是预编译的,很好,但最好是官方版本)
    【解决方案2】:

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

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

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

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

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

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

    1. Package Explorer 中右键单击进入您的项目。
    2. 选择Build Path → Configure Build Path... 选项。
    3. 在出现的窗口中,选择Libraries 标签。
    4. 展开JRE System library 选项,然后选择Native library location
    5. 点击右侧面板上的Edit... 按钮。
    6. 找到所需的库,然后单击OK

    可能需要在没有共享库的情况下构建库,以避免 dll 依赖项出现问题。你可以看到一个深入的解释here。请注意作者所说的:

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

    【讨论】:

    • 感谢 Jose:我之前在其他库中使用过它,并且效果很好。在这种情况下,它没有。我已经尝试过设置命令行参数并在运行时通过System.setProperty 执行相同的操作,但没有任何乐趣。如果评论以正确的顺序加载依赖的dll,它确实可以工作,但这就是我试图避免的。您能否使用我上面发布的 zip 对其进行快速测试?
    • 当然@GeorgeProfenza,我使用的是Mac,但我会尝试在我的Windows虚拟机中测试它。
    • 我知道这有点痛苦:非常感谢您的努力
    • 我一直在研究这个问题并做了一些测试。实际上,在这种情况下,库之间似乎存在一些依赖关系,这使得加载过程无法正常工作。无论如何,似乎在java.library.path 中包含JNI 本机库opencv_java440.dll 并动态加载它就足够了(这也在org.openpnp 的Maven 依赖项中完成)。在下一条评论中继续
    • 我很高兴听到 cmets 提供了帮助。如果我能以任何方式帮助您,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2015-09-24
    • 2021-03-17
    • 2016-01-31
    • 2023-04-06
    • 2015-07-13
    • 2018-04-17
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多