java開發_比較使用ImageReader和BufferedImage獲取圖片尺寸總結

weixin_34119545發表於2012-11-26

多的不用說,先上程式碼,大家夥兒看看

 1 /**
 2  * 
 3  */
 4 package com.b510;
 5 
 6 import java.awt.image.BufferedImage;
 7 import java.io.File;
 8 import java.io.FileInputStream;
 9 import java.io.FileNotFoundException;
10 import java.io.IOException;
11 import java.util.Date;
12 import java.util.Iterator;
13 
14 import javax.imageio.ImageIO;
15 import javax.imageio.ImageReader;
16 import javax.imageio.stream.ImageInputStream;
17 
18 public class ImageUtil {
19 
20     /**
21      * 源圖片路徑名稱如:c:/1.jpg
22      */
23     private String srcpath = "e:/pool.jpg";
24 
25     public ImageUtil() {
26 
27     }
28 
29     public static void main(String[] args) throws Exception {
30         ImageUtil util = new ImageUtil();
31         util.getImageSizeByImageReader(util.getSrcpath());
32         util.getImageSizeByBufferedImage(util.getSrcpath());
33     }
34 
35     /**
36      * 使用ImageReader獲取圖片尺寸
37      * 
38      * @param src
39      *            源圖片路徑
40      */
41     public void getImageSizeByImageReader(String src) {
42         long beginTime = new Date().getTime();
43         File file = new File(src);
44         try {
45             Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
46             ImageReader reader = (ImageReader) readers.next();
47             ImageInputStream iis = ImageIO.createImageInputStream(file);
48             reader.setInput(iis, true);
49             System.out.println("width:" + reader.getWidth(0));
50             System.out.println("height:" + reader.getHeight(0));
51         } catch (IOException e) {
52             e.printStackTrace();
53         }
54         long endTime = new Date().getTime();
55         System.out.println("使用[ImageReader]獲取圖片尺寸耗時:[" + (endTime - beginTime)+"]ms");
56     }
57 
58     /**
59      * 使用BufferedImage獲取圖片尺寸
60      * 
61      * @param src
62      *            源圖片路徑
63      */
64     public void getImageSizeByBufferedImage(String src) {
65         long beginTime = new Date().getTime();
66         File file = new File(src);
67         FileInputStream is = null;
68         try {
69             is = new FileInputStream(file);
70         } catch (FileNotFoundException e2) {
71             e2.printStackTrace();
72         }
73         BufferedImage sourceImg = null;
74         try {
75             sourceImg = javax.imageio.ImageIO.read(is);
76             System.out.println("width:" + sourceImg.getWidth());
77             System.out.println("height:" + sourceImg.getHeight());
78         } catch (IOException e1) {
79             e1.printStackTrace();
80         }
81         long endTime = new Date().getTime();
82         System.out.println("使用[BufferedImage]獲取圖片尺寸耗時:[" + (endTime - beginTime)+"]ms");
83     }
84 
85     public String getSrcpath() {
86         return srcpath;
87     }
88 
89     public void setSrcpath(String srcpath) {
90         this.srcpath = srcpath;
91     }
92 
93 }

執行結果:

大家可以很容易明白...應該選擇哪個方法了吧...

相關文章