我有一个1D阵列像素,其中包含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) {

}

最佳答案

如果更改了图像,则返回的Raster是图像数据的副本未更新。


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

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

关于java - 从一维数组构建图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15233847/

10-11 01:46