简体   繁体   English

调整BufferedImages Java大小后如何删除黑色背景

[英]How to remove black background after resizing BufferedImages java

I wrote a method to resize BufferedImages for me but after doing so .png images end up losing their transparency and instead they get a black background. 我写了一种方法来调整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;
}

How can I fix the method so that the images keep their transparency? 如何修复该方法,以使图像保持透明?

Simple. 简单。 When you create your new re-sized BufferedImage here: 当您在此处创建新的调整大小的BufferedImage时:

BufferedImage outputImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

don't use BufferedImage.TYPE_INT_RGB but rather BufferedImage.TYPE_INT_ARGB . 不要使用BufferedImage.TYPE_INT_RGB而是BufferedImage.TYPE_INT_ARGB The "A" stands for "alpha" and this gives you the transparency. "A"代表“ alpha”,这使您具有透明度。 For more on this, please see the BufferedImage API . 有关更多信息,请参见BufferedImage API

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

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