工具类:
package com.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class ImageResizer { /*** * 功能 :调整图片大小 开发:wuyechun 2011-7-22 * @param srcImgPath 原图片路径 * @param distImgPath 转换大小后图片路径 * @param width 转换后图片宽度 * @param height 转换后图片高度 */ public static void resizeImage(String srcImgPath, String distImgPath, int width, int height) throws IOException { File srcFile = new File(srcImgPath); Image srcImg = ImageIO.read(srcFile); BufferedImage buffImg = null; buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics().drawImage( srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(buffImg, "JPEG", new File(distImgPath)); } }
调用:
try { ImageResizer.resizeImage("原路径","修改后路径",300,300); //宽、高 } catch (IOException e) { System.out.println("图片转换出现异常!"); }