Tutorials Java How to Calculate Image Dimensions in Java

How to Calculate Image Dimensions in Java

The following code snippet shows how to calculate image dimension in java by reading the file from specified path.

public static Dimension getImageDimension(final File path) {
    if (path == null) return null;

    Dimension result = null;
    String suffix = getFileExtension(path.toString());
    if (suffix.startsWith(".")) {
        suffix = suffix.substring(1);

    final Iterator iterator = ImageIO.getImageReadersBySuffix(suffix);
    if (iterator.hasNext()) {
        final ImageReader reader = iterator.next();
        try {
            final ImageInputStream stream = new FileImageInputStream(path);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            result = new Dimension(width, height);
        } catch (IOException e) {
        } finally {
    } else {
        logger.error("No reader found for given format: " + suffix);
    return result;
Leave a Reply

Your email address will not be published.

Previous Post
Install WordPress Theme

Beginners Guide: How to Install a WordPress Theme

Next Post

How to Generate Unique File Name When Saving a File in Java

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All