Posted by & filed under Coding.

Earlier this week I Googled for how to find the dimensions of an image using Java, and most of the solutions were way more complicated than they needed to be. Most of what I came across was code that opened the image and parsed through the binary code to get the dimensions.

Yes, there’s an easier way.

The BufferedImage class has a couple built in methods that will get the dimensions without much trouble at all thanks to the getHeight and getWidth methods.

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;

public class ImageSize {
	public static void main(String[] args) {

		BufferedImage img = ImageIO.read(new File("filename.jpg"));

		System.out.println("Width = " + img.getWidth());
		System.out.println("Height = " + img.getHeight());

	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *