Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem in reading image file

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem in reading image file

    i am getting following errors when trying to read an image files pixel:
    HTML Code:
    F:\tm\final>java pixelcheck
    1
    2
    no. of row is:-1
    no. of col is:-1
    Exception in thread "main" java.lang.NegativeArraySizeException
            at get3dimagepixelobj.get3ddata(get3dimagepixelobj.java:33)
            at pixelcheck.main(pixelcheck.java:15)
    my image file is in f:\tm\final .
    class file and image file is in same folder.


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
    class pixelcheck
    {
      public static void main(String args[])
       {
     
    	 get3dimagepixelobj j1=new get3dimagepixelobj();
     
     
    	 int[][][] datareturned = new int[400][][];//size of image file is           //400X299
    	 String fname="f2.jpeg";
    	 datareturned=j1.get3ddata(fname);
          for (int i=0;i<4 ;i++ )
          {
          System.out.println(datareturned[0][0][i]);
     
    	  }
     
       }
    }
    The other class get3dimagepixel obj is (few lines as it is very large):
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
     
    class pixeldetails
    {
    	int alpha;
    	int red;
    	int green;
    	int blue;
    	int area;
    }
    class get3dimagepixelobj extends pixeldetails
      {
        public int[][][] get3ddata(String imagefile)
    	  {
    	     //String imagefile;
             //Image rawimage=getImage(imagefile);
    		 System.out.println("1");//to check the error
    		 Image rawimage=Toolkit.getDefaultToolkit().getImage(imagefile);
    	     		 System.out.println("2");//to check the error
    		 int imagerows=rawimage.getHeight(null);
    	               System.out.println("no. of row is:"+imagerows);//to check //the error
    		 int imagecols=rawimage.getWidth(null);
    				 System.out.println("no. of col is:"+imagecols);
    		 // int imagecols=rawimage.getWidth(java.awt.image.ImageObserver);
    	     int[] onedpix=new int[imagerows*imagecols];
             int[][][] threedpix=new int[imagerows][imagecols][4];
    		 		 System.out.println("3.1");


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Problem in reading image file

    The API for Image states that getWidth/getHeight can return -1 if the height is not yet known. Use ImageIO to read the file, as using the Toolkit seems to result in this behavior (at least on my system):
    Image image = Toolkit.getDefaultToolkit().getImage("myImage.jpg");
    System.out.println(image.getWidth(null));//prints -1
    try{
    	image = ImageIO.read(new File("myImage.jpg"));
    	System.out.println(image.getWidth(null));//prints correctly
    }catch(Exception e){
    	e.printStackTrace();
    }

Similar Threads

  1. Reading a file
    By Soccer13 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 26th, 2010, 09:55 PM
  2. Help with reading from file
    By drazenmd in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 15th, 2010, 04:43 AM
  3. Reading file
    By nasi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 3rd, 2010, 04:14 AM
  4. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 09:14 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 10:31 AM