Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/ImageIO getting image dimensions without reading the entire file?

Is there a way to get the dimensions of an image without reading the entire file?

URL url=new URL(<BIG_IMAGE_URL>); BufferedImage img=ImageIO.read(url); System.out.println(img.getWidth()+" "+img.getHeight()); img=null; 
like image 905
Pierre Avatar asked Oct 13 '09 10:10

Pierre


People also ask

Can ImageIO read JPG?

imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP. Image I/O is also extensible so that developers or administrators can "plug-in" support for additional formats. For example, plug-ins for TIFF and JPEG 2000 are separately available.

What kind of exception does the ImageIO class cause?

imageio. ImageIO. write and that failure is causing a null pointer exception.

What does ImageIO write do?

The ImageIO. write method calls the code that implements PNG writing a “PNG writer plug-in”. The term plug-in is used since Image I/O is extensible and can support a wide range of formats.

What is the use of ImageIO class?

ImageIO provides ImageReader and ImageWriter plug-ins for the Graphics Interchange Format (GIF) image format.


1 Answers

try(ImageInputStream in = ImageIO.createImageInputStream(resourceFile)){     final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);     if (readers.hasNext()) {         ImageReader reader = readers.next();         try {             reader.setInput(in);             return new Dimension(reader.getWidth(0), reader.getHeight(0));         } finally {             reader.dispose();         }     } }  

Thanks to sfussenegger for the suggestion

like image 78
Sam Barnum Avatar answered Oct 14 '22 00:10

Sam Barnum