为什么使用 ImageIO 下载的文件比浏览器下载的文件小?
下面的代码从指定的 URL 下载图像。 当从浏览器本地保存相同的图像时,它比以编程方式下载的图像大大约 66%。
虽然代码中没有明确指定任何大小参数,但是为什么下载的文件比从浏览器下载的文件小呢?
BufferedImage buffImage = ImageIO.read(new URL("random url to an image"));
ImageIO.write(buffImage, "jpg", new File("MyAppDl.jpg"));
编辑:奇怪......它不一致;在不同的 URL 上,缩略图明显更小
The code below downloads an image from the specified URL.
When the same image is saved locally from the browser, it is significantly larger than the one downloaded programmatically by about a factor of 66%.
Although the code does not explicitly specify any parameters about the size, why is the downloaded file smaller than the one downloaded from the browser?
BufferedImage buffImage = ImageIO.read(new URL("random url to an image"));
ImageIO.write(buffImage, "jpg", new File("MyAppDl.jpg"));
EDIT: Weird ... it isn't consistent; on a different URL the thumbnail is distinctly smaller
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是猜测:浏览器“按原样”加载文件......您的代码将文件读入数据结构,然后再次将其写入文件。最有可能的是它应用了默认压缩,该压缩可能高于原始使用的比率。
如果您想要原始文件,请不要使用 BufferedImage 和 ImageIO ,而是使用普通流读取/写入它。
Just a guess: the brower loads the file "as is" ... your code reads the file into a data structure and then writes it again to a file. Most probably it appls default compression that might be higher then the orginal used ratio.
If you want the original file do not use
BufferedImage
andImageIO
but read/write it with plain streams.ImageIO
版本正在解压缩并使用不同的质量设置重新压缩。不要这样做,使用不操作数据的常规Stream
类,只是将其未经解释地传递。The
ImageIO
version is being uncompressed and re-compressed with a different quality setting. Don't do this, use the regularStream
classses that don't manipulate the data and just pass it through un-interpreted.