中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

圖像壓縮、生成縮略圖Java類

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    import java.awt.Graphics2D;  
    import java.awt.Image;  
    import java.awt.image.BufferedImage;  
    import java.io.File;  
    import java.io.FileInputStream;  
    import java.io.FileNotFoundException;  
    import java.io.IOException;  
      
    import javax.imageio.ImageIO;  
        
    /**  
     * * @author WQ * @date 2011-01-14 * @versions 1.0 圖片壓縮工具類 提供的方法中可以設(shè)定生成的  
     * 縮略圖片的大小尺寸等  
     */    
    public class ImageUtil {    
        /** * 圖片文件讀取 * * @param srcImgPath * @return */    
        private static BufferedImage InputImage(String srcImgPath) {    
            BufferedImage srcImage = null;    
            try {    
                FileInputStream in = new FileInputStream(srcImgPath);    
                srcImage = javax.imageio.ImageIO.read(in);    
            } catch (IOException e) {    
                System.out.println("讀取圖片文件出錯(cuò)!" + e.getMessage());    
                e.printStackTrace();    
            }    
            return srcImage;    
        }    
        
        /**  
         * * 將圖片按照指定的圖片尺寸壓縮 * * @param srcImgPath :源圖片路徑 * @param outImgPath *  
         * :輸出的壓縮圖片的路徑 * @param new_w * :壓縮后的圖片寬 * @param new_h * :壓縮后的圖片高  
         */    
        public static void compressImage(String srcImgPath, String outImgPath,    
                int new_w, int new_h) {    
            BufferedImage src = InputImage(srcImgPath);    
            disposeImage(src, outImgPath, new_w, new_h);    
        }    
        
        /**  
         * * 指定長或者寬的最大值來壓縮圖片 * * @param srcImgPath * :源圖片路徑 * @param outImgPath *  
         * :輸出的壓縮圖片的路徑 * @param maxLength * :長或者寬的最大值  
         */    
        public static void compressImage(String srcImgPath, String outImgPath,    
                int maxLength) {    
            // 得到圖片    
            BufferedImage src = InputImage(srcImgPath);    
            if (null != src) {    
                int old_w = src.getWidth();    
                // 得到源圖寬    
                int old_h = src.getHeight();    
                // 得到源圖長    
                int new_w = 0;    
                // 新圖的寬    
                int new_h = 0;    
                // 新圖的長    
                // 根據(jù)圖片尺寸壓縮比得到新圖的尺寸    
                if (old_w > old_h) {    
                    // 圖片要縮放的比例    
                    new_w = maxLength;    
                    new_h = (int) Math.round(old_h * ((float) maxLength / old_w));    
                } else {    
                    new_w = (int) Math.round(old_w * ((float) maxLength / old_h));    
                    new_h = maxLength;    
                }    
                disposeImage(src, outImgPath, new_w, new_h);    
            }    
        }    
        
        /** * 處理圖片 * * @param src * @param outImgPath * @param new_w * @param new_h */    
        private synchronized static void disposeImage(BufferedImage src,    
                String outImgPath, int new_w, int new_h) {    
            // 得到圖片    
            int old_w = src.getWidth();    
            // 得到源圖寬    
            int old_h = src.getHeight();    
            // 得到源圖長    
            BufferedImage newImg = null;    
            // 判斷輸入圖片的類型    
            switch (src.getType()) {    
            case 13:    
                // png,gifnewImg = new BufferedImage(new_w, new_h,    
                // BufferedImage.TYPE_4BYTE_ABGR);    
                break;    
            default:    
                newImg = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);    
                break;    
            }    
            Graphics2D g = newImg.createGraphics();    
            // 從原圖上取顏色繪制新圖    
            g.drawImage(src, 0, 0, old_w, old_h, null);    
            g.dispose();    
            // 根據(jù)圖片尺寸壓縮比得到新圖的尺寸    
            newImg.getGraphics().drawImage(    
                    src.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0, 0,    
                    null);    
            // 調(diào)用方法輸出圖片文件    
            OutImage(outImgPath, newImg);    
        }    
        
        /**  
         * * 將圖片文件輸出到指定的路徑,并可設(shè)定壓縮質(zhì)量 * * @param outImgPath * @param newImg * @param  
         * per  
         */    
        private static void OutImage(String outImgPath, BufferedImage newImg) {    
            // 判斷輸出的文件夾路徑是否存在,不存在則創(chuàng)建    
            File file = new File(outImgPath);    
            if (!file.getParentFile().exists()) {    
                file.getParentFile().mkdirs();    
            }// 輸出到文件流    
            try {    
                ImageIO.write(newImg, outImgPath.substring(outImgPath    
                        .lastIndexOf(".") + 1), new File(outImgPath));    
            } catch (FileNotFoundException e) {    
                e.printStackTrace();    
            } catch (IOException e) {    
                e.printStackTrace();    
            }    
        }    
        public static void main(String args[]){  
            long st = System.currentTimeMillis();  
            // ImageUtil.compressImage("c:/test1.jpg", "c:/test2.jpg", 558, 273);  
            ImageUtil.compressImage("c:/test.jpg", "c:/test3.jpg",558);  
            System.out.println(System.currentTimeMillis()-st);  
        }  
    }  

標(biāo)簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Java中正則表達(dá)式驗(yàn)證日期格式

下一篇:js將人民幣金額轉(zhuǎn)換為大寫