How to use OpenCV 4 in Java on Windows?

99封情书 提交于 2020-06-29 03:40:01

问题


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.

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

and this how the jar references the native libraries:

And this is the basic test snippet:

import org.opencv.core.*;

public class CVTest {

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

}

which throws this exception:

     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:

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)

    

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

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");

This works but hardcoding every single DLL in that order feels very hacky.

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

来源:https://stackoverflow.com/questions/62503875/how-to-use-opencv-4-in-java-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!