java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)

问题描述:怎样快速获取一个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类包含一些用来查找 ImageReaderImageWriter
以及执行简单编码和解码的静态便捷方法。ImageReader用来解析和解码图像的抽象超类。

 

posted @ 2013-10-23 00:35  swim_fish  阅读(12166)  评论(4编辑  收藏  举报