• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Save BufferedImage

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic