Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the size of an image in java

Tags:

java

url

image

hi I am using Jtidy parser in java.


URL url = new URL("http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg");  
Image image = new ImageIcon(url).getImage();
int imgWidth = image.getWidth(null);
int imgHeight = image.getHeight(null);

Above code is working fine,I am getting height & width properly.But I want to see the size of an image (for example whether it is in KB,or in MB ).Please help me,how to get the size of an image.Is there any method.

like image 549
DJ31 Avatar asked Jun 06 '11 09:06

DJ31


People also ask

How to find out file size in Java?

Java get file size using File classJava File length() method returns the file size in bytes. The return value is unspecified if this file denotes a directory.

How to get file size bytes Java?

In Java, we can use Files. size(path) to get the size of a file in bytes.

How do you change the size of an image in Java?

You can resize an image in Java using the getScaledInstance() function, available in the Java Image class. We'll use the BufferedImage class that extends the basic Image class. It stores images as an array of pixels.

What is BufferedImage in Java?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).


3 Answers

This is one of the simplest method to find the dimensions of image.

 URL url=new URL("Any web image url");
 BufferedImage image = ImageIO.read(url);
 int height = image.getHeight();
 int width = image.getWidth();
 System.out.println("Height : "+ height);
 System.out.println("Width : "+ width);
like image 119
Ruthra Avatar answered Oct 09 '22 15:10

Ruthra


Try:

url.openConnection().getContentLength();

If this doesn't work, you can load the stream using:

url.openStream()

...and read the stream until the end, counting how many bytes were actually read. You might also use CountingInputStream decorator to reuse the stream later. However the first code snippet seems to work.

like image 36
Tomasz Nurkiewicz Avatar answered Oct 09 '22 15:10

Tomasz Nurkiewicz


How to count your bytes & eat them too.

import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.net.URL;
import java.io.*;

class ImageInfo {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = url.openStream();
        byte[] b = new byte[2^16];
        int read = is.read(b);
        while (read>-1) {
            baos.write(b,0,read);
            read = is.read(b);
        }
        int countInBytes = baos.toByteArray().length;
        ByteArrayInputStream bais = new ByteArrayInputStream(
            baos.toByteArray());
        Image image = ImageIO.read(bais);
        int width = image.getWidth(null);
        int height = image.getHeight(null);
        String imageInfo =
            width + "x" + height + " px, " +
            countInBytes + " bytes.";
        JOptionPane.showMessageDialog(null,
            new JLabel(imageInfo, new ImageIcon(image), SwingConstants.CENTER));
    }
}

enter image description here

like image 32
Andrew Thompson Avatar answered Oct 09 '22 15:10

Andrew Thompson