我有一个一维数组像素,其中包含 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) {
}