BufferedImage.getScaledInstance() 不调整图像大小 [英] BufferedImage.getScaledInstance() not resizing image

查看:127
本文介绍了BufferedImage.getScaledInstance() 不调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解这行代码时遇到了一些问题:

I'm having some problems understanding this line of code:

bufferedImage.getScaledInstance(100, -1, Image.SCALE_SMOOTH);

我用它来调整带有以下数字的图像的大小.宽度:3264px,高度:2448px,水平&垂直分辨率:150dpi,大小:2.72MB.

I use it to resize an image with following numbers. Width: 3264px, height: 2448px, horizontal & vertical resolution: 150dpi, size: 2.72MB.

然后我使用

ImageIO.write(bufferedImage, "jpg", outputFile);

它产生了具有以下数字的图片:Same width &高度 (3264x2448),水平 &垂直分辨率:96dpi,大小:674KB.

It resulted in a picture with the following numbers: Same width & height (3264x2448), horizontal & vertical resolution: 96dpi, size: 674KB.

图片的宽度不应该是 100px 吗,以及保持纵横比的任何高度(根据 Javadoc)?为什么结果图片具有相同的宽度 &高度,但不同的 hor&ver 分辨率和大小?我是否遗漏了什么,因为似乎无论宽度和值是多少?高度,结果图片总是和上面描述的一样.

Isn't the picture suppose to be 100px in width, and whatever height to maintain aspect ratio (according to Javadoc)? And why would the result pic have the same width & height, but different hor&ver resolution, and size? Am I missing something, cause it seems like no matter what value of width & height, the result pic is always the same as described above.

我还读到我应该将调整大小的 BufferedImage 绘制到 Graphics2D 中,并将 Graphics2D 保存到文件中.这是正确的方法吗,而不是将 BufferedImage 直接保存到文件中?

I've also read that I should draw the resized BufferedImage into a Graphics2D, and save the Graphics2D into file. Is that the proper way to do it, rather than saving the BufferedImage straight into file?

我使用的图片是这里

推荐答案

您没有得到 .getScaledInstance() 的响应.它不会修改 bufferedImage,而是返回缩放后的图像实例.由于使用了jpg",您可能会在输出图像中看到分辨率发生变化.

You are not getting the response of the .getScaledInstance(). It does not modify the bufferedImage, but returns the scaled image instance. Due to the usage of "jpg" you might get the resolution change in the output image.

因此您必须执行以下操作;

So you'd have to do the following;

Image scaledImage = bufferedImage.getScaledInstance(100, -1, Image.SCALE_SMOOTH);
// map Image to BufferedImage
ImageIO.write(scaledBufferedImage, "jpg", outputFile);

要从缩放的 Image 创建 BufferedImage,请使用以下 答案.

To create a BufferedImage from the scaled Image, use the following answer.

这篇关于BufferedImage.getScaledInstance() 不调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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