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<ImageReader> iterator = ImageIO.getImageReadersBySuffix(suffix);
    if (iterator.hasNext()) {
        final ImageReader reader =;
        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;
Tagged in : #Java


A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (, DZone MVB and founder of

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.