简体   繁体   English

如何在 java 中获取图像的大小

[英]how to get the size of an image in java

hi I am using Jtidy parser in java.嗨,我在 java 中使用 Jtidy 解析器。


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.上面的代码工作正常,我得到了正确的高度和宽度。但是我想查看图像的大小(例如它是 KB 还是 MB)。请帮助我,如何获取图像的大小.有什么方法吗。

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);

Try:尝试:

url.openConnection().getContentLength();

If this doesn't work, you can load the stream using:如果这不起作用,您可以使用以下命令加载 stream:

url.openStream()

...and read the stream until the end, counting how many bytes were actually read. ...并读取 stream 直到最后,计算实际读取了多少字节。 You might also use CountingInputStream decorator to reuse the stream later.您还可以使用CountingInputStream装饰器稍后重用 stream。 However the first code snippet seems to work.然而,第一个代码片段似乎工作。

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));
    }
}

在此处输入图像描述

Use a HEAD request to get the content length使用 HEAD 请求获取内容长度

URL url = new URL("http://l1.yimg.com/t/frontpage/baba-ramdev-310511-60.jpg");
//Set the user agent
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0");
//Use Http to get the head request
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//head request to minimal response
urlConnection.setRequestMethod("HEAD");
length = (urlConnection).getContentLengthLong();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM