I am trying to read image in java using Opencv 3.3.1 but getting this Error: "java.lang.UnsatisfiedLinkError", [closed]
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);
}}
please be so nice, and add the exact error to your question, thank you !
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:
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)
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)