设置像素的红色值,不设置RGB

时间:2014-05-23 08:43:04

标签: java colors rgb pixel bufferedimage

我正在尝试设置BufferedImage的红色,绿色或蓝色值。 之后,我将检索该红色/绿色/蓝色值,然后我将处理该值。

但我的问题是当我这样做时

Color cipherColor = new Color(cipherDecimal[offColor], cipherDecimal[offColor], cipherDecimal[offColor]);

int cipherColorInt = cipherColor.getRGB();
stegoImage.setRGB(x, y, cipherColorInt);

然后,我对这个值感到好奇......所以我添加了一些代码来查看设置RGB之前和之后的RGB值

Color cipherColor = new Color(cipherDecimal[offColor], cipherDecimal[offColor], cipherDecimal[offColor]);

int cipherColorInt = cipherColor.getRGB();
stegoImage.setRGB(x, y, cipherColorInt);
int tesColor = stegoImage.getRGB(x, y);
System.out.println(cipherColorInt + "\t" + tesColor);

关于stegoImage的信息

  

BufferedImage stegoImage = new BufferedImage(img.getWidth(null),   img.getHeight(null)+ h,BufferedImage.TYPE_BYTE_GRAY);

     

img只是一张普通的图片

它将RGB值设置得几乎正确。几乎没有,因为一些价值发生了变化。这是:

-------- RGB after and before----------
 before      after

-6052957    -6052957
-7303024    -7303024
-10855846   -10855846
-11053225   -11119018 --> changed
-3158065    -3158065
-2500135    -2500135
-13027015   -13092808 --> changed
-5658199    -5658199
-131587 -131587
-12500671   -12566464 --> changed
-5658199    -5658199
-16448251   -16777216 --> changed
-5526613    -5526613
-8553091    -8553091
-1579033    -1579033
-3421237    -3421237

请帮帮我:) 现在,如果您知道其他方式设置红色/绿色/值没有setRGB甚至BufferedImage,请告诉我。 或者,如果您知道改变价值的原因或解决方法,请告诉我。

谢谢

1 个答案:

答案 0 :(得分:0)

如您所知,您的图像为TYPE_BYTE_GRAY,它将具有线性灰度色彩空间和字节数据缓冲区,因此您可以获取数据缓冲区,将其转换为DataBufferByte并访问本机灰色字节值直接:

DataBufferByte buffer = (DataBufferByte) stegoImage.getRater().getDataBuffer();
byte[] data = buffer.getData(); // signed gray values in linear gray space

// Access each pixel
byte signedGrayPixelValue = data[x + y * stegoImage.getWidth()]; // [-128...127]

// Alternatively
int grayPixelValue = data[x + y * stegoImage.getWidth()] & 0xff; // [0...255]

使用setRGB/getRGB的问题可能是这些方法假定值在sRGB颜色空间中,因此并不总是对应于线性灰色空间(即,将发生舍入错误)。