I'm trying for too long to setup a Java env with OpenCV, I was able to do it for Android but not for a simple Java project.
I have vscode with all the extensions, and set up a project with the jar in the .classpath
And I get:
java.lang.UnsatisfiedLinkError: org.opencv.core.Core
I understand that the problem is that the actual OpenCV-420.dll
is not set up, but I cannot find any documentation on how to set it up in vscode.
I tried in Ecplise ( https://opencv-java-tutorials.readthedocs.io/en/latest/01-installing-opencv-for-java.html ), but I get the same error
This works for me:
package app;
import java.lang.reflect.Field;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
// import org.opencv.core.Core;
public class App {
public static void main(String[] args) {
try {
App.loadOpenCV_Lib();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat mat = Mat.eye(3, 3, CvType.CV_8UC1);
System.out.println("mat = " + mat.dump());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void loadOpenCV_Lib() throws Exception {
// get the model
String model = System.getProperty("sun.arch.data.model");
// the path the .dll lib location
String libraryPath = "D:/opencv/build/java/x86/";
// check for if system is 64 or 32
if(model.equals("64")) {
libraryPath = "D:/opencv/build/java/x64/";
}
// set the path
System.setProperty("java.library.path", libraryPath);
Field sysPath = ClassLoader.class.getDeclaredField("sys_paths");
sysPath.setAccessible(true);
sysPath.set(null, null);
// load the lib
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
explain: the loadOpenCV_Lib will import the package on the fly with reflect
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.