Java Image Resize resizeImage(String loadFile, String saveFile, int maxDim)

Here you can find the source of resizeImage(String loadFile, String saveFile, int maxDim)

Description

resize Image

License

Open Source License

Declaration

public static void resizeImage(String loadFile, String saveFile, int maxDim) throws IOException 

Method Source Code


//package com.java2s;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
    public static void resizeImage(String loadFile, String saveFile, int maxDim) throws IOException {
        File save = new File(saveFile.replaceAll("/", "\\" + File.separator));
        FileInputStream fis = new FileInputStream(loadFile.replaceAll("/", "\\" + File.separator));

        BufferedImage im = ImageIO.read(fis);
        Image inImage = new ImageIcon(loadFile).getImage();

        double scale = (double) maxDim / (double) inImage.getHeight(null);

        if (inImage.getWidth(null) > inImage.getHeight(null)) {
            scale = (double) maxDim / (double) inImage.getWidth(null);
        }//from   ww  w  .ja  v a2  s .  c  o  m

        int scaledW = (int) (scale * inImage.getWidth(null));
        int scaledH = (int) (scale * inImage.getHeight(null));

        //allocate information for making image
        BufferedImage thumb = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);

        //draw image
        Graphics2D g2 = thumb.createGraphics();
        g2.drawImage(im, 0, 0, scaledW, scaledH, null);

        ImageIO.write(thumb, "jpg", save);//make image
    }
}

Related

  1. getResizedImage(String path, int height, int width)
  2. resize(Image img, int newWidth, float quality)
  3. resize(String srcImageFile, String result, int newWidth, float quality)
  4. resizeImage(ImageIcon tmpIcon)
  5. resizeImage(String imagePath, int width, int height)