日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

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對象  
  21.         width = img.getWidth(null); // 得到源圖寬  
  22.         height = img.getHeight(null); // 得到源圖長  
  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ù)流。該接口的用戶應(yīng)在 Raster 或 
  38.              * BufferedImage 中提供圖像數(shù)據(jù),在 JPEGEncodeParams 對象中設(shè)置必要的參數(shù), 并成功地打開 
  39.              * OutputStream(編碼 JPEG 流的目的流)。JPEGImageEncoder 接口可 將圖像數(shù)據(jù)編碼為互換的縮略 
  40.              * JPEG 數(shù)據(jù)流,該數(shù)據(jù)流將寫入提供給編碼器的 OutputStream 中。 
  41.              * 注意:com.sun.image.codec.jpeg 包中的類并不屬于核心 Java API。它們屬于 Sun 發(fā)布的 JDK 
  42.              * 和 JRE 產(chǎn)品的組成部分。雖然其它獲得許可方可能選擇發(fā)布這些類,但開發(fā)人員不能寄 希望于從非 Sun 
  43.              * 實現(xiàn)的軟件中得到它們。我們期望相同的功能最終可以在核心 API 或標準擴 展中得到。 
  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.      * 以寬度為基準,等比例放縮圖片 
  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.      * 以高度為基準,等比例縮放圖片 
  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è)置目標文件名 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.      * 獲取目標文件名 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ù)進行圖片繪制。

 

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 類表示 2D 仿射變換,它執(zhí)行從 2D 坐標到其他 2D 
  17.             坐標的線性映射,保留了線的“直線性”和“平行性”??梢允褂靡幌?nbsp;
  18.             列平移、縮放、翻轉(zhuǎn)、旋轉(zhuǎn)和剪切來構(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類使用仿射轉(zhuǎn)換來執(zhí)行從源圖像或 Raster 中 2D 坐標到目標圖像或 
  32.              *  Raster 中 2D 坐標的線性映射。所使用的插值類型由構(gòu)造方法通過 
  33.              *  一個 RenderingHints 對象或通過此類中定義的整數(shù)插值類型之一來指定。 
  34.             如果在構(gòu)造方法中指定了 RenderingHints 對象,則使用插值提示和呈現(xiàn) 
  35.             的質(zhì)量提示為此操作設(shè)置插值類型。要求進行顏色轉(zhuǎn)換時,可以使用顏色 
  36.             呈現(xiàn)提示和抖動提示。 注意,務(wù)必要滿足以下約束:源圖像與目標圖像 
  37.             必須不同。 對于 Raster 對象,源圖像中的 band 數(shù)必須等于目標圖像中 
  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 表示一個具有 8 位 RGB 顏色分量的圖像, 
  44.              * 對應(yīng)于 Windows 風(fēng)格的 BGR 顏色模型,具有用 3 字節(jié)存 
  45.              * 儲的 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. }  

 

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

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多