小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

java 圖片縮略圖的兩種方法

 青_春 2016-08-09

   最近網(wǎng)上看到兩種不同的java圖片縮略圖的繪制方案

 

    第一種,使用Graphics().drawImage按照一定的比例重新繪制圖像。

 

Java代碼  收藏代碼
  1. package com.image.suoluetu;  
  2.   
  3. import java.io.*;  
  4. import java.awt.*;  
  5. import java.awt.image.*;  
  6. import com.sun.image.codec.jpeg.*;  
  7.   
  8. public class DrawImage {  
  9.     private String destFile;  
  10.     private int width;  
  11.     private int height;  
  12.     private Image img;  
  13.   
  14.     public DrawImage(String fileName) throws IOException {  
  15.         File _file = new File(fileName); // 讀入文件  
  16.         _file.getName();  
  17.         this.destFile = "D:/dage2.jpg";// this.srcFile.substring(0,  
  18.                                         // this.srcFile.lastIndexOf("."))  
  19.                                         // +"_s.jpg";  
  20.         img = javax.imageio.ImageIO.read(_file); // 構(gòu)造Image對(duì)象  
  21.         width = img.getWidth(null); // 得到源圖寬  
  22.         height = img.getHeight(null); // 得到源圖長(zhǎng)  
  23.     }  
  24.   
  25.     /** 
  26.      * /** 
  27.      *  
  28.      * @param args 
  29.      */  
  30.     public void resize(int w, int h) throws IOException {  
  31.         try {  
  32.             BufferedImage _image = new BufferedImage(w, h,  
  33.                     BufferedImage.TYPE_INT_RGB);  
  34.             _image.getGraphics().drawImage(img, 0, 0, w, h, null); // 繪制縮小后的圖  
  35.             FileOutputStream newimageout = new FileOutputStream(destFile); // 輸出到文件流  
  36.             /* 
  37.              * JPEGImageEncoder 將圖像緩沖數(shù)據(jù)編碼為 JPEG 數(shù)據(jù)流。該接口的用戶(hù)應(yīng)在 Raster 或 
  38.              * BufferedImage 中提供圖像數(shù)據(jù),在 JPEGEncodeParams 對(duì)象中設(shè)置必要的參數(shù), 并成功地打開(kāi) 
  39.              * OutputStream(編碼 JPEG 流的目的流)。JPEGImageEncoder 接口可 將圖像數(shù)據(jù)編碼為互換的縮略 
  40.              * JPEG 數(shù)據(jù)流,該數(shù)據(jù)流將寫(xiě)入提供給編碼器的 OutputStream 中。 
  41.              * 注意:com.sun.image.codec.jpeg 包中的類(lèi)并不屬于核心 Java API。它們屬于 Sun 發(fā)布的 JDK 
  42.              * 和 JRE 產(chǎn)品的組成部分。雖然其它獲得許可方可能選擇發(fā)布這些類(lèi),但開(kāi)發(fā)人員不能寄 希望于從非 Sun 
  43.              * 實(shí)現(xiàn)的軟件中得到它們。我們期望相同的功能最終可以在核心 API 或標(biāo)準(zhǔn)擴(kuò) 展中得到。 
  44.              */  
  45.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimageout);  
  46.             encoder.encode(_image); // 近JPEG編碼  
  47.             newimageout.close();  
  48.         } catch (Exception ex) {  
  49.             ex.printStackTrace();  
  50.         }  
  51.     }  
  52.   
  53.     /** 
  54.      * 按照固定的比例縮放圖片 
  55.      *  
  56.      * @param t 
  57.      *            double 比例 
  58.      * @throws IOException 
  59.      */  
  60.     public void resize(double t) throws IOException {  
  61.         int w = (int) (width * t);  
  62.         int h = (int) (height * t);  
  63.         resize(w, h);  
  64.     }  
  65.   
  66.     /** 
  67.      * 以寬度為基準(zhǔn),等比例放縮圖片 
  68.      *  
  69.      * @param w 
  70.      *            int 新寬度 
  71.      * @throws IOException 
  72.      */  
  73.     public void resizeByWidth(int w) throws IOException {  
  74.         int h = (int) (height * w / width);  
  75.         resize(w, h);  
  76.     }  
  77.   
  78.     /** 
  79.      * 以高度為基準(zhǔn),等比例縮放圖片 
  80.      *  
  81.      * @param h 
  82.      *            int 新高度 
  83.      * @throws IOException 
  84.      */  
  85.     public void resizeByHeight(int h) throws IOException {  
  86.         int w = (int) (width * h / height);  
  87.         resize(w, h);  
  88.     }  
  89.   
  90.     /** 
  91.      * 按照最大高度限制,生成最大的等比例縮略圖 
  92.      *  
  93.      * @param w 
  94.      *            int 最大寬度 
  95.      * @param h 
  96.      *            int 最大高度 
  97.      * @throws IOException 
  98.      */  
  99.     public void resizeFix(int w, int h) throws IOException {  
  100.         if (width / height > w / h) {  
  101.             resizeByWidth(w);  
  102.         } else {  
  103.             resizeByHeight(h);  
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      * 設(shè)置目標(biāo)文件名 setDestFile 
  109.      *  
  110.      * @param fileName 
  111.      *            String 文件名字符串 
  112.      */  
  113.     public void setDestFile(String fileName) throws Exception {  
  114.         if (!fileName.endsWith(".jpg")) {  
  115.             throw new Exception("Dest File Must end with \".jpg\".");  
  116.         }  
  117.         destFile = fileName;  
  118.     }  
  119.   
  120.     /** 
  121.      * 獲取目標(biāo)文件名 getDestFile 
  122.      */  
  123.     public String getDestFile() {  
  124.         return destFile;  
  125.     }  
  126.   
  127.     /** 
  128.      * 獲取圖片原始寬度 getSrcWidth 
  129.      */  
  130.     public int getSrcWidth() {  
  131.         return width;  
  132.     }  
  133.   
  134.     /** 
  135.      * 獲取圖片原始高度 getSrcHeight 
  136.      */  
  137.     public int getSrcHeight() {  
  138.         return height;  
  139.     }  
  140.   
  141.     public static void main(String[] args) {  
  142.         // TODO Auto-generated method stub  
  143.         try {  
  144.             DrawImage ccc = new DrawImage("D:/dage.jpg");  
  145.             ccc.resizeFix(600, 400);  
  146.         } catch (IOException e) {  
  147.             // TODO Auto-generated catch block  
  148.             e.printStackTrace();  
  149.         }  
  150.     }  
  151. }  

 第二種:使用仿射轉(zhuǎn)換的技術(shù)進(jìn)行圖片繪制。

 

Java代碼  收藏代碼
  1. package com.image.suoluetu;  
  2. import javax.imageio.ImageIO;  
  3. import java.awt.image.BufferedImage;  
  4. import java.io.File;  
  5. import java.awt.image.AffineTransformOp;  
  6. import java.awt.geom.AffineTransform;  
  7.   
  8. public class AffineTransImage {  
  9.   
  10.     public static void main (String argv[]) {  
  11.         try {  
  12.             File fi = new File("D:/dage.jpg"); //大圖文件  
  13.             File fo = new File("D:/dagex.jpg"); //將要轉(zhuǎn)換出的小圖文件  
  14.             int nw = 500;  
  15.             /* 
  16.             AffineTransform 類(lèi)表示 2D 仿射變換,它執(zhí)行從 2D 坐標(biāo)到其他 2D 
  17.             坐標(biāo)的線(xiàn)性映射,保留了線(xiàn)的“直線(xiàn)性”和“平行性”。可以使用一系 
  18.             列平移、縮放、翻轉(zhuǎn)、旋轉(zhuǎn)和剪切來(lái)構(gòu)造仿射變換。 
  19.             */  
  20.             AffineTransform transform = new AffineTransform();  
  21.             BufferedImage bis = ImageIO.read(fi); //讀取圖片  
  22.             int w = bis.getWidth();  
  23.             int h = bis.getHeight();  
  24.              //double scale = (double)w/h;  
  25.             int nh = (nw*h)/w ;  
  26.             double sx = (double)nw/w;  
  27.             double sy = (double)nh/h;  
  28.             transform.setToScale(sx,sy); //setToScale(double sx, double sy) 將此變換設(shè)置為縮放變換。  
  29.             System.out.println(w + " " +h);  
  30.             /* 
  31.              * AffineTransformOp類(lèi)使用仿射轉(zhuǎn)換來(lái)執(zhí)行從源圖像或 Raster 中 2D 坐標(biāo)到目標(biāo)圖像或 
  32.              *  Raster 中 2D 坐標(biāo)的線(xiàn)性映射。所使用的插值類(lèi)型由構(gòu)造方法通過(guò) 
  33.              *  一個(gè) RenderingHints 對(duì)象或通過(guò)此類(lèi)中定義的整數(shù)插值類(lèi)型之一來(lái)指定。 
  34.             如果在構(gòu)造方法中指定了 RenderingHints 對(duì)象,則使用插值提示和呈現(xiàn) 
  35.             的質(zhì)量提示為此操作設(shè)置插值類(lèi)型。要求進(jìn)行顏色轉(zhuǎn)換時(shí),可以使用顏色 
  36.             呈現(xiàn)提示和抖動(dòng)提示。 注意,務(wù)必要滿(mǎn)足以下約束:源圖像與目標(biāo)圖像 
  37.             必須不同。 對(duì)于 Raster 對(duì)象,源圖像中的 band 數(shù)必須等于目標(biāo)圖像中 
  38.             的 band 數(shù)。 
  39.             */  
  40.             AffineTransformOp ato = new AffineTransformOp(transform,null);  
  41.             BufferedImage bid = new BufferedImage(nw,nh,BufferedImage.TYPE_3BYTE_BGR);  
  42.             /* 
  43.              * TYPE_3BYTE_BGR 表示一個(gè)具有 8 位 RGB 顏色分量的圖像, 
  44.              * 對(duì)應(yīng)于 Windows 風(fēng)格的 BGR 顏色模型,具有用 3 字節(jié)存 
  45.              * 儲(chǔ)的 Blue、Green 和 Red 三種顏色。 
  46.             */  
  47.             ato.filter(bis,bid);  
  48.             ImageIO.write(bid,"jpeg",fo);  
  49.         } catch(Exception e) {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.   
  54. }  

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶(hù) 評(píng)論公約

    類(lèi)似文章 更多