Is there any way in Java to take image width and height without transfer or download?
First, your questions:
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 allImageIO.read(...)
operations are delegated to format specific plugins orImageReader
instances (likeJPEGImageReader
), 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 aBufferedImage
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.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 (seejava.io.tempdir
System property). - If the setting is
false
the data is temporarily stored in memory while the image is decoded.
- If the setting is
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.
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
Eclipse No Tests Found Using Junit 5 Caused by Noclassdeffounderror for Launcherfactory
How to Append a Query Parameter to an Existing Url
Java.Sql.Sqlexception: Field Doesn't Have a Default Value
How to Call a Function Only After the Previous Function Is Fully Executed in Java
Type Safety: Unchecked Cast from Object to Arraylist<Myvariable>
How to Exclude Classes from Test Coverage Using Java Annotations in Spring Boot
Setting a Log File Name to Include Current Date in Log4J
Why Is Git Bash Not Using the Correct Java Path as Defined in the Path Environment Variable
Kafka: How to Delete Records from a Topic Using Java API
How to Autowire a Component Which Is Having Constructor With Arguments in Springboot Application
How to Properly Re-Run Spring Boot Application from Eclipse
How to Count Method Calls by Instance
How to Avoid Thread.Sleep in Unit Tests
How to Evaluate a Math Expression Given in String Form