JAVA改變上傳圖片大小__java

來源:互聯網
上載者:User
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流,可以對方法進對適當的修改即可

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.