2013-03-05 45 views
2

我有一个包含512x512灰度图像的像素值的1维数组像素。我想写入一个PNG文件。我写了下面的代码,但它只是创建一个空白图像。从1d阵列构建图像

public void write(int width ,int height, int[] pixel) { 

     try { 
// retrieve image 
BufferedImage writeImage = new BufferedImage(512,512,BufferedImage.TYPE_BYTE_GRAY); 
File outputfile = new File("saved.png"); 
WritableRaster raster = (WritableRaster) writeImage.getData(); 
raster.setPixels(0,0,width,height,pixel); 

ImageIO.write(writeImage, "png", outputfile); 

} catch (IOException e) { 

} 
+0

为什么近距离投票?它显示了努力并且清楚地表明了它,这似乎是一个模范问题。 – 2013-03-05 21:20:06

回答

1

返回的Raster是如果图像改变时它不会更新的图像数据的副本。

尝试将新的栅格对象设置回图像。

WritableRaster raster = (WritableRaster)writeImage.getData(); 
raster.setPixels(0, 0, width, height, pixel); 
writeImage.setData(raster);