正如Steven C所说的那样,它就像在
Extract and load DLL from JAR和
bug report一样.我对如何使用dylib略显无知,并且试图与使用“用户库”添加jar的
OpenCV tutorial保持一致,然后添加原生dylib.此外,由于某些原因加载资源,即使使用“/”从src目录加载,而不是我的项目的根目录(在我做的测试项目中就是这种情况).
对于那些试图做同样事情的人,这里有一些代码可以帮助:
private static void loadLibrary() {
try {
InputStream in = null;
File fileOut = null;
String osName = System.getProperty("os.name");
Utils.out.println(Main.class, osName);
if(osName.startsWith("Windows")){
int bitness = Integer.parseInt(System.getProperty("sun.arch.data.model"));
if(bitness == 32){
Utils.out.println(Main.class, "32 bit detected");
in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
else if (bitness == 64){
Utils.out.println(Main.class, "64 bit detected");
in = Main.class.getResourceAsStream("/opencv/x64/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
else{
Utils.out.println(Main.class, "Unknown bit detected - trying with 32 bit");
in = Main.class.getResourceAsStream("/opencv/x86/opencv_java245.dll");
fileOut = File.createTempFile("lib", ".dll");
}
}
else if(osName.equals("Mac OS X")){
in = Main.class.getResourceAsStream("/opencv/mac/libopencv_java245.dylib");
fileOut = File.createTempFile("lib", ".dylib");
}
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();
System.load(fileOut.toString());
} catch (Exception e) {
throw new RuntimeException("Failed to load opencv native library", e);
}