我正在为每个像素写入值为15的PNG图像。当我读取书面图像时,我没有收到垃圾值。为什么这样? 由于我想恢复相同的值,我选择了无损的PNG格式。我仍然得到垃圾价值。 这是代码。为什么读取PNG图像时出现垃圾值
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("");
}
}
仅供参考我打印了五个值来检查我是否会得到15。但输出 -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
你能把这个降到最小的例子吗?例如不需要创建512x512字节的图像,将它们复制到图像中,然后提取5x5位;为什么不只是做一个1x1图像,不创建'数据',然后看看1像素是否具有相同的“垃圾”值? –