問題描述:怎樣快速獲取一個20MB圖片的分辨率?
程序代碼:
1 package test; 2 3 import java.awt.Dimension; 4 import java.awt.image.BufferedImage; 5 import java.io.File; 6 import java.io.IOException; 7 import java.util.Iterator; 8 import javax.imageio.ImageIO; 9 import javax.imageio.ImageReader; 10 import javax.imageio.stream.FileImageInputStream; 11 import javax.imageio.stream.ImageInputStream; 12 13 /** 14 * 讀取大圖片的分辨率 15 * 16 * @author 遠方bruce 17 * 18 */ 19 public class ReadResolution { 20 /** 21 * 通過BufferedImage獲取 22 * @param file 文件 23 * @return 圖片的分辨率 24 * @throws IOException 25 */ 26 public static String getResolution1(File file) throws IOException { 27 BufferedImage image = ImageIO.read(file); 28 return image.getWidth() + "x" + image.getHeight(); 29 } 30 31 /** 32 * 獲取圖片的分辨率 33 * 34 * @param path 35 * @return 36 */ 37 public static Dimension getImageDim(String path) { 38 Dimension result = null; 39 String suffix = getFileSuffix(path); 40 //解碼具有給定后綴的文件 41 Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); 42 System.out.println(ImageIO.getImageReadersBySuffix(suffix)); 43 if (iter.hasNext()) { 44 ImageReader reader = iter.next(); 45 try { 46 ImageInputStream stream = new FileImageInputStream(new File( 47 path)); 48 reader.setInput(stream); 49 int width = reader.getWidth(reader.getMinIndex()); 50 int height = reader.getHeight(reader.getMinIndex()); 51 result = new Dimension(width, height); 52 } catch (IOException e) { 53 e.printStackTrace(); 54 } finally { 55 reader.dispose(); 56 } 57 } 58 System.out.println("getImageDim:" + result); 59 return result; 60 } 61 62 /** 63 * 獲得圖片的后綴名 64 * @param path 65 * @return 66 */ 67 private static String getFileSuffix(final String path) { 68 String result = null; 69 if (path != null) { 70 result = ""; 71 if (path.lastIndexOf('.') != -1) { 72 result = path.substring(path.lastIndexOf('.')); 73 if (result.startsWith(".")) { 74 result = result.substring(1); 75 } 76 } 77 } 78 System.out.println("getFileSuffix:" + result); 79 return result; 80 } 81 82 /** 83 * 截取Dimension對象獲得分辨率 84 * @param path 85 * 86 * @return 87 */ 88 public static String getResolution2(String path) { 89 String s = getImageDim(path).toString(); 90 s = s.substring(s.indexOf("[") + 1, s.indexOf("]")); 91 String w = s.substring(s.indexOf("=") + 1, s.indexOf(",")); 92 String h = s.substring(s.lastIndexOf("=") + 1); 93 String result = w + " x " + h; 94 System.out.println("getResolution:" + result); 95 return result; 96 } 97 98 /** 99 * 測試 100 * 101 * @param args 102 * @throws IOException 103 */ 104 public static void main(String[] args) throws IOException { 105 String path = "d:\\abc.JPG"; 106 File file = new File(path); 107 System.out.println("第1種方法使用的時間:"); 108 long s1 = System.currentTimeMillis(); 109 System.out.println(getResolution1(file)); 110 long l1 = System.currentTimeMillis(); 111 System.out.println((l1 - s1)+"ms"); 112 System.out.println("******************************"); 113 System.out.println("第2種方法使用的時間:"); 114 long s2 = System.currentTimeMillis(); 115 getResolution2(path); 116 long l2 = System.currentTimeMillis(); 117 System.out.println((l2 - s2)+"ms"); 118 } 119 }
運行結果:
第1種方法使用的時間:
11935x8554
4867ms
******************************
第2種方法使用的時間:
getFileSuffix:JPG
javax.imageio.ImageIO$ImageReaderIterator@11ddcde
getImageDim:java.awt.Dimension[width=11935,height=8554]
getResolution:11935 x 8554
0ms
注意:由於第一種方法是將圖片一次性讀入內存所以需要設置JVM的運行內存,具體方法是run圖標下拉-》Run Configurations-》VM arguments填入
-Xms256m -Xmx1024m
解釋:Dimension類封裝單個對象中組件的寬度和高度(精確到整數)。ImageIO類包含一些用來查找 ImageReader
和 ImageWriter
以及執行簡單編碼和解碼的靜態便捷方法。ImageReader用來解析和解碼圖像的抽象超類。