-1

I used

BufferedImage bi= new BufferedImage(320,240,BufferedImage.TYPE_BYTE_GRAY);

ImageIO.write(bi, "png", outputfile);

to save an image but save a black image. I can not understand what is the problem.

Ioanna
  • 95
  • 6
  • 14
  • 4
    That is not enough to know what is the problem. Most likely `bi` is black, but you don't show how you create its contents. – kiheru Oct 04 '13 at 18:25
  • If you don't put _anything_ on the canvas, it will be black. This can also happen by mistake, of course. Please add your code before this line. What did you do with `bi` before? – qben Oct 04 '13 at 18:37
  • how did you manipulate the `bi` Image?! this is because the default pixel values is 0, and it means black. –  Oct 04 '13 at 18:48
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Oct 04 '13 at 18:55
  • I'm just passing by and have no experience with BufferedImage, but `BufferedImage.TYPE_BYTE_GRAY` looks suspicious to me. – Marc-Andre Oct 04 '13 at 19:19
  • @Marc-Andre it's OK, it means that the image type is grayscale image. – qben Oct 05 '13 at 09:21
  • What did you expect? If you save a black image, it will be black. I can not understand what is the problem... ;-) – Harald K Oct 05 '13 at 09:27

1 Answers1

1

As it was suggested already, the default pixel values are zeros (RGB(0,0,0)) in a BufferedImage, so by this line:

BufferedImage bi= new BufferedImage(320,240,BufferedImage.TYPE_BYTE_GRAY);

you create a black image. So the result is exactly what is expected in this case.

If you want to create a white one for example, you can do this:

BufferedImage bi= new BufferedImage(320,240,BufferedImage.TYPE_BYTE_GRAY);
Graphics gc = bi.getGraphics();
gc.setColor(Color.white);
gc.fillRect(0,0,320,240);

before saving.

qben
  • 813
  • 10
  • 22