I am trying to read image in java using Opencv 3.3.1 but getting this Error: "java.lang.UnsatisfiedLinkError", [closed]

asked 2017-11-30 13:24:58 -0600

fahad_nadeem gravatar image

updated 2017-11-30 13:45:53 -0600

Code and Exception snippet is giving here..

Exception::


Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.imgcodecs.Imgcodecs.imread_0(Ljava/lang/String;I)J at org.opencv.imgcodecs.Imgcodecs.imread_0(Native Method) at org.opencv.imgcodecs.Imgcodecs.imread(Imgcodecs.java:109) at simple.SimpleSample.main(SimpleSample.java:26) C:\Users\muhammad adeel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)


Code:::

public class SimpleSample {
public static void main(String[] args){
    Imgcodecs imgcodecs = new Imgcodecs();
    Mat img = imgcodecs.imread("E:\\OpenCV_Project\\Java\\Images\\Lenna.jpg");
    Image image = null;

    image = Mat2BufferedImage(img);
    displayImage(image);

}
public static BufferedImage Mat2BufferedImage(Mat m){

// source: http://answers.opencv.org/question/10344/opencv-java-load-image-to-gui/
// Fastest code
// The output can be assigned either to a BufferedImage or to an Image

int type = BufferedImage.TYPE_BYTE_GRAY;
if ( m.channels() > 1 ) {
    type = BufferedImage.TYPE_3BYTE_BGR;
}
int bufferSize = m.channels()*m.cols()*m.rows();
byte [] b = new byte[bufferSize];
m.get(0,0,b); // get all the pixels
BufferedImage image = new BufferedImage(m.cols(),m.rows(), type);
final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
System.arraycopy(b, 0, targetPixels, 0, b.length);  
return image;
}

public static void displayImage(Image img2)
{   
//BufferedImage img=ImageIO.read(new File("/HelloOpenCV/lena.png"));
ImageIcon icon=new ImageIcon(img2);
JFrame frame=new JFrame();
frame.setLayout(new FlowLayout());        
frame.setSize(img2.getWidth(null)+50, img2.getHeight(null)+50);     
JLabel lbl=new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-09-26 13:16:37.170969

Comments

please be so nice, and add the exact error to your question, thank you !

berak gravatar imageberak ( 2017-11-30 13:36:32 -0600 )edit

btw, i do not see, where your code loads the c++ native opencv dll/so. can it be, that there's some code missing ? there should be a block before main(), like:

 static {
       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 }
berak gravatar imageberak ( 2017-11-30 13:43:37 -0600 )edit
1

after adding the above code I get this error Error::

java.lang.UnsatisfiedLinkError: no opencv_java331 in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867) at java.lang.Runtime.loadLibrary0(Runtime.java:870) at java.lang.System.loadLibrary(System.java:1122) at simple.SimpleSample.<clinit>(SimpleSample.java:27) Exception in thread "main" C:\Users\muhammad adeel\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

fahad_nadeem gravatar imagefahad_nadeem ( 2017-11-30 13:48:59 -0600 )edit

so, your netbeans ide found the opencv-331.jar, but not the opencv_java330.dll

put it on the PATH, or copy it next to your class (no idea about netbeans, not using any of that)

berak gravatar imageberak ( 2017-11-30 14:01:52 -0600 )edit