搭建opencv javaweb項目
用到的技術maven、git、ssm、opencv、javaweb
搭建opencv javaweb項目時,踩了很多坑;懷疑過spring,想過python,最后竟然一不小心成了,what.......閑話不多說,讓我們看看這關鍵的一條命令
即把opencv jar包放到maven本地倉庫中
mvn install:install-file -Dfile="G:\opencv\opencv\build\java\opencv-341.jar" -DgroupId=org.opencv -DartifactId=opencv -Dversion=3.4.1 -Dpackaging=jar
再看看一直報'javaClassNotDefound'的maven依賴配置
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>3.4.1</version>
<systemPath>G:/opencv/opencv/build/java/opencv-341.jar</systemPath>
<scope>system</scope>
</dependency>
再看看不報錯的配置
<dependency>
<groupId>org.opencv</groupId>
<artifactId>opencv</artifactId>
<version>3.4.1</version>
</dependency>
到這離成功已經很近了,我們還需要加載dll或者so文件
我們可以在用到opencv的類中用靜態代碼塊加載dll或者so文件,
或者配置一個監聽器如下,別忘了在web.xml中配置
package cn.edu.njupt.configure;
import cn.edu.njupt.utils.OpencvConstantUtils;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class InitOpencv implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
public void contextInitialized(ServletContextEvent arg0) {
System.load("G:/opencv/opencv/build/java/x64/opencv_java341.dll");
}
}
web.xml
<listener>
<listener-class>cn.edu.njupt.configure.InitOpencv</listener-class>
</listener>
到此項目可以說就搭建好了,liunx,mac只需要按照上述步驟把對應文件路徑替換掉就可以了
本項目地址:https://github.com/YLDarren/stitp
相關項目地址:https://github.com/YLDarren/opencvHandleImg