当前位置: 首页 > 知识库问答 >
问题:

使用BufferedImage读取和写入图像文件

洪通
2023-03-14

下面是以下代码,它使用BufferedImage读取RGB值,然后简单地将它们再次写回文件。生成的图像是完美的,看起来很好。不用担心。

我运行了一个打印测试,打印出前10个RBG int值。这是为了测试“test.png”文件,然后测试结果图像——“new-test . png”。出于某种原因,我得到了不同的RBG值之间的两个文件。

例如(前3个RGB int值)

test.png:-16704215,-16704215,-16704215

new-test.png:-16638935,-16638935,-16573142

有人能确定为什么两个测试文件打印出的RBG值不同吗?

    try
    { 
    BufferedImage imgBuf = ImageIO.read(new File("test.png"));//also testing with GIFs, JPEGs
    int w = imgBuf.getWidth();
    int h = imgBuf.getHeight();
    int[] RGBarray = imgBuf.getRGB(0,0,w,h,null,0,w);

    //Arrays to store their respective component values
    int [][] redPixels = new int [h][w]; 
    int [][] greenPixels = new int [h][w]; 
    int [][] bluePixels = new int [h][w];

    for(int i=0; i<=10; i++)
    {
       //print out the first 10 RGB int values - testing purposes
       System.out.println(RGBarray[i]);
    }

    //Separate the RGB int values into 3 array, red, green and blue ....
    int x=0;
    for(int row=0; row<h; row++)
    {
       for(int col=0; col<w; col++)
       {
          redPixels[row][col] = ((RGBarray[x]>>16)&0xff);
          greenPixels[row][col] = ((RGBarray[x]>>8)&0xff);
          bluePixels[row][col] = (RGBarray[x]&0xff);
          x++;
       }
    }

    //set pixels back using the setRBG() ...
    BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for(int row=0; row<h; row++) 
    {
       for(int col=0; col<w; col++)
       {
          //use bit shifting to re-form the RGB int again
          int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);

          bufferedImage.setRGB(col, row, rgb);
       }
    }
  }
  catch(IOException i){}; // This exception format is only temporary !

共有1个答案

云建木
2023-03-14

下面是对代码的一个小修改,它也保存了图像,然后再次读取它。通过这段代码,我得到了前后完全相同的int值。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.junit.Test;


public class PngReadWriteTest {

@Test
public void test(){
    try
    { 
    BufferedImage imgBuf = ImageIO.read(new File("test.png"));//also testing with GIFs, JPEGs
    int w = imgBuf.getWidth();
    int h = imgBuf.getHeight();
    int[] RGBarray = imgBuf.getRGB(0,0,w,h,null,0,w);

    //Arrays to store their respective component values
    int [][] redPixels = new int [h][w]; 
    int [][] greenPixels = new int [h][w]; 
    int [][] bluePixels = new int [h][w];

    System.out.println("IMAGE FIRST READ:");
    printPixelValues(imgBuf);

    //Separate the RGB int values into 3 array, red, green and blue ....
    int x=0;
    for(int row=0; row<h; row++)
    {
       for(int col=0; col<w; col++)
       {
          redPixels[row][col] = ((RGBarray[x]>>16)&0xff);
          greenPixels[row][col] = ((RGBarray[x]>>8)&0xff);
          bluePixels[row][col] = (RGBarray[x]&0xff);
          x++;
       }
    }

    //set pixels back using the setRBG() ...
    BufferedImage bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

    for(int row=0; row<h; row++)
    {
       for(int col=0; col<w; col++)
       {
          //use bit shifting to re-form the RGB int again
          int rgb = (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);

          bufferedImage.setRGB(col, row, rgb);
       }
    }

    System.out.println("IMAGE BEFORE SAVE:");
    printPixelValues(bufferedImage);

    //Save image as png
    ImageIO.write(bufferedImage, "png", new File("new-test.png"));

    BufferedImage newImage = ImageIO.read(new File("new-test.png"));//also testing with GIFs, JPEGs

    System.out.println("IMAGE AFTER SECOND READ:");
    printPixelValues(newImage);

  }
  catch(IOException i){}; // This exception format is only temporary !
}

private void printPixelValues(BufferedImage image){
    int w = image.getWidth();
    int h = image.getHeight();
    int[] RGBarray = image.getRGB(0,0,w,h,null,0,w);

    for(int i=0; i<=10; i++)
    {
        //print out the first 10 RGB int values - testing purposes
        System.out.println(RGBarray[i]);
    }
}

}

这只有在您使用PNG时才有效,因为它是无损的。如果您使用JPG,您将不会得到相同的结果,因为它是有损的。当您将文件保存为JPG时,它不会导致包含与您的第一张图像中完全相同的字节的文件。压缩级别会影响图像。因此,即使文件看起来完全相同,它们也不会完全相同。

 类似资料:
  • 问题内容: 如何使用框架有效地从大文件读取并将大数据写入文件。 我工作,并和曾尝试类似如下: 谁能告诉我,如果我的文件大小超过2 GB,我应该遵循相同的步骤吗? 如果大量的书面操作,我想在写作时做类似的事情,该怎么办? 问题答案: 请注意,您可以像示例代码那样简单地用于复制文件,只是速度更快,而且仅一行代码。 否则,如果您已经打开了两个文件通道,则可以使用 将该通道的全部内容传输到该通道。请注意,

  • null 如果我的理解有误,请指正。还有以下问题: 我的理解是,Hadoop中的文件读/写没有任何并行性,它所能执行的最佳操作与传统的文件读或写(即,如果复制设置为1)+分布式通信机制中涉及的一些开销是一样的。 并行性仅在数据处理阶段通过Map Reduce提供,而不是在客户端读/写文件期间提供。

  • 问题内容: 我试图逐行读取文件,然后使用Node.js将其输出到另一个文件。 我的问题是由于Node.js的异步特性,行的顺序有时会混乱。 例如,我的输入文件就像:第1行第2行第3行 但是输出文件可能像:第1行第3行第2行 下面是我的代码。 任何想法将不胜感激,谢谢。 问题答案: 如果要编写同步代码,请仅使用同步功能: 对于异步方法,您可以编写类似

  • 问题内容: 我在文件中有以下JSON : 如何使用PHP 添加到文件中? 这是我到目前为止的内容: 这给了我一个致命错误:无法在此行上将stdClass类型的对象用作数组: 我正在使用PHP5.2。有什么想法吗?谢谢! 问题答案: 错误消息中的线索是-如果您查看文档以了解它可能需要第二个参数,该参数控制返回数组还是对象-它默认为object。 因此,将您的通话更改为 并且它将返回一个关联数组,您的

  • 问题内容: 这是一个有点奇怪的请求,但我正在寻找一种方法来将列表写入文件,然后再读回去。 我没有办法重新制作列表,以使它们如下面的示例所示正确地形成/格式化。 我的列表具有如下数据: 问题答案: 如果您不需要它是人类可读/可编辑的,则最简单的解决方案是使用。 来写: 读书: 如果您 确实 需要使它们易于阅读,则我们需要更多信息。 如果保证是没有嵌入换行符的字符串列表,则只需每行写一个: 如果它们是

  • 我目前正在使用Spring集成实现一些导入/导出机制,总的来说进展很顺利,但在功能方面似乎存在差距,我不了解: 有Spring集成文件轮询目录,写入文件,...我可以用它来轮询一个目录,并获得一个