Vikrant Singh
Vikrant Singh

Reputation: 13

how to read height and width of an image without completely loading it

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.

Upvotes: 0

Views: 1150

Answers (1)

Tino M Thomas
Tino M Thomas

Reputation: 1746

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.

Upvotes: 1

Related Questions