save resized image java
Asked Answered
S

3

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

Salmons answered 27/9, 2012 at 11:19 Comment(1)
On a slightly unrelated note, you might want to have a read through The Perils of Image.getScaledInstance()Bellybutton
D
8

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:

Duly answered 27/9, 2012 at 21:6 Comment(3)
hey mr. david what do i do if i want like 3 file types to be read?Salmons
oh sorry i was talking about my filechooser. wrong question sorry XDSalmons
This is a useful start, but gives a poor quality scale. I used getScaledInstance with SCALE_SMOOTH as well and it works well.Macrocosm
C
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) {
    ...
}
Cotquean answered 27/9, 2012 at 12:47 Comment(0)
M
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

Manfred answered 27/9, 2012 at 12:31 Comment(10)
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.RenderedImageSalmons
now i get this error Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (-1) and height (-1) must be > 0Salmons
@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 properlyBellybutton
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);Salmons
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); }Salmons
Just replace image.getWidth(null) and image.getHeight(null) with 105 and 105 which should be the size of you rescaled image.Manfred
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 :DSalmons
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.Manfred
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.Salmons
Try to set the image type as BufferedImage.TYPE_INT_RGB or BufferedImage.TYPE_INT_BGR.Manfred

© 2022 - 2024 — McMap. All rights reserved.