68

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;
silver est
  • 51
  • 6
Pierre
  • 34,472
  • 31
  • 113
  • 192
  • Did you try truncating image content, just passing a small portion, say 10kb, of the image to ImageIO.read? – Mohsen Oct 13 '09 at 10:11
  • possible duplicate of [How to get image height and width using java?](http://stackoverflow.com/questions/672916/how-to-get-image-height-and-width-using-java) – ripper234 Jan 31 '12 at 08:52
  • No it is not a duplicate of that question. – Stephen C Sep 18 '21 at 08:05

4 Answers4

123
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

Danielson
  • 2,605
  • 2
  • 28
  • 51
Sam Barnum
  • 10,559
  • 3
  • 54
  • 60
  • 3
    Awesome... Your code worked me perfect. I tried couple of code, but those codes are failed in some use cases. You saved my time... Thank you. – vissu Dec 20 '11 at 12:02
  • 1
    The code presented issues a warning in Eclipse: Iterator is a raw type. References to generic type Iterator should be parameterized. Remedy options offered by Eclipse: rename in file (Ctrl+2, R direct access), Add type parameters to 'Iterator', Infer Generic Type Arguments..., Add @SuppressWarnings 'unchecked' to 'methodNameHere'. What's the syntactically correct fix for it? – David Aug 22 '12 at 23:40
  • 1
    Also the code snippet doesn't mention the class references needed to use the code. From Eclipse, seems to be: import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; – David Aug 22 '12 at 23:41
  • Thanks @David, fixed the casting issues. Those look like the right packages. – Sam Barnum Jul 30 '13 at 23:22
  • Man, you're awesome, you should know it – Alexander.Iljushkin Oct 03 '14 at 14:24
  • 2
    @Sam Barnum. How do you get the parameter 'resourceFile' without downloading the whole image? OP starts with an arbitrary URL, not a file. – E L Apr 11 '15 at 22:40
  • 2
    @EL it accepts a File, readable RandomAccessFile, or InputStream – Sam Barnum Apr 13 '15 at 15:41
  • Any chance anyone could explain me the image index of `imageReader.getWidth(/*image index*/ 0 )`? Could this ever be any other value than 0? Thank you in advance. – Claus Polanka Feb 28 '23 at 18:15
  • 1
    @ClausPolanka some image file formats can contain multiple images, like GIF and multi-image JPEG – Sam Barnum Mar 03 '23 at 21:19
17

Using ImageReader.getHeight(int) and ImageReader.getWidth(int) normally only reads the image header (I'm looking at JDK6 sources). So ImageReader is most likely the best choice.

sfussenegger
  • 35,575
  • 15
  • 95
  • 119
1

You'll have to look into ImageReader.getImageMetadata(). Unfortunately, The Java Image API is not at all easy to use.

You can find descriptions of the metadata formats in the package documentation of javax.imageio.metadata.

There are third party libraries that are easier to use, such as MediaUtil (last updated 3 years ago, but it worked well for me).

alex
  • 479,566
  • 201
  • 878
  • 984
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

The solution with ImageInputStream and ImageReader is still not so efficient though because they create tmp files. It becomes much slower when image is larger or concurrency is higher, i recommend to use metadata-extractor instead.

kensou97
  • 21
  • 6