kalman03

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

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


          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 {
              
          /**
               * 創(chuàng)建圖片縮略圖(等比縮放)
               * 
               * 
          @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;
                      
          // 判斷如果高、寬都不大于設(shè)定值,則不處理
                      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(
          "創(chuàng)建縮略圖成功");
                  } 
          catch (Exception e) {
                      System.out.println(
          "創(chuàng)建縮略圖發(fā)生異常" + 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 閱讀(7148) 評論(1)  編輯  收藏

          評論

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

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 西和县| 清水河县| 宝应县| 泾川县| 安龙县| 万州区| 察哈| 萍乡市| 彰化县| 沈丘县| 江达县| 大名县| 保定市| 建宁县| 许昌县| 桂东县| 顺昌县| 嵊州市| 山阴县| 尖扎县| 红安县| 准格尔旗| 晋州市| 江安县| 商都县| 灵丘县| 田东县| 土默特右旗| 黄骅市| 佛坪县| 山东省| 渝北区| 西乌| 沭阳县| 泰安市| 万宁市| 永宁县| 娱乐| 绥滨县| 嵊泗县| 蛟河市|