6

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

qwerty
  • 2,392
  • 3
  • 30
  • 55
Weddy
  • 543
  • 6
  • 10
  • 21
  • 2
    On a slightly unrelated note, you might want to have a read through [The Perils of Image.getScaledInstance()](http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) – MadProgrammer Sep 27 '12 at 21:13

3 Answers3

9

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:

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
4

Try this...

Use ImageIO.write() method...

static boolean ImageIO.write(RenderedImage im, String formatName, File output) throws IOException

Eg:

try {

    // retrieve image

    BufferedImage bi = getMyImage();
    File outputfile = new File("saved.png");
    ImageIO.write(bi, "png", outputfile);

} catch (IOException e) {
    ...
}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
3

First transform your image into a BufferedImage and then use ImageIO to save the image:

BufferedImage image = new BufferedImage(img2.getWidth(null), img2.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = image.createGraphics();
g2.drawImage(img2, 0, 0, null);
g2.dispose();
ImageIO.write(image, formatName, outputFile);

Where the format name is a String like "jpg", "png" or "gif" and outputFile is the File to save the image to.

Also please note that if you are saving an image that doesn't support an alpha level (transparency) then the third parameter you pass to the BufferedImage constructor should be a 3 byte image like: BufferedImage.TYPE_3BYTE_BGR

Dan D.
  • 32,246
  • 5
  • 63
  • 79
  • 1
    I tried your code and i also made an output FIle like this `File output = new File("samp.jpg");` but when then it gets an error like this **Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.RenderedImage** – Weddy Sep 27 '12 at 14:25
  • 1
    now i get this error **Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0** – Weddy Sep 27 '12 at 19:39
  • @eric if your getting this error, and you followed the example, it's likely that the image you are trying to save is invalid (to small) or the original image hasn't loaded properly – MadProgrammer Sep 27 '12 at 21:15
  • 1
    can you please check on my code again XD `ImageIcon icon = new ImageIcon(me,"id pic"); Image img1 = icon.getImage(); Image img2 = img1.getScaledInstance(105, 105, 0); BufferedImage image = new BufferedImage(img2.getWidth(null), img2.getHeight(null), BufferedImage.TYPE_4BYTE_ABGR); Graphics2D g2 = image.createGraphics(); g2.drawImage(img2, 0, 0, null); g2.dispose(); icon.setImage(img2);` – Weddy Sep 28 '12 at 01:37
  • `File output = new File("samp.jpg"); try { ImageIO.write(image, "jpg", output); } catch (IOException ex) { Logger.getLogger(ID_Form.class.getName()).log(Level.SEVERE, null, ex); }` – Weddy Sep 28 '12 at 01:37
  • Just replace image.getWidth(null) and image.getHeight(null) with 105 and 105 which should be the size of you rescaled image. – Dan D. Sep 28 '12 at 06:19
  • thank you Dan. it kinda worked. it can save an image but the image is black. and how can i save it on a specific folder? should i just combine the image name and the path? Thank you for all the answers :D – Weddy Sep 28 '12 at 11:25
  • What kind of image do you have? If you save a .jpg then change the image type as I explained. The File needs to be build such as it contains the full path, including the folder and the file name. – Dan D. Sep 28 '12 at 11:39
  • `try { ImageIO.write(image, "jpg", output);` yes the file type is jpg. but the image is still black. Im really sorry dan for the inconvenience. but with every answer and comment really learn a lot from you guys. Thank you again. – Weddy Sep 28 '12 at 11:57
  • Try to set the image type as BufferedImage.TYPE_INT_RGB or BufferedImage.TYPE_INT_BGR. – Dan D. Sep 28 '12 at 12:13