隨筆-51  評論-14  文章-0  trackbacks-0
          方法一:
          import javax.imageio.ImageIO;
          import javax.imageio.IIOException;
          import java.awt.image.BufferedImage;
          import java.awt.Image;
          import java.io.File;
          import java.awt.image.AffineTransformOp;
          import java.awt.geom.AffineTransform;

          public class bbb {

          public static void main (String argv[]) {
          try {
          File fi 
          = new File("c:/image2.jpg"); //大圖文件
          File fo = new File("c:/imgTest.jpg"); //將要轉換出的小圖文件
          int nw = 100;
          /*
          AffineTransform 類表示 2D 仿射變換,它執行從 2D 坐標到其他 2D
          坐標的線性映射,保留了線的“直線性”和“平行性”。可以使用一系
          列平移、縮放、翻轉、旋轉和剪切來構造仿射變換。
          */
          AffineTransform transform 
          = new AffineTransform();
          BufferedImage bis 
          = ImageIO.read(fi); //讀取圖片
          int w = bis.getWidth();
          int h = bis.getHeight();
           
          //double scale = (double)w/h;
          int nh = (nw*h)/w ;
          double sx = (double)nw/w;
          double sy = (double)nh/h;
          transform.setToScale(sx,sy); 
          //setToScale(double sx, double sy) 將此變換設置為縮放變換。
          System.out.println(w + " " +h);
          /*
           * AffineTransformOp類使用仿射轉換來執行從源圖像或 Raster 中 2D 坐標到目標圖像或
           *  Raster 中 2D 坐標的線性映射。所使用的插值類型由構造方法通過
           *  一個 RenderingHints 對象或通過此類中定義的整數插值類型之一來指定。
          如果在構造方法中指定了 RenderingHints 對象,則使用插值提示和呈現
          的質量提示為此操作設置插值類型。要求進行顏色轉換時,可以使用顏色
          呈現提示和抖動提示。 注意,務必要滿足以下約束:源圖像與目標圖像
          必須不同。 對于 Raster 對象,源圖像中的 band 數必須等于目標圖像中
          的 band 數。
          */
          AffineTransformOp ato 
          = new AffineTransformOp(transform,null);
          BufferedImage bid 
          = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);
          /*
           * TYPE_3BYTE_BGR 表示一個具有 8 位 RGB 顏色分量的圖像,
           * 對應于 Windows 風格的 BGR 顏色模型,具有用 3 字節存
           * 儲的 Blue、Green 和 Red 三種顏色。
          */
          ato.filter(bis,bid);
          ImageIO.write(bid,
          "jpeg",fo);
          catch(Exception e) {
          e.printStackTrace();
          }
          }

          }



          方法二:
          import java.io.*;import java.awt.*;
          import java.awt.image.*;import com.sun.image.codec.jpeg.*;
          /**
           * <p>Title: </p>
           * <p>Description: </p>
           * <p>Copyright: Copyright (c)2007-6-13</p>
           * <p>Company: fuen</p>
           * 
          @author 楊振朋
           * 
          @version 1.0
           
          */
          public class ccc {
             
          private String srcFile;
             
          private String destFile;
             
          private int width;
             
          private int height;
             
          private Image img;  
            
             
          /**
              * 構造函數
              * 
          @param fileName String
              * 
          @throws IOException
              
          */
             
          public ccc(String fileName) throws IOException {
               File _file 
          = new File(fileName); //讀入文件
               this.srcFile = _file.getName();
               
          this.destFile = "c:/aa.jpg";//this.srcFile.substring(0, this.srcFile.lastIndexOf(".")) +"_s.jpg";
               img = javax.imageio.ImageIO.read(_file); //構造Image對象
               width = img.getWidth(null); //得到源圖寬
               height = img.getHeight(null); //得到源圖長
             }   /**
              * 強制壓縮/放大圖片到固定的大小
              * 
          @param w int 新寬度
              * 
          @param h int 新高度
              * 
          @throws IOException
              
          */
             
          public void resize(int w, int h) throws IOException {
               BufferedImage _image 
          = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
               _image.getGraphics().drawImage(img, 
          00, w, h, null); //繪制縮小后的圖
               FileOutputStream newimageout = new FileOutputStream(destFile); //輸出到文件流
               /*
                * JPEGImageEncoder 將圖像緩沖數據編碼為 JPEG 數據流。該接口的用戶應在 Raster
                * 或 BufferedImage 中提供圖像數據,在 JPEGEncodeParams 對象中設置必要的參數,
                * 并成功地打開 OutputStream(編碼 JPEG 流的目的流)。JPEGImageEncoder 接口可
                * 將圖像數據編碼為互換的縮略 JPEG 數據流,該數據流將寫入提供給編碼器的 OutputStream 中。
                注意:com.sun.image.codec.jpeg 包中的類并不屬于核心 Java API。它們屬于 Sun 發布的
                JDK 和 JRE 產品的組成部分。雖然其它獲得許可方可能選擇發布這些類,但開發人員不能寄
                希望于從非 Sun 實現的軟件中得到它們。我們期望相同的功能最終可以在核心 API 或標準擴
                展中得到。
               
          */
               JPEGImageEncoder encoder 
          = JPEGCodec.createJPEGEncoder(newimageout);
               encoder.encode(_image); 
          //近JPEG編碼
               newimageout.close();
             }   
          /**
              * 按照固定的比例縮放圖片
              * 
          @param t double 比例
              * 
          @throws IOException
              
          */
             
          public void resize(double t) throws IOException {
               
          int w = (int) (width * t);
               
          int h = (int) (height * t);
               resize(w, h);
             }   
          /**
              * 以寬度為基準,等比例放縮圖片
              * 
          @param w int 新寬度
              * 
          @throws IOException
              
          */
             
          public void resizeByWidth(int w) throws IOException {
               
          int h = (int) (height * w / width);
               resize(w, h);
             }   
          /**
              * 以高度為基準,等比例縮放圖片
              * 
          @param h int 新高度
              * 
          @throws IOException
              
          */
             
          public void resizeByHeight(int h) throws IOException {
               
          int w = (int) (width * h / height);
               resize(w, h);
             }   
          /**
              * 按照最大高度限制,生成最大的等比例縮略圖
              * 
          @param w int 最大寬度
              * 
          @param h int 最大高度
              * 
          @throws IOException
              
          */
             
          public void resizeFix(int w, int h) throws IOException {
               
          if (width / height > w / h) {
                 resizeByWidth(w);
               }
               
          else {
                 resizeByHeight(h);
               }
             }   
          /**
              * 設置目標文件名
              * setDestFile
              * 
          @param fileName String 文件名字符串
              
          */
             
          public void setDestFile(String fileName) throws Exception {
               
          if (!fileName.endsWith(".jpg")) {
                 
          throw new Exception("Dest File Must end with \".jpg\".");
               }
               destFile 
          = fileName;
             }   
          /**
              * 獲取目標文件名
              * getDestFile
              
          */
             
          public String getDestFile() {
               
          return destFile;
             }   
          /**
              * 獲取圖片原始寬度
              * getSrcWidth
              
          */
             
          public int getSrcWidth() {
               
          return width;
             }
             
          /**
              * 獲取圖片原始高度
              * getSrcHeight
              
          */
             
          public int getSrcHeight() {
               
          return height;
             }
           
          /* 
            * 調用測試
            
          */
             
          public static void main(String[] args) throws Exception {
                ccc ccc 
          = new ccc("c:/image2.jpg");
                ccc.resizeFix(
          500300);
              }
          }

          posted on 2008-05-08 13:26 Hank1026 閱讀(7535) 評論(1)  編輯  收藏

          評論:
          # re: java 生成圖片縮略圖 2014-04-29 10:50 | ss
          11  回復  更多評論
            

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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 武陟县| 手游| 普陀区| 南投市| 郎溪县| 邢台市| 墨脱县| 呼伦贝尔市| 山东省| 渝北区| 敦煌市| 青岛市| 固始县| 宁明县| 江陵县| 武隆县| 三台县| 永城市| 阳朔县| 自贡市| 台前县| 夏津县| 大连市| 东乡族自治县| 桐庐县| 新宾| 金阳县| 宁海县| 定日县| 桃江县| 法库县| 彭山县| 乌兰察布市| 寻甸| 泰顺县| 大英县| 唐山市| 集安市| 宜都市| 綦江县| 福贡县|