如何计算java BufferedImage filesize [英] How to calculate java BufferedImage filesize

查看:253
本文介绍了如何计算java BufferedImage filesize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想在写出图像之前设置内容长度。除了将图像写入临时文件或字节数组之外,有没有办法找到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?

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

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

推荐答案

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

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

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

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

此外,它还不足以猜测文件尺寸; Content-Length 标题必须是现货。

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

这篇关于如何计算java BufferedImage filesize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆