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

4

67

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;
Jamey answered 13/10, 2009 at 10:1 Comment(3)
Did you try truncating image content, just passing a small portion, say 10kb, of the image to ImageIO.read?Troupe
possible duplicate of How to get image height and width using java?Rhomboid
No it is not a duplicate of that question.Chesnut
C
122
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

Cohla answered 13/10, 2009 at 12:55 Comment(7)
The code presented issues a warning in Eclipse: Iterator is a raw type. References to generic type Iterator<E> 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?Myelitis
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;Myelitis
Thanks @David, fixed the casting issues. Those look like the right packages.Cohla
@Sam Barnum. How do you get the parameter 'resourceFile' without downloading the whole image? OP starts with an arbitrary URL, not a file.Devereux
@EL it accepts a File, readable RandomAccessFile, or InputStreamCohla
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.Ri
@ClausPolanka some image file formats can contain multiple images, like GIF and multi-image JPEGCohla
S
16

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.

Sledge answered 13/10, 2009 at 10:23 Comment(0)
T
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).

Throwback answered 13/10, 2009 at 10:17 Comment(0)
E
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.

Edgeworth answered 18/9, 2021 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.