如何缩放BufferedImage [英] how do I scale a BufferedImage
问题描述
我已经查看了这个问题,但是它似乎并没有真正回答我有问题。我有一个图像文件,它可以是任何分辨率。我需要以特定分辨率将该图像加载到 BufferedImage
对象中(例如,对于本示例来说为800x800)。我知道Image类可以使用 getScaledInstance()
将图像缩放到新的大小,但是后来我不知道如何将其恢复到 BufferedImage
。是否有一种简单的方法可以将缓冲图像缩放到特定大小?
I have viewed this question, but it does not seem to actually answer the question that I have. I have a, image file, that may be any resolution. I need to load that image into a BufferedImage
Object at a specific resolution (say, for this example, 800x800). I know the Image class can use getScaledInstance()
to scale the image to a new size, but I then cannot figure out how to get it back to a BufferedImage
. Is there a simple way to scale a Buffered Image to a specific size?
注意 II不希望通过特定因子缩放图像,我想拍摄一张图像,然后制作一个特定的尺寸。
NOTE I I do not want to scale the image by a specific factor, I want to take an image and make is a specific size.
推荐答案
像这样的东西? :
/**
* Resizes an image using a Graphics2D object backed by a BufferedImage.
* @param srcImg - source image to scale
* @param w - desired width
* @param h - desired height
* @return - the new resized image
*/
private BufferedImage getScaledImage(Image srcImg, int w, int h){
BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TRANSLUCENT);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(srcImg, 0, 0, w, h, null);
g2.dispose();
return resizedImg;
}
这篇关于如何缩放BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!