0

I am using some application where I have several image URLs loaded on local system. I need to get height and width of each image for which I am using ImageIO.read(File). This does the work but first loads the complete image into the memory and then lets me get the data. However I would like to know if there is any way I can get the image dimensions without loading or partly loading the image into the memory.

  • Have a look [here](https://stackoverflow.com/questions/18507180/get-image-dimension-and-image-size-from-binary) - they talk about an external library that you maybe could use for that. – sarkasronie Mar 28 '18 at 09:00
  • Possible duplicate of [How to get image height and width using java?](https://stackoverflow.com/questions/672916/how-to-get-image-height-and-width-using-java) – Boaz Mar 28 '18 at 09:01
  • In particular, see [this answer](https://stackoverflow.com/a/2911772/1889273) from the suggested duplicate. – Boaz Mar 28 '18 at 09:02
  • Please see [this](https://stackoverflow.com/questions/1559253/java-imageio-getting-image-dimensions-without-reading-the-entire-file) also – Tino M Thomas Mar 28 '18 at 09:04

1 Answers1

1

Refer this and this links

Obtaining an ImageInputStream is straightforward. Given an input source in the form of a File or InputStream, an ImageInputStream is produced by the call:

Object source; // File or InputStream
ImageInputStream iis = ImageIO.createImageInputStream(source);

Once a source has been obtained, it may be attached to the reader by calling:

reader.setInput(iis, true);

The second parameter should be set to false if the source file contains multiple images and the application is not guaranteed to read them in order. For file formats that allow only a single image to be stored in a file, it is always legal to pass in a value of true.

Once the reader has its input source set, we can use it to obtain information about the image without necessarily causing image data to be read into memory. For example, calling reader.getImageWidth(0) and reader.getImageHeight(0) allows us to obtain the width and height of the first image stored in the file respectively. A well-written plug-in will attempt to decode only as much of the file as is necessary to determine the image width, without reading any pixels.

Tino M Thomas
  • 1,676
  • 2
  • 16
  • 24
  • ImageIo is already taking care of what you are suggesting in the read() method itself where it creates the ImageInputStream and passes it to the reader thus enabling me to get the dimensions. – Vikrant Singh Mar 28 '18 at 09:27
  • You pasted text from a official document. In that case please use the text in quoted form! – zappee Mar 28 '18 at 09:27