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

Advertisements

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

21 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: https://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

  11. Fantastic stuff! Any idea how hard it would be to add support for Photoshop .psd image files?

    Comment by Lea Hayes | January 8, 2013 | Reply

    • For those interested, adding this seems to do the trick 🙂

      } else {
      int c4 = is.read();

      if (c1 == ‘8’ && c2 == ‘B’ && c3 == ‘P’ && c4 == ‘S’) { //PSD
      int ver = readInt(is,2,true);
      if (ver == 1) {
      is.skip(8);
      height = readInt(is,4,true);
      width = readInt(is,4,true);
      mime = “image/psd”;
      }
      }
      else if ( … ) //TIFF

      Comment by Lea Hayes | January 8, 2013 | Reply

  12. Thanks, this really saved me. Getting the size of a large batch of images with BufferedImages took way too long. Performance wise, this is a really great solution.

    Comment by Nephilion | February 23, 2013 | Reply

  13. simple + works + multiple implementations + closes streams = great work!

    Thanks a lot
    Edy Hinzen

    Comment by Edy Hinzen | July 24, 2013 | Reply

  14. Hi…this is really aweseom script..is there anyway to save the scroll position on reloading page. when reloading the page always scrolls to the top.
    Thank You

    Comment by leslie | September 5, 2013 | Reply

  15. Anybody can help with adding support to image/x-icon ? 🙂

    Comment by moonbladewolf | May 10, 2014 | Reply

  16. Lovely piece of work. Thanks!
    I hope this can be extended to video files as well.

    Comment by Raj Madhuram | December 27, 2014 | Reply

  17. Wow it just saved my day !!!. Simple and to the point implementation.

    Comment by kalyani | July 12, 2015 | Reply

  18. hmm just noticed today, for some of the images jpeg its behavior is same as ImageIO.read() in which it is inter changing width and height of a image if its in portrait mode. Not sure why this approach do works for some and not for others.

    Comment by kalyani | July 14, 2015 | Reply

  19. thanks for the class i’ve made some small adjustments and refactors , with the ability to save a file as well
    http://pastebin.com/ic5YUpRW

    Comment by Elhanan Maayan | February 7, 2016 | 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: