〇、环境概述
- Java 版本8
- 当前框架 springcloud
一、引用
路径选择jar的存放路径,我此处是选择跟src同级的lib文件夹
<!--pom.xml->
<dependency>
<groupId>org.opencv</groupId>
<artifactId>core</artifactId>
<scope>system</scope>
<version>4.1.0</version>
<systemPath>${project.basedir}/lib/opencv-410.jar</systemPath>
</dependency>
二、ide设定library.path
同样的为opencv存放的文件夹
三、载入
请在你的程序入口写入,此处我在ApplicationRunner的一个实例中执行
import org.opencv.core.Core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class LoadOpencv implements ApplicationRunner {
@Value("${spring.profiles.active}")
private String active;
@Override
public void run(ApplicationArguments args) throws Exception {
if(active.equals("dev"))System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 关键句 windows
if(active.equals("prod"))System.load("/usr/lib/libopencv_java410.so"); // 关键句 linux
}
}
三、使用
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
public class OpenCvService {
public static void main(){
String path = "";
Mat mat = Imgcodecs.imread(path);
}
}