Skip to main content

APIzation: Replication Package

[Q#42159428][A#42159770] how can I get height and width of a photo that I downloaded from internet in Java?

https://stackoverflow.com/q/42159428

I'm downloading a photo from url address with the following code: But how can I additionaly display width and height of that image?

Answer

https://stackoverflow.com/a/42159770

You can use BufferedImage it has methods getHeight() & getWidth() in Java 7. BufferedImage Code for the same :

APIzation

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();
        }
package com.stackoverflow.api;

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

public class Human42159770 {

  public static void printDimensionsOfBufferedImage(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();
    }
  }
}

package com.stackoverflow.api;

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

/**
 * how can I get height and width of a photo that I downloaded from internet in Java?
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/42159770">https://stackoverflow.com/a/42159770</a>
 */
public class APIzator42159770 {

  public static void getHeight() throws Exception {
    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();
    }
  }
}