How to Get Image Width and Height using Java

Get Image Width and Height using Java

In this example, We will show you about, How to Get Image Width and Height using Java Programming language.

ImageWidthHeight.java

package com.dineshkrish.networking;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

/**
 * 
 * @author Dinesh Krishnan
 *
 */

public class ImageWidthHeight {

	public static void main(String[] args) {
		
		String imageUrl = "https://placeholdit.imgix.net/~text?txtsize=56&txt=600%C3%97400&w=600&h=400";
		
		try {
			
			// Loading Image URL
			URL url = new URL(imageUrl);
			
			BufferedImage bufferedImage = ImageIO.read(url);

			System.out.println("Image Height : "+bufferedImage.getHeight());
			System.out.println("Image Width : "+bufferedImage.getWidth());
			
		} catch (IOException e) {
			
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
	}
}

Input Image

Get Image Width and Height using Java

Output

Image Height : 400
Image Width : 600

References

1. Java ImageIO API
2. Java BufferedImage API

Hello everyone, I am a Founder of dineshkrish.com.