简体   繁体   English

在Java2D中使用ColorConvertOp时,有没有办法为透明度设置默认颜色?

[英]Is there a way to set a default color for transparency when using ColorConvertOp in Java2D?

I'm converting an image with transparency in it into a Colorspace that doesn't have transparency. 我正在将其中具有透明性的图像转换为不具有透明性的Colorspace。 I'd like to set a background color for the transparent areas. 我想为透明区域设置背景颜色。 Right now when I convert it any area that is transparent turns to black in the final image. 现在,当我将其转换时,任何透明区域都会在最终图像中变为黑色。 Is there a way to do that while I'm converting between ColorSpaces? 在ColorSpaces之间进行转换时,有没有办法做到这一点? Here is my code I use to convert between color spaces: 这是我用来在色彩空间之间转换的代码:

public BufferedImage convertColorspace( BufferedImage source, int newType) {
    BufferedImage destination = new BufferedImage( source.getWidth(), source.getHeight(), newType);
    ColorConvertOp colorConvertOp = new ColorConvertOp(null);
    colorConvertOp.filter(source, destination);
    return destination;
}

// here is how its used
BufferedImage converted = convertColorspace(combinedImage, BufferedImage.TYPE_3BYTE_BGR);

I'm converting from BufferedImage.TYPE_4BYTE_ARGB to BufferedImage.TYPE_3BYTE_BGR. 我正在从BufferedImage.TYPE_4BYTE_ARGB转换为BufferedImage.TYPE_3BYTE_BGR。

How about: 怎么样:

    BufferedImage temp = new BufferedImage(source.getWidth(), source.getHeight(), 
        BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = temp.createGraphics();
    g2.setColor(Color.green);
    g2.fillRect(0, 0, source.getWidth(), source.getHeight());
    g2.drawImage(0, 0, source, null);
    g2.dispose();

Then call colorConvertOp.filter with temp instead of source . 然后使用temp而不是source调用colorConvertOp.filter

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

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