Ask Your Question

Revision history [back]

Exception in thread "main" java.lang.IllegalArgumentException

Hi I get this error msg Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) must be > 0. Image is loaded in mat type then is converted to BufferedImage for displaying. in between some OpenCV function will be used. But i get bunch of error msg please can you tell me what is wrong?

public class Tutorial1 {

 public static void main(String[] args){

     System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
         Tutorial1 mat2Img = new Tutorial1();
         System.out.println("ok");
         mat2Img.converter();

 }

 public void converter(){

     Mat imageInMat = Imgcodecs.imread(getClass().getResource("image.jpg").getPath());
     BufferedImage bufImage =  Mat2BufferedImage(imageInMat);
     displayImage(bufImage);
 }

public 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 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);
    }}