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

Java圖片處理工具類

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    import java.io.*;  
    import java.util.Date;  
    import java.awt.*;  
    import java.awt.image.*;  
    import javax.imageio.ImageIO;  
    import com.sun.image.codec.jpeg.*;  
      
    /** 
     * 圖片壓縮處理 
     *  
     * @author kuang hj 
     */  
    public class ImgCompressUtils  
    {  
        // 圖片對象  
        private Image img;  
          
        // 原圖的寬度  
        private int width;  
          
        // 原圖的高度  
        private int height;  
          
        private static BufferedImage image;  
          
        @SuppressWarnings("deprecation")  
        public static void main(String[] args)  
            throws Exception  
        {  
            System.out.println("開始:" + new Date().toLocaleString());  
            ImgCompressUtils imgCom = new ImgCompressUtils("D:\\jjj.jpg");  
            // 處理圖片得到 image  
            imgCom.resizeFix(80, 80);  
            // 輸出結果文件  
            outputNewFile("D:\\result.jpg", image);  
            System.out.println("結束:" + new Date().toLocaleString());  
        }  
          
        /** 
         * 構造函數(shù) 取到需要處理的圖片,得到寬 和 高 
         */  
        public ImgCompressUtils(String fileName)  
            throws IOException  
        {  
            File file = new File(fileName);// 讀入文件  
            img = ImageIO.read(file); // 構造Image對象  
            width = img.getWidth(null); // 得到源圖寬  
            height = img.getHeight(null); // 得到源圖長  
        }  
          
        /** 
         * 按照寬度還是高度進行壓縮 
         *  
         * @param destwidth int 最大寬度 
         * @param destheight int 最大高度 
         */  
        public void resizeFix(int destwidth, int destheight)  
            throws IOException  
        {  
            if (width / height > destwidth / destheight)  
            {  
                resizeByWidth(destwidth);  
            }  
            else  
            {  
                resizeByHeight(destheight);  
            }  
        }  
          
        /** 
         * 以寬度為基準,等比例放縮圖片 
         *  
         * @param destwidth int 新寬度 
         */  
        public void resizeByWidth(int destwidth)  
            throws IOException  
        {  
            int tmph = (int)(height * destwidth / width);  
            resize(destwidth, tmph);  
        }  
          
        /** 
         * 以高度為基準,等比例縮放圖片 
         *  
         * @param destheight int 新高度 
         */  
        public void resizeByHeight(int destheight)  
            throws IOException  
        {  
            int tmpw = (int)(width * destheight / height);  
            resize(tmpw, destheight);  
        }  
          
        /** 
         * 強制壓縮/放大圖片到固定的大小 
         *  
         * @param w int 新寬度 
         * @param h int 新高度 
         */  
        public BufferedImage resize(int destwidth, int destheight)  
        {  
            // SCALE_SMOOTH 的縮略算法 生成縮略圖片的平滑度的 優(yōu)先級比速度高 生成的圖片質(zhì)量比較好 但速度慢  
            image = new BufferedImage(destwidth, destheight, BufferedImage.TYPE_INT_RGB);  
              
            // 繪制縮小后的圖  
            image.getGraphics().drawImage(img, 0, 0, destwidth, destheight, null);  
              
            return image;  
        }  
          
        /** 
         * 新生成的壓縮文件 <一句話功能簡述> <功能詳細描述> 
         *  
         * @param path 
         * @param image 
         * @see [類、類#方法、類#成員] 
         */  
        public static void outputNewFile(String path, BufferedImage image)  
        {  
            try  
            {  
                File destFile = new File(path);  
                FileOutputStream out = new FileOutputStream(destFile); // 輸出到文件流  
                // 可以正常實現(xiàn)bmp、png、gif轉(zhuǎn)jpg  
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
                encoder.encode(image); // JPEG編碼  
                out.close();  
            }  
            catch (Exception e)  
            {  
                e.printStackTrace();  
            }  
        }  
    }  

標簽:

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

上一篇:JAVA導出成CSV文件

下一篇:JSONHelper JSON幫助類