【问题标题】:building a image from a 1d array从一维数组构建图像
【发布时间】:2013-02-20 10:43:48
【问题描述】:

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

}

【问题讨论】:

  • 为什么是近距离投票?它显示了努力并明确说明,这似乎是一个模范问题。

标签: java image image-processing bufferedimage grayscale


【解决方案1】:

返回的 Raster 是图像数据的副本,如果图像发生更改,则不会更新。

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

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

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 2011-06-22
    • 2015-07-13
    • 2016-09-06
    • 1970-01-01
    • 2013-12-05
    • 2022-01-01
    • 1970-01-01
    相关资源
    最近更新 更多