Java:如何以像素为单位调整缓冲图像的大小?
我需要调整原始缓冲图像的大小,使其宽度达到100px,并保持宽高比。下面是我使用的代码。有人能帮我吗? 提前谢谢Java:如何以像素为单位调整缓冲图像的大小?,java,resize,pixel,bufferedimage,Java,Resize,Pixel,Bufferedimage,我需要调整原始缓冲图像的大小,使其宽度达到100px,并保持宽高比。下面是我使用的代码。有人能帮我吗? 提前谢谢 int width = 100; int height = 100; BufferedImage resizedImage = new BufferedImage(width, height, type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, width, he
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();
嗯,你能试试吗
int width = 100;
int height = 100;
height = originalImage.height() / (originalImage.width()/width)
但是,height现在可能小于或大于100。您可能想看看imgscalr,它是一个简单的类,通过一组简单的静态方法来实现这一点: 示例用法如下所示:
BufferedImage img = Scalr.resize(src, 100);
质量方面有许多高级功能,如果您需要,还可以使用速度和简单的图像操作,并且该库已在许多项目和web应用程序的生产中部署。它没有出现错误,但我认为该图像的大小未正确调整为100 px,保留了宽度/高度比。@Riyad Kalla链接的10倍。这看起来是JAI.Peharps的绝佳替代品,我也可以试试这个。谢谢
BufferedImage img = Scalr.resize(src, 100);