Results 1 to 14 of 14
  1. #1
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default height of the image.

    what is wrong with the code .. i want to read image and display its height..
    May be sumthing wrong with the path of the image ..m using ubantu.

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.io.*;
    import javax.imageio.*;
    
    
    
    public class Test1 {
    	int height;
    	public void getHeight()
    	{
    		BufferedImage img = null;
    		try{
    			img = ImageIO.read(new File("/home/sandeep/Pictures/images.jpg"));
    			height = img.getHeight();
    			}
    		catch(IOException e){
    			e.printStackTrace();
    			
    		}
    		
    	}
    	public static void main()
    	{
    		Test1 t = new Test1();
    		t.getHeight();
    	}
    
    }
    exception generated are :

    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at ReadImage.<init>(ReadImage.java:14)
    at ReadImage.main(ReadImage.java:24)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    You managed to miss out telling us the actual exception.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Paths can be tricky. Try opening things up a bit:
    Java Code:
    public class Test {
        private int getHeight() {
            int height = -1;
            String path = "/home/sandeep/Pictures/images.jpg";
            try{
                File file = new File(path);
                URI uri = file.toURI();
                System.out.println("uri = " + uri);
                URL url = uri.toURL();
                System.out.println("url = " + url);
                BufferedImage img = ImageIO.read(file);
                height = img.getHeight();
            } catch(IOException e) {
                System.out.println(e.getMessage());
            }
            return height;
        }
    
        public static void main(String[] args) {
            System.out.println("height = " + new Test().getHeight());
        }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    e.printStacktrace()!

    What is it with e.getMessage()?
    It's next to useless for debugging...

  5. #5
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    nothing worked !! and yah sorry 4 not answering back to the thread ..

  6. #6
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    You managed to miss out telling us the actual exception.
    Actual Exception is the code is unable to read the image file ...

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Um, no.
    You gave us a stacktrace, but you missed the first line of it which is the exception that was thrown.

  8. #8
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    okay the exception was as below:

    javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1275)
    at Test.getHeight(Test.java:12)
    at Test.main(Test.java:28)

    ...

    i also want to detect the file format of the image .. can u suggest some code?

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    There you go then.
    It can't read the file.
    This exception usually has another exception wrapped in it...is this the full stack trace, or is there (possibly) a FileNotFoundException in there as well?

  10. #10
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    yeah .. this is the full stack trace ...there is nothing like FIleNotFoundException..

  11. #11
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    hey Tolls.
    i did some R & D on this stuff.
    Actually there is something wrong with the file itself .. when i put some another jpeg file in that directory .. and Violla.. it worked..
    Now i can read the height, width ,format of the file.. after playing with the code ..
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.Iterator;
    import java.awt.image.*;
    import javax.imageio.ImageIO;
    import javax.imageio.ImageReader;
    import javax.imageio.stream.ImageInputStream;
    
    public class Test {
    	int height;
    	int width;
    	String formatName;
    	
    	//CONSTRUCTOR OF THE CLASS Test.
    	public Test()
    	{
    		BufferedImage img = null;
    		try{
    			File file = new File("/home/sandeep/TestCodes/test/bin/j.jpg");
    			img = ImageIO.read(file);
    			
    			height = img.getHeight();
    			width = img.getWidth();
    			//System.out.println(height);
    			//System.out.println(img.getWidth());
    			//System.out.println(img.getType());
    		    formatName = getFormatName(file);
    			//System.out.println(formatName);
    			}
    		catch(IOException e){
    			e.printStackTrace();
    			}
    		
    	}//CONSTRUCTOR ENDS
    	
    	//FUNCTION TO GET FORMAT NAME OF THE IMAGE FILE.
    	private static String getFormatName(Object o) {
    	    try {
    	        // Create an image input stream on the image
    	        ImageInputStream iis = ImageIO.createImageInputStream(o);
    
    	        // Find all image readers that recognize the image format
    	        Iterator iter = ImageIO.getImageReaders(iis);
    	        if (!iter.hasNext()) {
    	            // No readers found
    	            return null;
    	        }
    
    	        // Use the first reader
    	        ImageReader reader = (ImageReader)iter.next();
    
    	        // Close stream
    	        iis.close();
    
    	        // Return the format name
    	        return reader.getFormatName();
    	    } catch (IOException e) {
    	    }
    	    // The image could not be read
    	    return null;
    	}
    	//FUNCTION ENDS.
    	
    	
    	
    	public static void main(String []args)
    	{
    		Test t = new Test();
    		System.out.println("Height of the Image : "+t.height);
    		System.out.println("Width of the Image  : "+t.width);
    		System.out.println("Format of the Image : "+t.formatName);
    	}
    
    }

  12. #12
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    I am working on a project to extract text(Handwritten + printed) from images , jpeg .. and thus playing with the API's to explore ..
    I need some good guidance and support from the forum... can u point some way .. in Java

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    OK.
    So the jpg file was corrupt, as far as ImageIO was concerned.

    For text identification do a search on the forums. That has been asked a few times over the past year at least. One or two of them have some possible links, or advice.

  14. #14
    programmer_007's Avatar
    programmer_007 is offline Senior Member
    Join Date
    Aug 2009
    Posts
    125
    Rep Power
    0

    Default

    Arrite .... lets see where it goes !! already googled a lot ..

Similar Threads

  1. I want set HEIGHT, WIDTH for Button
    By manhtungtnk28@gmail.com in forum AWT / Swing
    Replies: 3
    Last Post: 11-16-2009, 08:26 AM
  2. Using setBounds (x.y,width,height) constructor
    By hitmen in forum AWT / Swing
    Replies: 3
    Last Post: 03-06-2009, 01:12 PM
  3. Get Image Height and Width
    By neeraj.singh in forum AWT / Swing
    Replies: 10
    Last Post: 02-18-2009, 02:09 PM
  4. Replies: 4
    Last Post: 06-25-2008, 09:09 PM
  5. dynamising the height of a JPopupMenu
    By iimasd in forum AWT / Swing
    Replies: 6
    Last Post: 11-21-2007, 11:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •