圖片上傳的處理工具類

來源:互聯網
上載者:User

標籤:

圖片上傳的處理

package com.tools;//圖片上傳處理import java.awt.Image;import java.awt.image.BufferedImage;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class CompressPic {    public static String darwSmallImg(String url,int compressType, String fname, int new_w1,            int new_h1, int new_w2, int new_h2) {        String path = url + "\\" + fname;        java.io.File file = new java.io.File(path); // 讀入剛才上傳的檔案        // 新的縮圖儲存地址        String newfname = "\\s" + fname;        String newpath = url + newfname;                int old_w=0,old_h=0;                int width = 100;        int height = 100;        try {            Image src = javax.imageio.ImageIO.read(file);            old_w= src.getWidth(null); // 得到源圖寬            old_h = src.getHeight(null); // 得到源圖高                        if ((old_w < new_w1 && old_h < new_h1) || (old_w < new_w2 && old_h < new_h2)) {                // 這是不用壓縮的情況                FileInputStream input = new FileInputStream(path);                FileOutputStream output = new FileOutputStream(newpath);                int in = input.read();                while (in != -1) {                    output.write(in);                    in = input.read();                }                input.close();output.close();            } else {                if(compressType==1){//135*90--90*135                    if ((float) (old_w / old_h) < 0.75) {// 3:4此時為90:135                        // 如果原圖比要壓縮的圖小,則不壓縮                        if (old_w < new_w2) {                            width = old_w;                        } else {                            width = new_w2;                        }                        if (old_h < new_h2) {                            height = old_h;                        } else {                            height = new_h2;                        }                    } else if (0.75 <= (float) (old_w / old_h)) {                        if (old_w < new_w1) {                            width = old_w;                        } else {                            width = new_w1;                        }                        if (old_h < new_h1) {                            height = old_h;                        } else {                            height = new_h1;                        }                    }                }else if(compressType==2){//270*178                    if(old_w<new_w1){                        width=old_w;                    }else{                        width=new_w1;                    }                    if (old_h < new_h1) {                        height = old_h;                    } else {                        height = new_h1;                    }                }                BufferedImage tag = new BufferedImage(width, height,                        BufferedImage.TYPE_INT_RGB);                tag.getGraphics().drawImage(src, 0, 0, width, height, null); // 繪製縮小後的圖                FileOutputStream newimage = new FileOutputStream(newpath); // 輸出到檔案流                JPEGImageEncoder encoder = JPEGCodec                        .createJPEGEncoder(newimage);                encoder.encode(tag); // 近JPEG編碼                newimage.close();            }        } catch (IOException e) {            e.printStackTrace();        }        return old_w+"*"+old_h+"."+old_w/old_h;    }}

 

圖片上傳的處理工具類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.