Java BufferedImage Save saveImage(BufferedImage image)

Here you can find the source of saveImage(BufferedImage image)

Description

save Image

License

Open Source License

Declaration

public static boolean saveImage(BufferedImage image) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.imageio.ImageIO;
import javax.swing.*;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Main {
    public static boolean saveImage(BufferedImage image) {
        if (image == null)
            return false;
        JFileChooser fC = new JFileChooser();
        File file;/*ww  w .  j ava2  s  . c  o  m*/
        if (JFileChooser.APPROVE_OPTION == fC.showSaveDialog(null)) {
            file = fC.getSelectedFile();
            return saveImage(image, file);
        }
        return false;
    }

    public static boolean saveImage(BufferedImage image, File file) {
        if (image == null)
            return false;
        file = checkFile(file);
        if (file.exists()) {
            int response = JOptionPane.showConfirmDialog(null, "Do you want to override the existing file?",
                    "Replace file", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.CANCEL_OPTION)
                return false;
        }
        try {
            ImageIO.write(image, "jpg", file);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    private static File checkFile(File file) {
        String[] split = file.getAbsolutePath().split("\\.");
        if (split.length > 1) {
            return file;
        }
        return new File(file.getAbsolutePath() + ".jpg");
    }
}

Related

  1. saveImage(BufferedImage img, String extension)
  2. saveImage(BufferedImage img, String targetfile)
  3. SaveImageToFile(BufferedImage bimg, String filename, String outformat)