【发布时间】:2014-11-24 00:07:56
【问题描述】:
我有一个 JavaBufferedImage。前景为黑色,背景为透明。我想将图像重新着色为红色。
我已阅读其他人的帖子并尝试使用此代码,但我的图像在运行时完全透明。
有人有什么想法吗?我是 Java 2D 图像处理库的新手。谢谢。
imageIcon= new ImageIcon(getImageURL("/ImagesGun/GunBase.png"));
gunBaseImage= Utilities.toBufferedImage(imageIcon.getImage());
int red = 0x00ff0000;
int green = 0x0000ff00;
int blue = 0x000000ff;
int width = gunBaseImage.getWidth();
int height = gunBaseImage.getHeight();
//Loop through the image and set the color to red
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
long pixel = gunBaseImage.getRGB(x, y);
if(pixel != 0){
red = 0x00ff0000;
gunBaseImage.setRGB(x,y,red);
}
}
}
【问题讨论】:
-
为每个像素调用 getRGB 和 setRGB 会很慢,因为这些方法试图进行完整的色彩空间转换。对于图像处理,最好访问 BufferedImage 后面的 int 数组。