Amar C
Amar C

Reputation: 374

building a image from a 1d array

i have a 1 D array pixel which contains pixel values of a 512x512 grayscale image. i want to write it into a png file. i wrote the following code , but it just creates a blank image.

    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) {

}

Upvotes: 2

Views: 1230

Answers (1)

martinez314
martinez314

Reputation: 12332

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

Try setting the new Raster object back to the image.

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

Upvotes: 1

Related Questions