如何在调整 BufferedImages 大小后删除黑色背景 java
How to remove black background after resizing BufferedImages java
我写了一个方法来为我调整 BufferedImages 的大小,但在这样做之后,.png 图像最终失去了透明度,取而代之的是黑色背景。
public BufferedImage getSizedImg(BufferedImage otherImage,int width,int height){
BufferedImage outputImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = outputImg.createGraphics();
g.drawImage(otherImage, 0, 0, width, height, null);
g.dispose();
return outputImg;
}
如何修改方法使图像保持透明度?
简单。当您在此处创建新的调整大小的 BufferedImage 时:
BufferedImage outputImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
不要使用 BufferedImage.TYPE_INT_RGB
,而是 BufferedImage.TYPE_INT_ARGB
。 "A"
代表 "alpha",这为您提供了透明度。有关更多信息,请参阅 BufferedImage API.
我写了一个方法来为我调整 BufferedImages 的大小,但在这样做之后,.png 图像最终失去了透明度,取而代之的是黑色背景。
public BufferedImage getSizedImg(BufferedImage otherImage,int width,int height){
BufferedImage outputImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = outputImg.createGraphics();
g.drawImage(otherImage, 0, 0, width, height, null);
g.dispose();
return outputImg;
}
如何修改方法使图像保持透明度?
简单。当您在此处创建新的调整大小的 BufferedImage 时:
BufferedImage outputImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
不要使用 BufferedImage.TYPE_INT_RGB
,而是 BufferedImage.TYPE_INT_ARGB
。 "A"
代表 "alpha",这为您提供了透明度。有关更多信息,请参阅 BufferedImage API.