OpenCV + Java = UnsatisfiedLinkError
Asked Answered
D

6

25

I need capture a video stream from my USB webcam, for this i use Opencv 2.4.6 for developing in 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. 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

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.

PLEASE HELP ME!

Dutton answered 5/9, 2013 at 0:34 Comment(0)
C
33

in your second example , you skipped this line

 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

so the opencv libs werent loaded, UnsatisfiedLinkError, etc...

[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:

 System.load("/path to/our/java_wrapper");
Coster answered 5/9, 2013 at 7:20 Comment(1)
static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); } to make sure it's loaded one time..Siglos
R
4

I had a similar error while using OpenCV with java.I did 2 things to resolve it.

  1. static{ System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }
  2. I added the path to OpenCV dll or .so to javalibpath or path. which actually didnt work for some reason and i ended up putting the OpenCV dll in the system32 folder.
Rubinrubina answered 23/5, 2015 at 18:52 Comment(0)
E
3

Try the below code

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

import nu.pattern.OpenCV;

public class OpencvMain
{
    public static void main( String[] args )
    {
        OpenCV.loadLocally();
        Mat mat = Mat.eye( 3, 3, CvType.CV_8UC1 );
        System.out.println( "mat = " + mat.dump() );
    }
}
Elaterin answered 30/5, 2018 at 5:32 Comment(0)
P
1

For general users using opencv3.x:

HighGUI module does not exist anymore in Java for opencv 3.0 and above.

import org.opencv.videoio.VideoCapture;

instead of

import org.opencv.highgui.VideoCapture;

videoio includes VideoCapture, VideoWriter.

Similarly:

imgcodecs includes imread/imwrite and friends

Example:

Highgui.imread(fileName)

-->

 Imgcodecs.imread(fileName)
Pointtopoint answered 2/8, 2017 at 22:58 Comment(0)
W
0

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.

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.

Wordbook answered 16/12, 2015 at 20:27 Comment(0)
R
0

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.

Secondly, make sure you import the OpenCV.mk file that is in the native/jdk of the SDK..

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

Here's the link to the tutorial that helped me https://medium.com/@rdeep/android-opencv-integration-without-opencv-manager-c259ef14e73b

Refresh answered 21/1, 2021 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.