How to Get Image Height and Width Using Java

Is there any way in Java to take image width and height without transfer or download?

First, your questions:

  1. Kind of. First of all, the ImageIO.read(...) methods, are convenience methods that will decode the first image in a file, using all default parameters for decoding. As all ImageIO.read(...) operations are delegated to format specific plugins or ImageReader instances (like JPEGImageReader), this may be plugin specific. But the general case, is that the image data is transferred and decoded "on the fly". Note that there's no "size calculation" here, rather the entire image is decoded to a BufferedImage in this case. As much data as is needed to decode the first image in the file has to be transferred, but not necessarily stored anywhere on the computer, other than the decoded pixel values.

  2. It depends. There's a setting ImageIO.setUseCache(boolean), that controls whether the data is cached on disk or in RAM.

    • If the setting is true, the data is temporarily stored on disk, typically in the default temp directory (see java.io.tempdir System property).
    • If the setting is false the data is temporarily stored in memory while the image is decoded.
  3. No, not unless your server has a special API to give you this data, and you perform specific requests against this API. However, the ImageIO API does have a lot more granular methods that allows you to get the image dimensions a lot faster, ans without downloading/decoding the entire image up front.

The faster way of obtaining the image dimensions is:

try (InputStream stream = url.openStream()) {
// The "useCache" setting will decide whether "input" below
// will be disk or memory cached
try (ImageInputStream input = ImageIO.createImageInputStream(stream)) {
ImageReader reader = ImageIO.getImageReaders(input).next(); // TODO: Handle no reader
try {
reader.setInput(input);

// Get dimensions of first image in the stream, without decoding pixel values
int width = reader.getWidth(0);
int height = reader.getHeight(0);
}
finally {
reader.dispose();
}
}
}

Again, depending on image format, the above code should only read as much of the header/meta data as is needed to determine the image dimensions. Most ImageReader implementations will read all header data for this, but still, it's much faster and involves a lot less data (and memory) than decoding the entire image.

It's hard to make any assumptions as to how much data is or needs to be downloaded, because different formats have headers of varying size, the underlying transport (ie. HTTP) may transfer data in "chunks" etc.

How can I get height and width of an image?

Use ImageIO.read(URL) or ImageIO.read(File) instead. It will block while loading, and the image width & height will be known after it returns.

E.G.

Sample Image

import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class SizeOfImage {

public static void main(String[] args) throws Exception {
URL url = new URL("https://i.stack.imgur.com/7bI1Y.jpg");
final BufferedImage bi = ImageIO.read(url);
final String size = bi.getWidth() + "x" + bi.getHeight();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JLabel l = new JLabel(
size,
new ImageIcon(bi),
SwingConstants.RIGHT );
JOptionPane.showMessageDialog(null, l);
}
});
}
}

Alternately, add a MediaTracker to the image being loaded asynchronously by the Toolkit and wait until it is completely loaded.

how can I get height and width of a photo that I downloaded from internet in Java?

You can use BufferedImage it has methods getHeight() & getWidth() in Java 7.

BufferedImage

Code for the same :

BufferedImage imo;
try {
imo = ImageIO.read(new File("location_of_file"));
System.out.println(imo.getHeight());
System.out.println(imo.getWidth());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Related Topics



Leave a reply



Submit