如何计算java BufferedImage文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/632229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:10:50  来源:igfitidea点击:

How to calculate java BufferedImage filesize

javabufferedimage

提问by GregA100k

I have a servlet based application that is serving images from files stored locally. I have added logic that will allow the application to load the image file to a BufferedImage and then resize the image, add watermark text over the top of the image, or both.

我有一个基于 servlet 的应用程序,它从本地存储的文件中提供图像。我添加了允许应用程序将图像文件加载到 BufferedImage 的逻辑,然后调整图像大小,在图像顶部添加水印文本,或两者兼而有之。

I would like to set the content length before writing out the image. Apart from writing the image to a temporary file or byte array, is there a way to find the size of the BufferedImage?

我想在写出图像之前设置内容长度。除了将图像写入临时文件或字节数组之外,有没有办法找到 BufferedImage 的大小?

All files are being written as jpg if that helps in calculating the size.

如果这有助于计算大小,则所有文件都被写入为 jpg。

采纳答案by Jason Cohen

No, you must write the file in memory or to a temporary file.

不,您必须将文件写入内存或临时文件。

The reason is that it's impossible to predict how the JPEG encoding will affect file size.

原因是无法预测 JPEG 编码将如何影响文件大小。

Also, it's not good enough to "guess" at the file size; the Content-Lengthheader has to be spot-on.

此外,“猜测”文件大小还不够;的Content-Length报头具有被斑点上。

回答by David Z

Well, the BufferedImage doesn't know that it's being written as a JPEG - as far as it's concerned, it could be PNG or GIF or TGA or TIFF or BMP... and all of those have different file sizes. So I don't believe there's any way for the BufferedImage to give you a file size directly. You'll just have to write it out and count the bytes.

嗯,BufferedImage 不知道它是作为 JPEG 编写的 - 就它而言,它可以是 PNG 或 GIF 或 TGA 或 TIFF 或 BMP ......所有这些都有不同的文件大小。所以我不相信 BufferedImage 有任何方法可以直接为您提供文件大小。你只需要把它写出来并计算字节数。

回答by Tom Hawtin - tackline

Unless it is a very small image file, prefer to use chunked encoding over specifying a content length.

除非它是一个非常小的图像文件,否则更喜欢使用分块编码而不是指定内容长度。

It was noted in one or two recent stackoverflow podcasts that HTTP proxies often report that they only support HTTP/1.0, which may be an issue.

在最近的一两个 stackoverflow 播客中指出,HTTP 代理经常报告它们仅支持 HTTP/1.0,这可能是一个问题。

回答by Petr

    BufferedImage img = = new BufferedImage(500, 300, BufferedImage.TYPE_INT_RGB);

    ByteArrayOutputStream tmp = new ByteArrayOutputStream();
    ImageIO.write(img, "png", tmp);
    tmp.close();
    Integer contentLength = tmp.size();

    response.setContentType("image/png");
    response.setHeader("Content-Length",contentLength.toString());
    OutputStream out = response.getOutputStream();
    out.write(tmp.toByteArray());
    out.close();

回答by vpointer

Before you load the image file as a BufferedImage make a reference to the image file via the File object.

在将图像文件作为 BufferedImage 加载之前,请通过 File 对象引用图像文件。

File imgObj = new File("your Image file path");
int imgLength = (int) imgObj.length();

imgLength would be your approximate image size though it my vary after resizing and then any operations you perform on it.

imgLength 将是您的近似图像大小,尽管它在调整大小以及您对其执行的任何操作后会有所不同。

回答by Verna Smith

You can calculate the size of a BufferedImage in memory very easily. This is because it is a wrapper for a WritableRaster that uses a DataBuffer for it's backing. If you want to calculate it's size in memory you can get a copy of the image's raster using getData() and then measuring the size of the data buffer in the raster.

您可以非常轻松地计算内存中 BufferedImage 的大小。这是因为它是 WritableRaster 的包装器,它使用 DataBuffer 作为其支持。如果您想计算它在内存中的大小,您可以使用 getData() 获取图像光栅的副本,然后测量光栅中数据缓冲区的大小。

DataBuffer dataBuffer = bufImg.getData().getDataBuffer();

// Each bank element in the data buffer is a 32-bit integer
long sizeBytes = ((long) dataBuffer.getSize()) * 4l;
long sizeMB = sizeBytes / (1024l * 1024l);`