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
21 Comments »
Leave a Reply
-
Recent
- Check digit calculator for barcodes and credit cards
- An Excel Addin to work with Unicode CSV files
- SimpleImageInfo – A Java class to get image size without loading the whole data
- Making Scrollable Tables with fixed headers – Updated
- Migrating 10g JSF Web project with ADF Faces components in jspx pages to 11g Facelets pages using Rich components
- Monitoring Log4J messages on a browser via server push
- Managing Vimeo video player via Javascript using Moogaloop API
- Making Scrollable Tables with fixed headers
- C# FTP Client Library
-
Links
-
Archives
- April 2013 (1)
- August 2011 (1)
- January 2011 (1)
- March 2010 (1)
- February 2010 (4)
- September 2009 (1)
-
Categories
-
RSS
Entries RSS
Comments RSS
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!
Thanks for sharing.
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
Works like a charm. You saved my day! Thank you very very much.
Excellent, thanks a million. Just what I need and seems to be working perfectly.
Thank you… Solution has been indeed provided with simple logic…
[…] 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 |
very useful class, that’s what exactly I need. Thank you very much for sharing..
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…
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.
Fantastic stuff! Any idea how hard it would be to add support for Photoshop .psd image files?
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
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.
simple + works + multiple implementations + closes streams = great work!
Thanks a lot
Edy Hinzen
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
Anybody can help with adding support to image/x-icon ? 🙂
Lovely piece of work. Thanks!
I hope this can be extended to video files as well.
Wow it just saved my day !!!. Simple and to the point implementation.
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.
[…] https://jaimonmathew.wordpress.com/2011/01/29/simpleimageinfo/ […]
Pingback by How to get image height and width using java? | ASK AND ANSWER | December 31, 2015 |
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