Here you can find the source of resizeImage(String loadFile, String saveFile, int maxDim)
public static void resizeImage(String loadFile, String saveFile, int maxDim) throws IOException
//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 } }