【发布时间】: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 库的优雅方式是什么?
为了方便测试,我上传了以下内容:
- opencv_440_windows32.zip:32 位头文件/动态库(以及 java 包装器)
- opencv_440_windows64.zip:64 位头文件/动态库(以及 java 包装器)
更新
这里是静态库,包括基于 José 出色答案的 java 绑定:
【问题讨论】:
标签: java opencv shared-libraries