爪哇 :如何以像素为单位调整缓冲图像的大小



我需要将原始缓冲图像的大小调整为 100 像素的宽度,并保留宽/高比。下面是我使用的代码。有人可以帮助我吗?提前谢谢。

int width = 100;
int height = 100;
BufferedImage resizedImage = new BufferedImage(width, height, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, width, height, null);
g.dispose();

你可能想看看imgscalr,它是一个简单的单一类,它使用一组简单的静态方法来做到这一点: http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/

示例用法如下所示:

BufferedImage img = Scalr.resize(src, 100);
如果需要,您还可以使用许多

有关质量,速度和简单图像操作的高级功能,并且该库已部署在众多项目和Web应用程序中的生产中。

int imageWidth = originalImage.getWidth();
int imageHeight = originalImage.getHeight();
height = imageHeight * width / imageWidth;
// Integer arithmetic doing lossless division last.

但是,高度现在可能小于或大于 100。

好吧,你能试试吗

int width = 100;
int height = 100;
height = originalImage.height() / (originalImage.width()/width)

最新更新