【问题标题】:Java BufferedImage setRGB, getRGB errorJava BufferedImage setRGB、getRGB 错误
【发布时间】:2014-10-28 12:46:45
【问题描述】:

我正在编辑一个 BufferedImage。

更改图片中的像素后,我会检查以确保新值与我预期的一致。但是,它们并没有更改为指定的像素颜色!

我认为这可能与 Alpha 值有关,因此我最近添加了一个从原始像素中提取 Alpha 值的步骤,并确保在创建要插入图像的新颜色时使用该值.

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);
}

方法getRGBs()返回一个数组,其中

  • 索引 0 是红色值
  • 索引 1 为绿色
  • 索引 2 为蓝色。

输出如下:

206, 207, 207
204
203
203

如您所见,值206, 207, 207 从图像中返回为204, 203, 203 - 事实上,我更改的每个像素都返回为204, 203, 203。 我究竟做错了什么?这没有任何意义。 提前致谢!

【问题讨论】:

  • 为什么不在image.setRGB(x, y, newColor.getRGB()); 之后使用image.getRGB(x, y) 来检查颜色是否正常?如果他们没问题,那么错误就在getRGBs(x,y)
  • 贴出方法getRGBs()的代码。可能有什么问题。
  • getRGBs() 就像你说的那样,Ezequiel。这就是问题所在,它返回了我没有放入图像的数字!谢谢。
  • Ortis,getRGBs()就可以了,很简单的方法。我现在想出了一个解决方案,但是谢谢!

标签: java bufferedimage


【解决方案1】:

我在网上找到了自己的答案,下面总结一下:

在带有 ColorModel 的 BufferedImages 中,像素被设置为选择的最接近的颜色。这意味着您可能无法获得想要的颜色,因为您可以设置的颜色仅限于 ColorModel 中的颜色。 您可以通过创建自己的 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));
            }
     }

这解决了问题。显然,ColorModel 没有我指定的颜色,因此将像素调整为最接近的颜色。

Source

【讨论】:

    【解决方案2】:

    我猜您正在寻找的是WritableRaster,它有助于写入读取的图像。 使用 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();
        }
    
    }
    

    【讨论】:

    • 虽然我没有尝试过您的解决方案,但它似乎与我发布的类似。谢谢,devilpreet。
    猜你喜欢
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多