如何将BufferedImage转换为某种颜色? [英] How to convert a BufferedImage to a certain colour?

查看:925
本文介绍了如何将BufferedImage转换为某种颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明显地,我有透明的全黑的图像。

Speicifically I have images that are all solid black on transparent. I want to assign an abritrary colour to the images when they are drawn so that the black areas are changed to the new colour.

我试图使用一个RGBImageFilter,它只是返回了一个RGBImageFilter颜色我想,但事情出了问题,没有什么正在绘制。 (ColourFilter扩展RGBImageFilter,并且只返回它的filterRGB()方法中的设置颜色。)

I tried using a RGBImageFilter that just returned the colour I want but something's going wrong and nothing is being drawn at all. (ColourFilter extends RGBImageFilter and just returns the set colour in it's filterRGB() method.)

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 :

Look at AlphaComposites, particularly 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

这篇关于如何将BufferedImage转换为某种颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆