Magpie
Magpie

Reputation: 627

How can you get the pixel dimensions of a jpg in java?

I need to find a way to work out the length and the width of the image (which is a .jpg) I have the length of the file but it is not useful because I want to make an array to store information about each pixel of the image file I am using.

public void getImageData(){
    File f= new File("myPicture.jpg");
    this.length = (int) f.length();
        System.out.println("length of file = " + length);
        BufferedImage image = null;
        try {
            image = ImageIO.read(f);
            } 
        catch (IOException e) {

            e.printStackTrace();
            }

Help much appreciated!

Upvotes: 1

Views: 1761

Answers (1)

Dan W
Dan W

Reputation: 5782

Once you get the BufferedImage, you can just call the properties getWidth() and getHeight() on it:

int imageWidth = image.getWidth();
int imageHeight = image.getHeight();

Hope this helps.

Upvotes: 4

Related Questions