2

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

}
1
  • Why the close vote? It shows effort and is clearly stated, it seems like a model question. Mar 5, 2013 at 21:20

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.