Anjali
Anjali

Reputation: 269

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

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

Upvotes: 2

Views: 34361

Answers (4)

Roberto Rodriguez
Roberto Rodriguez

Reputation: 3357

Pure Java, not third party library needed:

  byte[] image = /*your image*/
  String filePath = /*destination file path*/

  File file = new File(filePath); 
  
        try (FileOutputStream fosFor = new FileOutputStream(file)) {
            fosFor.write(image);
        }

Upvotes: 0

Zaz Gmy
Zaz Gmy

Reputation: 4356

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

Upvotes: 4

ANUBRATA GHOSH
ANUBRATA GHOSH

Reputation: 1

    //Start Photo Upload with  No// 
    if (simpleLoanDto.getPic() != null && simpleLoanDto.getAdharNo() != null) {
        String ServerDirPath = globalVeriables.getAPath() + "\\";
        File ServerDir = new File(ServerDirPath);
        if (!ServerDir.exists()) {
            ServerDir.mkdirs();
        }
        // Giving File operation permission for LINUX//
        IOperation.setFileFolderPermission(ServerDirPath);
        MultipartFile originalPic = simpleLoanDto.getPic();
        byte[] ImageInByte = originalPic.getBytes();
        FileOutputStream fosFor = new FileOutputStream(
                new File(ServerDirPath + "\\" + simpleLoanDto.getAdharNo() + "_"+simpleLoanDto.getApplicantName()+"_.jpg"));
        fosFor.write(ImageInByte);
        fosFor.close();
    }
    //End  Photo Upload with  No// 

Upvotes: -1

Kamran Amini
Kamran Amini

Reputation: 1062

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

Upvotes: 1

Related Questions