Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save resized image java

How do i save a resized image to a specific folder?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ImgChooser ic = new ImgChooser();
    ImageIcon icon = new ImageIcon(me,"id pic");
    Image img1 = icon.getImage();
    Image img2 = img1.getScaledInstance(105, 105, 0);
    icon.setImage(img2);
    jLabel1.setIcon(icon);
} 

This first code is where i get the image and resize it. Then i want the resized image to be saved in another folder. Thanks in advance

like image 531
Weddy Avatar asked Sep 27 '12 11:09

Weddy


1 Answers

Use ImageIO.write(...) as others have already said (+1 to them), to add here is an example for you:

public static void main(String[] args) {

    try {

        BufferedImage originalImage = ImageIO.read(new File("c:\\test.jpg"));//change path to where file is located
        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

        BufferedImage resizeImageJpg = resizeImage(originalImage, type, 100, 100);
        ImageIO.write(resizeImageJpg, "jpg", new File("c:\\images\\testresized.jpg")); //change path where you want it saved

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

}

private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();

    return resizedImage;
}

Reference:

  • http://www.mkyong.com/java/how-to-resize-an-image-in-java/
like image 120
David Kroukamp Avatar answered Sep 28 '22 08:09

David Kroukamp