gpt4 book ai didi

java - 如何将 BufferedImage 转换为某种颜色?

转载 作者:行者123 更新时间:2023-12-03 23:13:16 24 4
gpt4 key购买 nike

具体来说,我有透明的纯黑色图像。我想在绘制图像时为图像分配一种任意颜色,以便将黑色区域更改为新颜色。

我尝试使用刚刚返回我想要的颜色的 RGBImageFilter,但出了点问题,根本没有绘制任何东西。 (ColourFilter 扩展了 RGBImageFilter 并仅在其 filterRGB() 方法中返回设置的颜色。)

BufferedImage tileImage = _tiles.get( tileID );
ColourFilter cFilt = new ColourFilter();
cFilt.setColour( colour );
FilteredImageSource filteredSrc = new FilteredImageSource( tileImage.getSource(), cFilt );
Image finalImage = Toolkit.getDefaultToolkit().createImage(filteredSrc);
bufferGraphics2D.drawImage(finalImage.....

最佳答案

AlphaComposites ,特别是 DST_IN:

BufferedImage original = ... // dimensions width x height, black on transparent

// create red image
BufferedImage redVersion = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) redVersion.getGraphics();
g.setColor(Color.red);
g.fillRect(0, 0, width, height);

// paint original with composite
g.setComposite(AlphaComposite.DstIn);
g.drawImage(image, 0, 0, width, height, 0, 0, width, height, null);

// redVersion is now a red version of original

关于java - 如何将 BufferedImage 转换为某种颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6853384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com