package com.test;import java.awt.image.BufferedImage;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.Image;import java.io.File;import java.io.IOException;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.imageio.ImageIO;public class ImageUtil {public static void main(String arg[]){ String filePath = "C:\\Users\\Lee\\Desktop\\111.jpg"; // 圖片的位置 int height=300; int width=300; Icon icon = null; try { icon = getFixedIcon(filePath,width,height); } catch(Exception e) { System.out.println("exception : " +e); } System.out.println(" ### " +icon); //產生新圖片的位置;}/** * 按寬的比例更改圖片的大小 * @param filePath 圖片路徑 * @param width 需要改變圖片的寬度 * @return * @throws Exception */public static Icon getRatioWidth(String filePath, int width) throws Exception{File f = new File(filePath); BufferedImage bi = ImageIO.read(f);double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //寬度的比例int height = (int)(wRatio * bi.getHeight()); //圖片轉換後的高度Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //設定映像的縮放大小AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,wRatio),null); //設定映像的縮放比例image = op.filter(bi,null);int lastLength = filePath.lastIndexOf("."); String subFilePath = filePath.substring(0,lastLength); //得到圖片輸出路徑 String fileType = filePath.substring(lastLength); //圖片類型 File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType); Icon ret = null;try { ImageIO.write((BufferedImage)image, "jpg", zoomFile); ret = new ImageIcon(zoomFile.getPath()); } catch (Exception e) { e.printStackTrace(); } return ret;}/** * 按高的比例更改圖片大小 * @param filePath 圖片路徑 * @param height 需要改變圖片的高度 * @return * @throws Exception */public static Icon getRatioHeight(String filePath, int height) throws Exception{File f = new File(filePath); BufferedImage bi = ImageIO.read(f);double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例int width = (int)(hRatio * bi.getWidth()); //圖片轉換後的高度Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //設定映像的縮放大小AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(hRatio,hRatio),null); //設定映像的縮放比例image = op.filter(bi,null);int lastLength = filePath.lastIndexOf("."); String subFilePath = filePath.substring(0,lastLength); //得到圖片輸出路徑 String fileType = filePath.substring(lastLength); //圖片類型 File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType); Icon ret = null;try { ImageIO.write((BufferedImage)image, "jpg", zoomFile); ret = new ImageIcon(zoomFile.getPath()); } catch (Exception e) { e.printStackTrace(); } return ret;}/** * 按輸入的任意寬高改變圖片的大小 * @param filePath * @param width * @param height * @return * @throws Exception */public static Icon getFixedIcon(String filePath, int width, int height) throws Exception{File f = new File(filePath); BufferedImage bi = ImageIO.read(f);double wRatio = (new Integer(width)).doubleValue() / bi.getWidth(); //寬度的比例double hRatio = (new Integer(height)).doubleValue() / bi.getHeight(); //高度的比例Image image = bi.getScaledInstance(width,height,Image.SCALE_SMOOTH); //設定映像的縮放大小AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio,hRatio),null); //設定映像的縮放比例image = op.filter(bi,null);int lastLength = filePath.lastIndexOf("."); String subFilePath = filePath.substring(0,lastLength); //得到圖片輸出路徑 String fileType = filePath.substring(lastLength); //圖片類型 File zoomFile = new File(subFilePath +"_"+ width +"_" + height + fileType); Icon ret = null;try { ImageIO.write((BufferedImage)image, "jpg", zoomFile); ret = new ImageIcon(zoomFile.getPath()); } catch (Exception e) { e.printStackTrace(); } return ret;}}
上面的注釋與使用方法寫的很詳細了,可以自己去試試,如需要傳入IO流和輸出IO流,可以對方法進對適當的修改即可