繁体   English   中英

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

[英]Java : How to resize buffered image in pixels?

我需要将原始缓冲图像调整为宽度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-图书馆/

示例用法如下:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM