• Post Reply Bookmark Topic Watch Topic
  • New Topic

Save BufferedImage

 
Alain Boucher
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I use the following code to save a jpeg image on the hard drive. When I open the file, the scale of my picture is ok, but it's all black... why?
Thanks
private static void saveAsFile(BufferedImage img, String pathFile) {
FileOutputStream fos;
try {
fos = new FileOutputStream(
pathFile);
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder(fos);
encoder.encode(img);
fos.flush();
fos.close();
}
catch(FileNotFoundException e) {
System.out.println(e);
}
catch(IOException ioe) {
System.out.println(ioe);
}
}//end saveAsFile
 
Brendan Kennedy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alain,
I think your method is ok so your problem is probably to do with the bufferedimage you are passing to it. The image is probably black because the buffedimage you're writing is an empty buffer. What are you using to read in the image? Before changing to using the new ImageIO I used ImageIcon:
Image inImage = new ImageIcon(u).getImage();
You may also need to wait for the image to fully download, ie use media tracker (I've never used this, but here's some code):
MediaTracker mediaTracker = new MediaTracker(null);
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);
Hope that helps
Brendan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!