java - 从一维数组构建图像

标签 java image image-processing bufferedimage grayscale

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

}

最佳答案

The Raster returned is a copy of the image data is not updated if the image is changed.

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

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

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

相关文章:

image - 在 Go 中合并多个图像

java - 不显示定期更新的文本

java - JPA 中复合键的唯一约束

java - hibernate + Derby 。未找到数据库

java - Visual Studio 代码调试器 (Java)

batch-file - 如何在文件夹和子文件夹中的所有 Jpeg 图像上运行 guetzli

python - 检测4个有趣的像素

javascript - 如何更新图像的来源

python - python OpenCV(cv2)putText方法中的中文编码

matlab - 错误使用 bsxfun 不支持混合整数类输入