how to save the the image in folder on disk using java

30,015

Solution 1

you can save image

private static void save(String fileName, String ext) {

   File file = new File(fileName + "." + ext);
   BufferedImage image = toBufferedImage(file);
try {
   ImageIO.write(image, ext, file);  // ignore returned boolean
} catch(IOException e) {
 System.out.println("Write error for " + file.getPath() +
                               ": " + e.getMessage());
  }
 }

and read image from disk and show into label as

File file = new File("image.gif");
    image = ImageIO.read(file);
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);

Solution 2

You can use BufferedImage to load an image from your hard disk :

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}

Try this link for further information. Reading/Loading Images in Java

And this one for saving the image. Writing/Saving an Image

try {
    // retrieve image
    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
    ...
}
Share:
30,015
Anjali
Author by

Anjali

Updated on July 09, 2022

Comments

  • Anjali
    Anjali almost 2 years

    I want to save the image on disk such as c:/images which is captured by webcam using java ..and again I want to display that image on JForm as a label... is this possible using java and netbeans I'm new in java

    • aioobe
      aioobe almost 12 years
      sounds to me like you want to load an image from disk. BTW, exactly which version of netbeans-7 are you using here?
    • Kamran Amini
      Kamran Amini almost 12 years
    • Andrew Thompson
      Andrew Thompson almost 12 years
      By JForm DYM JFrame? There is no JForm in the J2SE.