国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

解決java壓縮圖片透明背景變黑色的問題

2019-11-26 15:33:00
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

public class Picture { 
        // TODO Auto-generated constructor stub 
     public static void resizePNG(String fromFile, String toFile, int outputWidth, int outputHeight,boolean proportion) {
              try { 
               File f2 = new File(fromFile); 

                  BufferedImage bi2 = ImageIO.read(f2); 
               int newWidth;
              int newHeight;
           // 判斷是否是等比縮放
           if (proportion == true) {
            // 為等比縮放計算輸出的圖片寬度及高度
            double rate1 = ((double) bi2.getWidth(null)) / (double) outputWidth + 0.1;
            double rate2 = ((double) bi2.getHeight(null)) / (double) outputHeight + 0.1;
            // 根據縮放比率大的進行縮放控制
            double rate = rate1 < rate2 ? rate1 : rate2;
            newWidth = (int) (((double) bi2.getWidth(null)) / rate);
            newHeight = (int) (((double) bi2.getHeight(null)) / rate);
           } else {
            newWidth = outputWidth; // 輸出的圖片寬度
            newHeight = outputHeight; // 輸出的圖片高度
           }
                  BufferedImage to = new BufferedImage(newWidth, newHeight, 

                          BufferedImage.TYPE_INT_RGB); 

                  Graphics2D g2d = to.createGraphics(); 

                  to = g2d.getDeviceConfiguration().createCompatibleImage(newWidth,newHeight, 

                          Transparency.TRANSLUCENT); 

                  g2d.dispose(); 

                  g2d = to.createGraphics(); 

                  Image from = bi2.getScaledInstance(newWidth, newHeight, bi2.SCALE_AREA_AVERAGING); 
                  g2d.drawImage(from, 0, 0, null);
                  g2d.dispose(); 

                  ImageIO.write(to, "png", new File(toFile)); 

              } catch (IOException e) { 

                  e.printStackTrace(); 

              } 

          } 

          public static void main(String[] args) throws IOException { 

              System.out.println("Start"); 

              resizePNG("C://Documents and Settings//Administrator//桌面//8d9e9c82d158ccbf8b31059319d8bc3eb035414e.jpg", "C://Documents and Settings//Administrator//桌面//ell.png",200, 100,true); 

              System.out.println("OK"); 

          } 
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德钦县| 峨边| 腾冲县| 定州市| 重庆市| 繁峙县| 汪清县| 喀喇沁旗| 巴塘县| 水城县| 南昌县| 鄂托克前旗| 江津市| 洪泽县| 延川县| 余江县| 陕西省| 图们市| 腾冲县| 米脂县| 怀宁县| 嵩明县| 突泉县| 东阿县| 元谋县| 乐山市| 温宿县| 离岛区| 车致| 静海县| 梅河口市| 长治县| 西贡区| 利川市| 淳安县| 松桃| 桐乡市| 田阳县| 西乌珠穆沁旗| 托克托县| 汉寿县|