我有一个JavaBufferedImage。前景是黑色,背景是透明的。我想将图像重新着色为红色。如何更改Java缓冲图像的颜色
我已阅读其他人的帖子,并尝试使用此代码,但是当我运行它时,我的图像完全透明。
有没有人有任何想法?我是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数组。 – lbalazscs 2014-11-24 06:54:14