简体   繁体   English

如何将OpenCV库添加到动态Web项目

[英]How to add OpenCV lib to Dynamic Web Project

Currently, I am building a Java web project that use Opencv to detect images that are similar. 当前,我正在构建一个使用Opencv来检测相似图像的Java Web项目。 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) java.lang.UnsatisfiedLinkError:期望库的绝对路径: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. 即使我尝试这个http://examples.javacodegeeks.com/java-basics/java-library-path-what-is-it-and-how-to-use/添加到java.library指向opencv-249的路径罐子在日食中,但仍无法解决。

Anyone can help me? 有人可以帮助我吗? Thanks in advance. 提前致谢。

To work with opencv you need jar file and binary file. 要使用opencv,您需要jar文件和二进制文件。 JAR file can be simply added by local maven repository or any other variant. JAR文件可以通过本地Maven存储库或任何其他变体简单地添加。

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) {
        }
    }
}

For web project, the lib jar file should be in the WEB-INF/lib dir. 对于Web项目,lib jar文件应位于WEB-INF / lib目录中。

Also make sure the jars in the dir are in the classpath 还要确保dir中的jar位于类路径中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM