【发布时间】:2016-09-12 04:09:24
【问题描述】:
我试图从 GrayScale 图像中获取 White Colored pixel 的值并将其替换为 another Color 但是当我运行时我的代码,整个 GrayScale 图像被转移到 另一种颜色。 谁能告诉我代码中的错误在哪里,或者我怎样才能得到我想要的结果? 这是代码...
public class gray {
public static void main (String args[])throws IOException{
int width;
int height;
BufferedImage myImage = null;
File f = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\1.png");
myImage = ImageIO.read(f);
width = myImage.getWidth();
height = myImage.getHeight();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
int pixels[];
pixels = new int[width * height];
myImage.getRGB(0, 0, width, height, pixels, 0, width);
for (int i = 0; i < pixels.length; i++) {
if (pixels[i] == 0xFFFFFF) {
pixels[i] = 0x000000FF;
}
}
File f2 = new File("E:\\eclipse\\workspace\\Graphs\\src\\ColorToGray\\out 1.png");
image.setRGB(0, 0, width, height, pixels, 0, width);
ImageIO.write( image, "jpg", f2);
}
}
之前的图片:
图片之后:
【问题讨论】: