Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add OpenCV lib to Dynamic Web Project

Tags:

java

opencv

Currently, I am building a Java web project that use Opencv to detect images that are similar. But when I run, I always get this error

java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: opencv_java249 java.lang.Runtime.load0(Runtime.java:806) java.lang.System.load(System.java:1086) com.hadoop.DriverServlet.doPost(DriverServlet.java:25) javax.servlet.http.HttpServlet.service(HttpServlet.java:650) javax.servlet.http.HttpServlet.service(HttpServlet.java:731) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

I also search this problem but still can not find any solutions for my case. even I try this http://examples.javacodegeeks.com/java-basics/java-library-path-what-is-it-and-how-to-use/ to add to java.library path point to opencv-249 jar in eclipse but still not be resolved.

Anyone can help me? Thanks in advance.

like image 793
ndk076 Avatar asked Jul 01 '15 09:07

ndk076


1 Answers

To work with opencv you need jar file and binary file. JAR file can be simply added by local maven repository or any other variant.

Binary file you need to add and load manually. Something like this:

private static void addLibraryPath(String pathToAdd) throws Exception{
    final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
    usrPathsField.setAccessible(true);

    //get array of paths
    final String[] paths = (String[])usrPathsField.get(null);

    //check if the path to add is already present
    for(String path : paths) {
        if(path.equals(pathToAdd)) {
            return;
        }
    }

    //add the new path
    final String[] newPaths = Arrays.copyOf(paths, paths.length + 1);
    newPaths[newPaths.length-1] = pathToAdd;
    usrPathsField.set(null, newPaths);
}

public void init() {
        String pathToOpenCvDll = "c:\\opencv\\"; //linux path works too
        try {
            addLibraryPath(pathToOpenCvDll);
            System.loadLibrary("opencv_java320");
        } catch (Exception ignored) {
        }
    }
}
like image 112
Okapist Avatar answered Nov 15 '22 01:11

Okapist