简体   繁体   English

在BufferedImage中使用setRGB()方法

[英]Using setRGB() method in BufferedImage

I can sucessfully read and write values to an image file which accurately shows an image created. 我可以成功地读取和写入图像文件的值,准确显示创建的图像。

I simply read the values using getRGB(), and then bit shift them into red, green and blue arrays respectively. 我只是使用getRGB()读取值,然后将它们分别转换为红色,绿色和蓝色数组。 Then I simply set them back into another BufferedImage object using the setRGB() method. 然后我使用setRGB()方法将它们设置回另一个BufferedImage对象。

Now, I am trying to alter the pixel values, say the very first pixel of the red array. 现在,我试图改变像素值,比如说红色阵列的第一个像素。 I then print out the first 5 pixels of the red array and the first value is changed as expected before invoking the setRGB() method, but when I read in that image again the first value is now back to its original value? 然后我打印出红色数组的前5个像素,并且在调用setRGB()方法之前,第一个值按预期更改,但是当我再次读入该图像时,第一个值现在又恢复到原始值?

Does anybody know that using the setRGB() only changes the values in memory, but does not actually write that altered values? 有人知道使用setRGB()只更改内存中的值,但实际上并没有写出更改的值吗?

EDITS - THIS IS A SAMPLE REPRESENTATION OF MY CODE (this works perfectly due to getting back an image) 编辑 - 这是我的代码的示例代表(由于返回图像,这非常有效)

//READ IN IMAGE
BufferedImage imgBuf =null;
imgBuf = ImageIO.read(new File("test.jpg"));

int w = imgBuf.getWidth();
int h = imgBuf.getHeight();
int[] RGBarray = imgBuf.getRGB(0,0,w,h,null,0,w);

//BIT SHIFT VALUES INTO ARRAY
for(int row=0; row<h; row++)
      {
         for(int col=0; col<w; col++)
         {
            alphaPixels[row][col] = ((RGBarray[g]>>24)&0xff);
            redPixels[row][col] = ((RGBarray[g]>>16)&0xff);
            greenPixels[row][col] = ((RGBarray[g]>>8)&0xff);
            bluePixels[row][col] = (RGBarray[g]&0xff);
            g++;
         }
      }

//BIT SHIFT VALUES BACK TO INTEGERS
for(int row=0; row<h; row++)
{
    for(int col=0; col<w; col++)
    {
        int rgb = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
        imgBuf.setRGB(col, row, rgb);
     }
}

//WRITE IMAGE BACK OUT
ImageIO.write(imgBuf, "jpeg", new File("new-test.jpg"));

Write where? 写在哪里? If you change the RGB value of the BufferedImage's raster, then yes the memory value is written to and changed. 如果更改BufferedImage栅格的RGB值,则会写入并更改内存值。 If you mean does it change it to disk? 如果你的意思是它将它改为磁盘? No, not unless you write the image yourself to disk, often with ImageIO.write(...) . 不,除非您自己将图像写入磁盘,通常使用ImageIO.write(...) Changes to the memory representation of disk data will not mathemagically change the disk representation on it's own; 对磁盘数据的内存表示的更改不会在数学上以数学方式更改磁盘表示本身; instead you have to explicitly do this with your code. 相反,你必须用你的代码明确地这样做。 I think that you may be missing this last important step. 我想你可能错过了最后一个重要的步骤。


Edit 编辑
You state in comment: 你在评论中说:

Currently I can write to an image created on disk with a new name. 目前,我可以使用新名称写入在磁盘上创建的映像。 So if that works, then surely changing a few values should be the same effect? 那么如果这样可行,那么肯定改变一些值应该是同样的效果? (Using setRBG() ) (使用setRBG())

I'm still not clear on this. 我还不清楚这一点。 Say for instance: 比如说:

  • If you have an image on disk, say imageA.jpg, 如果你在磁盘上有一个图像,比如imageA.jpg,
  • and say you read this into a BufferedImage via ImageIO.read(...) , say into the bufferedImageA variable, 并说你通过ImageIO.read(...)它读入BufferedImage,比如说进入bufferedImageA变量,
  • and then you change the data raster via setRGB(...) 然后通过setRGB(...)更改数据栅格
  • and then write your changed BufferedImage to disk with ImageIO.write(...) , say to a new file, imageB.jpg, 然后使用ImageIO.write(...)将已更改的BufferedImage写入磁盘,比如新文件imageB.jpg,
  • Then if you read in imageB.jpg, it should show the changes made. 然后,如果您阅读imageB.jpg,它应该显示所做的更改。
  • But if you re-read in the unchanged imageA.jpg file, it will remain unchanged. 但是如果你重新阅读未更改的imageA.jpg文件,它将保持不变。

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

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