【发布时间】:2015-02-12 07:01:14
【问题描述】:
所以我有一个图像文件,上面有火山。其他一切都是 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;
}
这工作正常,但似乎很慢。我见过有人以另一种方式这样做,但我不知道发生了什么。如果有人知道更好的方法来做到这一点,我非常想听听。
【问题讨论】:
标签: java image replace colors bufferedimage