我有一张图像文件,上面有一座火山。其他地方都是0xFFFF00FF(不透明的品红色)。我想要用0(透明)替换掉每个包含该颜色的像素。目前我的方法如下:
public static BufferedImage replace(BufferedImage image, int target, int preferred) {
int width = image.getWidth();
int height = image.getHeight();
BufferedImage newImage = new BufferedImage(width, height, image.getType());
int color;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
color = image.getRGB(i, j);
if (color == target) {
newImage.setRGB(i, j, preferred);
}
else {
newImage.setRGB(i, j, color);
}
}
}
return newImage;
}
这个功能可以正常工作,但似乎非常缓慢。我看到有人用另一种方式实现了这个功能,但我不知道具体是怎么回事。如果有人知道更好的方法,请务必告诉我。