cancel
Showing results for 
Search instead for 
Did you mean: 

Get size of Image

Former Member
0 Kudos

Hi All

I have uploaded a photo to a file server.

Now while displaying the image in the UI I need to check the size of the image.

How can i get the size of the image

Please help

Regard

Sonal Mangla

Accepted Solutions (0)

Answers (3)

Answers (3)

sid_sunny
Contributor
0 Kudos

Hi Sonal,

If you are reading the file in bytes the try this:

FileInputStream is = new FileInputStream(file);

long length = file.length();

This will give you the file size in bytes.

<b>Do reward points if it helps.</b>

Regards

Sid

Former Member
0 Kudos

Oops!!!

I want the height and width of the image

Sorry for the confusion.

Regards

Sonal Mangla

Sigiswald
Contributor
0 Kudos

Hi Sonal,

As Manish already pointed out, you can use something like this:


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

import org.apache.commons.io.FileUtils;

public class PrintImageDimension {
  public static void main(String[] args) throws Exception {
    BufferedImage img = ImageIO.read(new File(args[0]));
    System.out.println(img.getWidth() + "x" + img.getHeight());

    byte[] file = FileUtils.readFileToByteArray(new File(args[0]));
    img = ImageIO.read(new ByteArrayInputStream(file));
    System.out.println(img.getWidth() + "x" + img.getHeight());
  }
}

Where you probably just need the last two lines in case you already loaded the image in memory as a byte[].

Kind regards,

Sigiswald

Former Member
0 Kudos

hai,

InputStream stream = resource.read(false);

int size = stream.available();

this will gives size in bytes.

regards,

Naga

former_member194623
Participant
0 Kudos

Hi Sonal,

You can make use of ImageIO class for getting instance of BufferedImage class from InputStream.

Once you get instance of Image, use method getScaledInstance() to resize image.

Regards,

Manish Joshi

    • Feel good factor : )