kalman03

          每天早上看一遍《福布斯》富翁排行榜,如果上面沒有我的名字,我就去學習......
          隨筆 - 22, 文章 - 0, 評論 - 86, 引用 - 0
          數據加載中……

          (轉)圖片等比壓縮,確保不失真


          import java.awt.Image;
          import java.awt.image.BufferedImage;
          import java.io.File;
          import java.io.FileOutputStream;

          import javax.imageio.ImageIO;

          import com.sun.image.codec.jpeg.JPEGCodec;
          import com.sun.image.codec.jpeg.JPEGImageEncoder;

          public class Snippet {
              
          /**
               * 創建圖片縮略圖(等比縮放)
               * 
               * 
          @param src
               *            源圖片文件完整路徑
               * 
          @param dist
               *            目標圖片文件完整路徑
               * 
          @param width
               *            縮放的寬度
               * 
          @param height
               *            縮放的高度
               
          */
              
          public static void createThumbnail(String src, String dist, float width,
                      
          float height) {
                  
          try {
                      File srcfile 
          = new File(src);
                      
          if (!srcfile.exists()) {
                          System.out.println(
          "文件不存在");
                          
          return;
                      }
                      BufferedImage image 
          = ImageIO.read(srcfile);

                      
          // 獲得縮放的比例
                      double ratio = 1.0;
                      
          // 判斷如果高、寬都不大于設定值,則不處理
                      if (image.getHeight() > height || image.getWidth() > width) {
                          
          if (image.getHeight() > image.getWidth()) {
                              ratio 
          = height / image.getHeight();
                          } 
          else {
                              ratio 
          = width / image.getWidth();
                          }
                      }
                      
          // 計算新的圖面寬度和高度
                      int newWidth = (int) (image.getWidth() * ratio);
                      
          int newHeight = (int) (image.getHeight() * ratio);

                      BufferedImage bfImage 
          = new BufferedImage(newWidth, newHeight,
                              BufferedImage.TYPE_INT_RGB);
                      bfImage.getGraphics().drawImage(
                              image.getScaledInstance(newWidth, newHeight,
                                      Image.SCALE_SMOOTH), 
          00null);

                      FileOutputStream os 
          = new FileOutputStream(dist);
                      JPEGImageEncoder encoder 
          = JPEGCodec.createJPEGEncoder(os);
                      encoder.encode(bfImage);
                      os.close();
                      System.out.println(
          "創建縮略圖成功");
                  } 
          catch (Exception e) {
                      System.out.println(
          "創建縮略圖發生異常" + e.getMessage());
                  }
              }
              
              
          public static void main(String[] args) {
                  createThumbnail(
          "E:\\test_1920x1200.jpg""D:\\a.png"100100);
                  createThumbnail(
          "E:\\test_1920x1200.jpg""D:\\b.png"20002000);
                  createThumbnail(
          "E:\\test_1920x1200.jpg""D:\\c.jpg"800600);
              }

          }

          posted on 2012-01-11 15:43 kalman03 閱讀(7142) 評論(1)  編輯  收藏

          評論

          # re: (轉)圖片等比壓縮,確保不失真  回復  更多評論   

          很失真
          2015-10-22 11:07 | 小大人

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 林州市| 文水县| 三穗县| 白河县| 海口市| 浮山县| 汕尾市| 中江县| 马龙县| 白玉县| 泰顺县| 蚌埠市| 惠安县| 东丽区| 宣武区| 淮阳县| 乌拉特中旗| 江门市| 青田县| 明水县| 麻栗坡县| 邵武市| 海南省| 论坛| 惠安县| 竹山县| 龙川县| 洛扎县| 吐鲁番市| 桐柏县| 鹤壁市| 莱芜市| 茶陵县| 漠河县| 襄汾县| 久治县| 普格县| 通州市| 青河县| 弥勒县| 贺州市|