Java编程,opencv不满意的链接错误 [英] Java programming, opencv unsatisfied link error
问题描述
OpenCV库在捕获视频流时给出了不满意的链接错误.
OpenCV libraries gives unsatisfied link error in capturing a video stream.
opencv 3.2.0
库应该在哪里? opencv类包的正确路径是什么?
Where should be the opencv 3.2.0
libraries? What is the correct path for packages of opencv classes?
在哪里生成此错误的代码:
Code where to generate this error:
package opencv;
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
public class VideoCap
{
public static void main (String args[]){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // error
VideoCapture camera = new VideoCapture(0);
if(!camera.isOpened()){
System.out.println("Error");
}
else {
Mat frame = new Mat();
while(true){
if (camera.read(frame)){
System.out.println("Frame Obtained");
System.out.println("Captured Frame Width " +
frame.width() + " Height " + frame.height());
Imgcodecs.imwrite("camera.jpg", frame);
System.out.println("OK");
break;
}
}
}
camera.release();
}
}
例外是:
Exception in thread "main" java.lang.UnsatisfiedLinkError:
no opencv_java320 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at opencv.VideoCap.main(VideoCap.java:9)
推荐答案
下图是我的目录的内容,在该目录中装有tesseract库的opencv 4.0.1.我实际上是在Windows 10上使用mingw32 gcc编译器构建的.
The following picture is my contents of a directory in which opencv 4.0.1 installed with a tesseract library. I built it with a mingw32 gcc compiler on a windows actually 10.
我认为您至少有两个文件 opencv_javaXXX_dll 和 opencv-XXX.jar .
I think you have at least two file opencv_javaXXX_dll and opencv-XXX.jar.
我已经在Windows 10上使用mingw32 gcc编译器构建了opencv库 因此,我必须在安装mingw32和java的位置设置bin目录.
I have built the opencv library with mingw32 gcc compiler on Windows 10 So, i must set the bin directory where mingw32 installed and java.
我在cmd窗口上设置了变量,
I set the variables on a cmd window,
SET MINGW_HOME=D:/DEV/SDK/msys32/mingw32
SET JAVA_HOME=D:/DEV/SDK/JDK/jdk1.8.0_152
SET PATH=%MINGW_HOME%/bin;%JAVA_HOME%/bin
这是我的测试命令.
C:\Windows\System32\cmd.exe /C "javac -cp .;opencv-410.jar -d . *.java & java -Djava.library.path=. -cp .;opencv-410.jar VideoCap"
有三种可能的情况.
1.找不到依赖库的问题.
1. Can't find dependent libraries problem.
此问题表明您缺少某些库.
This problem indicates that you have some libraries missing.
在这种情况下,如果我在同一目录或系统路径中没有libtesseract40.dll,则应该收到此消息.
In this case, I should get this message if i don't have a libtesseract40.dll in the same directory or system's path.
2.带有库名称的不满意的链接错误.
2. An unsatisfied link error with a library name.
通常在库名称不匹配时发生. 如果我将libopencv_java410.dll作为库名,将会收到错误消息.
This commonly occurred when a name of library mismatched. I will get the error if i have a libopencv_java410.dll as a library name.
3.带有方法名称的不满意的链接错误.
3. An unsatisfied link error with a method name.
如果您还有另一个不满意的链接-方法名称.
If you have still got another unsatisfied link - method name.
例如,
java.lang.UnsatisfiedLinkError:org.opencv.core.Mat.n_eye(III)J
java.lang.UnsatisfiedLinkError: org.opencv.core.Mat.n_eye(III)J
我认为这将是一个复杂的问题.
I think this will be complicate problem.
您可能需要从头开始编译和构建库.
You might have your library compile and build from the scratch.
在Windows操作系统上,我必须在CMakefile.txt的以下行中添加一个构建标志.
On a windows os, I had to add a build flag at following line in a CMakefile.txt.
我添加了一个'--add-stdcall-alias'标志.
I added a '--add-stdcall-alias' flag.
CMakefile.txt的位置:
A location of a CMakefile.txt:
[〜opencv-4.1.0源目录] \ modules \ java \ jni \ CMakefile.txt
[~opencv-4.1.0 source directory]\modules\java\jni\CMakefile.txt
elseif(((CV_GCC OR CV_CLANG OR UNIX) OR (OPENCV_FORCE_FAT_JAVA_LIB_LD_RULES)) AND (NOT OPENCV_SKIP_FAT_JAVA_LIB_LD_RULES))
ocv_target_link_libraries(${the_module} LINK_PRIVATE -Wl,-whole-archive ${__deps} -Wl,-no-whole-archive -Wl,--add-stdcall-alias)
完整的测试代码如下:
//A test code for the opencv 4.0.1
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.videoio.VideoCapture;
public class HelloCV {
public static void test1(){
System.load(new java.io.File(".").getAbsolutePath() + java.io.File.separator + "opencv_java410.dll");
VideoCapture camera = new VideoCapture(0);
if(!camera.isOpened()){
System.out.println("Error");
}
else {
Mat frame = new Mat();
while(true){
if (camera.read(frame)){
System.out.println("Frame Obtained");
System.out.println("Captured Frame Width " +
frame.width() + " Height " + frame.height());
Imgcodecs.imwrite("camera.jpg", frame);
System.out.println("OK");
break;
}
}
}
camera.release();
}
public static void test2(){
System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // error
VideoCapture camera = new VideoCapture(0);
if(!camera.isOpened()){
System.out.println("Error");
}
else {
Mat frame = new Mat();
while(true){
if (camera.read(frame)){
System.out.println("Frame Obtained");
System.out.println("Captured Frame Width " +
frame.width() + " Height " + frame.height());
Imgcodecs.imwrite("camera.jpg", frame);
System.out.println("OK");
break;
}
}
}
camera.release();
}
public static void main (String args[]){
test1();
test2();
}
}
这篇关于Java编程,opencv不满意的链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!