简体   繁体   English

OpenCV + Java = UnsatisfiedLinkError

[英]OpenCV + Java = UnsatisfiedLinkError

I need capture a video stream from my USB webcam, for this i use Opencv 2.4.6 for developing in Java.我需要从我的 USB 网络摄像头捕获视频流,为此我使用 Opencv 2.4.6 进行 Java 开发。 I follow the steps listed in here我按照此处列出的步骤操作

I add the "C:\\opencv\\build\\java\\x64" dir to my System PATH and include the "opencv-246.jar" file into my libraries on ECLIPSE.我将“C:\\opencv\\build\\java\\x64”目录添加到我的系统路径中,并将“opencv-246.jar”文件包含到我在 ECLIPSE 上的库中。 When y run the explame当你运行这个解释

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

public class Main {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        Mat m  = Mat.eye(3, 3, CvType.CV_8UC1);
        System.out.println("m = " + m.dump());
    }
}

i get我明白了

m = [1, 0, 0;
  0, 1, 0;
  0, 0, 1]

OK =)好的 =)

but when i run但是当我跑步时

import org.opencv.highgui.VideoCapture;

public class Main {
    public static void main(String[] args) {
        VideoCapture vc = new VideoCapture(0);
        if(vc.isOpened()){
            System.out.println("Works!");
        }
    }
}

i get我明白了

Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.highgui.VideoCapture.n_VideoCapture(I)J
    at org.opencv.highgui.VideoCapture.n_VideoCapture(Native Method)
    at org.opencv.highgui.VideoCapture.<init>(VideoCapture.java:113)
    at Main.main(Main.java:5)

i add all the routes containes in:我添加了所有包含的路由:

C:\\opencv\\build\\x64\\vc10 C:\\opencv\\build\\x64\\vc10

one by one,but doesn`t work.一个一个,但不起作用。

Finally i create a variable called OPENCV_DIR with C:\\opencv\\build\\x64\\vc10 but still getting UnsatisfiedLinkError.最后,我用 C:\\opencv\\build\\x64\\vc10 创建了一个名为 OPENCV_DIR 的变量,但仍然得到 UnsatisfiedLinkError。

PLEASE HELP ME!请帮我!

in your second example , you skipped this line在你的第二个例子中,你跳过了这一行

 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

so the opencv libs werent loaded, UnsatisfiedLinkError, etc...所以opencv库没有加载,UnsatisfiedLinkError等......

[edit]: [编辑]:

thanks to @Jishnu Prathap for highlighting the java.library path issue, if you run into problems setting that, you can still try to use an absolute path to the java wrapper so/dll/dylib like:感谢@Jishnu Prathap 突出显示 java.library 路径问题,如果您在设置时遇到问题,您仍然可以尝试使用 java 包装器 so/dll/dylib 的绝对路径,例如:

 System.load("/path to/our/java_wrapper");

I had a similar error while using OpenCV with java.I did 2 things to resolve it.我在使用 OpenCV 和 java 时遇到了类似的错误。我做了两件事来解决它。

  1. static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
  2. I added the path to OpenCV dll or .so to javalibpath or path.我将 OpenCV dll 或 .so 的路径添加到 javalibpath 或路径。 which actually didnt work for some reason and i ended up putting the OpenCV dll in the system32 folder .这实际上由于某种原因不起作用,我最终将OpenCV dll 放在 system32 文件夹中

For general users using opencv3.x:对于使用 opencv3.x 的一般用户:

HighGUI module does not exist anymore in Java for opencv 3.0 and above.对于 opencv 3.0 及更高版本,Java 中不再存在 HighGUI 模块。

import org.opencv.videoio.VideoCapture;

instead of代替

import org.opencv.highgui.VideoCapture;

videoio includes VideoCapture, VideoWriter. videoio 包括 VideoCapture、VideoWriter。

Similarly:相似地:

imgcodecs includes imread/imwrite and friends imgcodecs 包括 imread/imwrite 和朋友

Example:例子:

Highgui.imread(fileName)

--> -->

 Imgcodecs.imread(fileName)

Try the below code试试下面的代码

import org.opencv.core.CvType;导入 org.opencv.core.CvType; import org.opencv.core.Mat;导入 org.opencv.core.Mat;

import nu.pattern.OpenCV;导入 nu.pattern.OpenCV;

public class OpencvMain {公共类 OpencvMain {

public static void main( String[] args )
   {

      OpenCV.loadLocally();
      Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
      System.out.println( "mat = " + mat.dump() );
   }

} }

So, I was having this problem too and I did what you all suggested, it worked fine in my x64 windows, but in a x86 couldn't make it work.所以,我也遇到了这个问题,我按照你们的建议做了,它在我的 x64 窗口中运行良好,但在 x86 中无法正常工作。

At last I found a solution by changing:最后我通过更改找到了解决方案:

VideoCapture capture = new VideoCapture(0);

for为了

    VideoCapture capture = new VideoCapture();
    capture.open("resources/vid.MP4");

I don't know why this worked but I hope it may help somebody with my same problem.我不知道为什么这行得通,但我希望它可以帮助解决我同样问题的人。

I tried a lot of tutorials online for the resolution, only one of them have helped me.我在网上尝试了很多教程来解决这个问题,只有其中一个对我有帮助。 There are two steps that are different in this method,这种方法有两个不同的步骤,

Firstly, while importing the java project from Opencv SDK into the Android studio, make sure to uncheck all the checkboxes presented in the import dialog.首先,在将 Java 项目从 Opencv SDK 导入 Android Studio 时,确保取消选中导入对话框中显示的所有复选框

Secondly, make sure you import the OpenCV.mk file that is in the native/jdk of the SDK..其次,请确保您导入了 SDK 的 native/jdk 中的OpenCV.mk文件。

The System.loadLibrary() seems to return true after this, which was a huge relief for me as it took me several hours to figure this out System.loadLibrary() 似乎在此之后返回 true,这对我来说是一个巨大的解脱,因为我花了几个小时才弄清楚这一点

Here's the link to the tutorial that helped me https://medium.com/@rdeep/android-opencv-integration-without-opencv-manager-c259ef14e73b这是帮助我的教程的链接https://medium.com/@rdeep/android-opencv-integration-without-opencv-manager-c259ef14e73b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM