<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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 閱讀(7540) 評論(1)  編輯  收藏

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

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲va中文字幕| 亚洲免费中文字幕| 特级毛片爽www免费版| 日本19禁啪啪无遮挡免费动图| 中文字幕乱码一区二区免费| 免费人成视频x8x8入口| 国产精品亚洲а∨无码播放麻豆| 一级毛片在线免费看| 亚洲AV一宅男色影视| 人妻无码一区二区三区免费 | 久久亚洲国产精品成人AV秋霞| 亚洲中文字幕乱码AV波多JI| 青春禁区视频在线观看直播免费| 亚洲 欧洲 视频 伦小说| 国产一二三四区乱码免费| 女人18毛片特级一级免费视频| 亚洲中文字幕乱码AV波多JI| 在线免费观看国产视频| eeuss影院免费92242部| 亚洲精品字幕在线观看| 日韩一区二区三区免费播放| 亚洲一区精品伊人久久伊人| 在线播放免费人成毛片乱码| 亚洲综合激情九月婷婷| 99视频在线免费观看| 亚洲国产成人精品女人久久久 | 毛片无码免费无码播放| 亚洲中文久久精品无码1| 国产成人3p视频免费观看| 亚洲卡一卡二卡乱码新区| 国产免费观看网站| 亚洲夂夂婷婷色拍WW47| 四虎影视在线永久免费观看| 成人无码WWW免费视频| 亚洲jizzjizz在线播放久| 亚洲免费在线观看| 欧洲一级毛片免费| 亚洲国产精品无码专区影院 | 亚洲成A人片在线观看无码3D| 精品免费视在线观看| 亚洲AV无一区二区三区久久|