Jaimon's Blog

SimpleImageInfo – A Java class to get image size without loading the whole data


SimpleImageInfo is a Java class to find out an image’s size and mime type without reading the whole image data. It doesn’t have any dependency on AWT or any other libraries. Currently it supports the following image types

  • JPG
  • GIF
  • PNG
  • BMP
  • TIFF

Because it only read the first few bytes, in most cases it only takes less than a millisecond to return the values.

Usage

Getting image data from a byte array

 

SimpleImageInfo imageInfo = new SimpleImageInfo(bytes);
System.out.println("MIME type : " + imageInfo.getMimeType() + " width : " + imageInfo.getWidth() + " height : " + imageInfo.getHeight());

Getting image data from a file

 

SimpleImageInfo imageInfo = new SimpleImageInfo(file);
System.out.println(imageInfo);


Download the source code

Please click here to download the source code.

HTH

Jaimon

January 29, 2011 - Posted by | Uncategorized | , ,

10 Comments »

  1. hahaha… nice nice nice…!!
    useful, actually this is the alternative
    rather than getting the node using XML nodes
    from basic image observer (oracle).

    (^^’// great thanks!

    Comment by g | March 15, 2011 | Reply

  2. Thanks for sharing.

    Comment by gshegosh | May 13, 2011 | Reply

  3. Very nice piece of work. We’ve used it in making a patch of Apache PDFBox code run on Google App Engine (which lacks most of AWT). In fact, we’ve submitted as part of a patch to PDFBox — hope the patch is accepted!

    Thanks a lot for good work and for sharing.
    –Ken Bowen

    Comment by Ken Bowen | June 10, 2011 | Reply

  4. Works like a charm. You saved my day! Thank you very very much.

    Comment by fabio surini | June 17, 2011 | Reply

  5. Excellent, thanks a million. Just what I need and seems to be working perfectly.

    Comment by Brian O Carroll - Dublin, Ireland | September 5, 2011 | Reply

  6. Thank you… Solution has been indeed provided with simple logic…

    Comment by prashanth | September 21, 2011 | Reply

  7. [...] Hilfe der Klasse SimpleImageInfo (Download unter: http://jaimonmathew.wordpress.com/2011/01/29/simpleimageinfo/) kann man die Höhe und Breite eines JPG wirklich einfach [...]

    Pingback by Java – Höhe und Breite eine JPG ermitteln « Srccode's Blog | October 4, 2011 | Reply

  8. very useful class, that’s what exactly I need. Thank you very much for sharing..

    Comment by Talha Kabakuş | October 12, 2011 | Reply

  9. Fantastic, exactly what i was looking for. Needed to use this with this component : http://codecanyon.net/item/smooth-zoom-pan-jquery-image-viewer/511142
    which requires to have image size information in order to be able to display correctly…

    Comment by Cédric NICOLAS | October 31, 2011 | Reply

  10. Nice peace of code. Thanx a lot!

    I’ve integrate it in my project Java Image Info (http://th-schwarz.github.com/JII/) which provides a simple interface to libs and codes that reading basic values from images like the size.

    Comment by Thilo | January 1, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.