9

我正在使用 setRGB() 来更改图像像素的值。

int rgb=new Color(0,0,0).getRGB();
image1.setRGB(i,j,rgb); //where i,j is the boundaries of the image

在这里,我将所有像素值设置为白色。但是这种变化并没有反映在图像中。任何人都知道setRGB()它是如何工作的?

4

2 回答 2

24

白色在 RGB 255,255,255 中,因此:

Color myWhite = new Color(255, 255, 255); // Color white
int rgb = myWhite.getRGB();

try {
    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("bubbles.bmp"));
    }
    catch (IOException e) {
    }

    for (int i = 0; i < 100; i++) {
        for (int j = 0; j < 100; j++) {
            img.setRGB(i, j, rgb);
        }
    }

    // retrieve image
    File outputfile = new File("saved.png");
    ImageIO.write(img, "png", outputfile);
}
catch (IOException e) {
}
于 2012-08-14T12:07:13.990 回答
2
 Color col = new Color(newValue, newValue, newValue);
            image1.setRGB(i, j, col.getRGB());
于 2013-09-17T09:23:38.900 回答