【问题标题】:setRGB() in javajava中的setRGB()
【发布时间】:2012-08-10 16:56:42
【问题描述】:

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

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

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

【问题讨论】:

  • 可能是另一个错误或者你走错了路。所以请发布更多代码。
  • 几个点.. - Color(0,0,0) 将是黑色 - setRGB 设置图像中的单个像素,而不是整个图像
  • 什么是image1?仅供参考:值 RGB 0,0,0 映射到 black 而 RGB 值 255,255,255 映射到 white
  • 试试setRGB(0,0,0),让我们知道结果。

标签: java image image-processing pixel


【解决方案1】:

白色是 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) {
}

【讨论】:

  • 如果这回答了您的问题,请接受它作为答案,以便问题被标记为已回答。
【解决方案2】:
 Color col = new Color(newValue, newValue, newValue);
            image1.setRGB(i, j, col.getRGB());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多