我有一个1 D数组像素,其中包含512x512灰度图像的像素值。我想把它写成一个png文件。我写了下面的代码,但它只是创建一个空白图像。
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 :(得分:1)
返回的光栅是图像数据的副本,如果图像被更改,则不会更新。
尝试将新的Raster对象设置回图像。
WritableRaster raster = (WritableRaster)writeImage.getData();
raster.setPixels(0, 0, width, height, pixel);
writeImage.setData(raster);