简体   繁体   English

Java BufferedImage setRGB,getRGB错误

[英]Java BufferedImage setRGB, getRGB error

I am editing a BufferedImage. 我正在编辑BufferedImage。

After altering the pixel in the picture, I do a check to ensure the new value is what I expected it to be. 更改图片中的像素后,我进行检查以确保新值是我期望的值。 However, they have not changed to the designated pixel Color ! 但是, 它们尚未更改为指定的像素Color

I thought it could be something to do with the Alpha value, so I recently added a step that extracts the Alpha value from the original pixel, and ensures that value is used when creating the new Color to be inserted back into the image. 我认为这可能与Alpha值有关,因此我最近添加了一个从原始像素中提取Alpha值的步骤,并确保在创建要插入到图像中的新Color时使用该值。

System.out.println(newColors[0] + ", " + newColors[1] + ", " + newColors[2]);
Color oldColor = new Color(image.getRGB(x, y));
Color newColor = new Color(newColors[0], newColors[1], newColors[2], oldColor.getAlpha()); // create a new color from the RGB values.
image.setRGB(x, y, newColor.getRGB());// set the RGB of the pixel in the image.

for (int col : getRGBs(x,y)) {
    System.out.println(col);
}

The method getRGBs() returns an array where 方法getRGBs()返回一个数组,其中

  • index 0 is the Red value 索引0是红色值
  • index 1 is green 索引1为绿色
  • index 2 is blue. 索引2为蓝色。

The output looks like: 输出如下:

206, 207, 207
204
203
203

As you can see, the values 206, 207, 207 come back out of the image as 204, 203, 203 - in fact, every pixel I change comes back out as 204, 203, 203 . 如您所见,值206, 207, 207从图像中返回为204, 203, 203实际上,我更改的每个像素都以204, 203, 203 What am I doing wrong? 我究竟做错了什么? It just doesn't make sense. 只是没有意义。 Thanks in advance! 提前致谢!

I found my own answer online, I'll summarise it below: 我在网上找到了自己的答案,下面将对其进行总结:

In BufferedImages with a ColorModel the pixel is set to the nearest colour chosen. 在具有ColorModel的BufferedImages中,像素设置为选择的最接近的颜色。 That means that you might not get the colour you wanted because the colours you can set are limited to the colours in the ColorModel. 这意味着您可能无法获得所需的颜色,因为可以设置的颜色仅限于ColorModel中的颜色。 You can get around that by creating your own BufferedImage and draw the source image onto that and then manipulate those pixels. 您可以通过创建自己的BufferedImage并在其上绘制源图像然后处理这些像素来解决该问题。

BufferedImage original = ImageIO.read(new File(file.getPath()));

    image= new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
    image.getGraphics().drawImage(original, 0, 0, null);
     for(int y = 0; y < original.getHeight(); y++){
            for(int x = 0; x < original.getWidth(); x++){
                image.setRGB(x,y, original.getRGB(x,y));
            }
     }

That solved the problem. 那解决了问题。 Clearly, the ColorModel did not have the colours I had specified and thus adjusted the pixel to the nearest colour it could. 显然, ColorModel没有我指定的颜色,因此将像素调整为可能的最接近颜色。

Source 资源

I guess what you are looking for is WritableRaster which helps to write to the image read. 我猜您正在寻找的是WritableRaster ,它有助于写入读取的图像。 Use ImageIO to write the final changes on to a new file or give the same file for altering. 使用ImageIO将最终更改写入新文件或提供相同文件进行更改。

   public class ImageTest {

    BufferedImage image;
    File imageFile = new File("C:\\Test\\test.bmp");

    public ImageTest() {
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void editImage() throws IOException {
        WritableRaster wr = image.getRaster();
        int width = image.getWidth();
        int height = image.getHeight();

        for(int ii=0; ii<width; ii++) {
            for(int jj=0; jj<height; jj++) {
                int color = image.getRGB(ii, jj);
                wr.setSample(ii, jj, 0 , 156);
            }
        }

        ImageIO.write(image, "BMP", new File("C:\\Test\\test.bmp"));
    }

    public static void main(String[] args) throws IOException {
        ImageTest test = new ImageTest();
        test.editImage();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM