简体   繁体   English

为什么读取png图像时会出现垃圾值

[英]Why junk values while reading a png image

I am writing a PNG image with values 15 for every pixel. 我正在写一个PNG图像,每个像素的值为15。 When I read the written image, instead of 15 I am getting junk values. 当我阅读书面图像时,不是15,而是垃圾值。 Why is it so? 为什么会这样呢? Since I wanted to recover the same values, I have chosen the PNG format which is lossless. 由于我想恢复相同的值,因此我选择了无损的PNG格式。 Still I get the junk values. 仍然我得到了垃圾值。 Here's the code. 这是代码。

public static void main(String args[])
    {
        BufferedImage img = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);

        int[] RGBarray = null;
        int col  = 0;
        int row = 0;

        col = img.getWidth();
        row = img.getHeight();
        RGBarray = img.getRGB(0,0,col,row,null,0,col);   // Now RGBarray has all the values

        byte[] data = new byte[512*512];

        for(int k = 0; k < 262144; k++)
        {

                data[k] = 15;

        }

        int count = 0;
        for(int ro = 0; ro < 512 ; ro++)
        {
            for(int co = 0;co < 512; co++)
            {
                img.setRGB(co, ro, data[count++]);
            }
        }

      try{
                ImageIO.write(img, "png", new File("Test3.png"));
            }
        catch(Exception e)
            {
                e.printStackTrace();
            }
    BufferedImage img1 = null;

    File f = new File("Test3.png");
    try
    {
        img1 = ImageIO.read(f);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

        int[] RGBarray1 = null;
        int col1  = 0;
        int row1 = 0;

        col1 = img1.getWidth();
        row1 = img1.getHeight();
        RGBarray1 = img1.getRGB(0,0,col1,row1,null,0,col1);   // Now RGBarray has all the values

        for( int p = 0; p < 5; p++)
        {
            for(int j = 0; j < 5 ; j++)
            {
                System.out.print(img1.getRGB(p,j) + "\t");
            }
            System.out.println("");
        }
    }

Just for reference I have printed five to values to check whether I'll get 15 or not. 仅供参考,我在值上打印了五个,以检查是否会得出15。 But the output is -16777216 -16777216 -16777216 -16777216 -16777216 但是输出是-16777216 -16777216 -16777216 -16777216 -16777216
-16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216
-16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216
-16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216
-16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216 -16777216

You're writing a byte and reading int . 您正在写一个byte并读取int Try this - 尝试这个 -

System.out.print((byte)img1.getRGB(p,j) + "\t");

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

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