Results 1 to 14 of 14
Thread: height of the image.
- 02-08-2010, 10:15 AM #1
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.
exception generated are :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(); } }
at javax.imageio.ImageIO.read(ImageIO.java:1275)
at ReadImage.<init>(ReadImage.java:14)
at ReadImage.main(ReadImage.java:24)
- 02-08-2010, 03:11 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You managed to miss out telling us the actual exception.
- 02-12-2010, 04:23 AM #3
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()); } }
- 02-12-2010, 07:48 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
e.printStacktrace()!
What is it with e.getMessage()?
It's next to useless for debugging...
- 02-17-2010, 08:17 AM #5
nothing worked !! and yah sorry 4 not answering back to the thread ..
- 02-17-2010, 08:21 AM #6
Actual Exception is the code is unable to read the image file ...You managed to miss out telling us the actual exception.
- 02-17-2010, 09:13 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Um, no.
You gave us a stacktrace, but you missed the first line of it which is the exception that was thrown.
- 02-18-2010, 09:04 AM #8
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?
- 02-18-2010, 09:12 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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?
- 02-18-2010, 10:50 AM #10
yeah .. this is the full stack trace ...there is nothing like FIleNotFoundException..
- 02-18-2010, 10:53 AM #11
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); } }
- 02-18-2010, 10:56 AM #12
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
- 02-18-2010, 11:19 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 02-18-2010, 12:17 PM #14
Similar Threads
-
I want set HEIGHT, WIDTH for Button
By manhtungtnk28@gmail.com in forum AWT / SwingReplies: 3Last Post: 11-16-2009, 07:26 AM -
Using setBounds (x.y,width,height) constructor
By hitmen in forum AWT / SwingReplies: 3Last Post: 03-06-2009, 12:12 PM -
Get Image Height and Width
By neeraj.singh in forum AWT / SwingReplies: 10Last Post: 02-18-2009, 01:09 PM -
Java Syntax for making 2 divs same height dynamically.
By jatrant in forum New To JavaReplies: 4Last Post: 06-25-2008, 08:09 PM -
dynamising the height of a JPopupMenu
By iimasd in forum AWT / SwingReplies: 6Last Post: 11-21-2007, 10:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks