java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)
2013-10-23 00:35
579 查看
问题描述:怎样快速获取一个20MB图片的分辨率?
程序代码:
运行结果:
第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用来解析和解码图像的抽象超类。
程序代码:
package test; import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.FileImageInputStream; import javax.imageio.stream.ImageInputStream; /** * 读取大图片的分辨率 * * @author 远方bruce * */ public class ReadResolution { /** * 通过BufferedImage获取 * @param file 文件 * @return 图片的分辨率 * @throws IOException */ public static String getResolution1(File file) throws IOException { BufferedImage image = ImageIO.read(file); return image.getWidth() + "x" + image.getHeight(); } /** * 获取图片的分辨率 * * @param path * @return */ public static Dimension getImageDim(String path) { Dimension result = null; String suffix = getFileSuffix(path); //解码具有给定后缀的文件 Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); System.out.println(ImageIO.getImageReadersBySuffix(suffix)); if (iter.hasNext()) { ImageReader reader = iter.next(); try { ImageInputStream stream = new FileImageInputStream(new File( path)); reader.setInput(stream); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); result = new Dimension(width, height); } catch (IOException e) { e.printStackTrace(); } finally { reader.dispose(); } } System.out.println("getImageDim:" + result); return result; } /** * 获得图片的后缀名 * @param path * @return */ private static String getFileSuffix(final String path) { String result = null; if (path != null) { result = ""; if (path.lastIndexOf('.') != -1) { result = path.substring(path.lastIndexOf('.')); if (result.startsWith(".")) { result = result.substring(1); } } } System.out.println("getFileSuffix:" + result); return result; } /** * 截取Dimension对象获得分辨率 * @param path * * @return */ public static String getResolution2(String path) { String s = getImageDim(path).toString(); s = s.substring(s.indexOf("[") + 1, s.indexOf("]")); String w = s.substring(s.indexOf("=") + 1, s.indexOf(",")); String h = s.substring(s.lastIndexOf("=") + 1); String result = w + " x " + h; System.out.println("getResolution:" + result); return result; } /** * 测试 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { String path = "d:\\abc.JPG"; File file = new File(path); System.out.println("第1种方法使用的时间:"); long s1 = System.currentTimeMillis(); System.out.println(getResolution1(file)); long l1 = System.currentTimeMillis(); System.out.println((l1 - s1)+"ms"); System.out.println("******************************"); System.out.println("第2种方法使用的时间:"); long s2 = System.currentTimeMillis(); getResolution2(path); long l2 = System.currentTimeMillis(); System.out.println((l2 - s2)+"ms"); } }
运行结果:
第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用来解析和解码图像的抽象超类。
相关文章推荐
- java转换tiff图片格式成gif和jpg
- (Java实现)HTML转JPG,TIFF等图片格式和TIFF图片合并功能解决方案。
- Java代码获取图片格式及颜色模型
- JAVA 实现jpg/tif/bmp 等图片之间格式得互相转换
- JAVA 实现jpg/tif/bmp 等图片之间格式得互相转换
- jpg、jpeg、png、gif、bmp、tiff、ai、cdr、eps 图片格式的区别
- ArcGIS desktop+Georeferencing 将JPG格式的图片转化为带地理坐标的TIFF格式
- 获取jpg图片的x,y的分辨率dpi
- iOS获取图片的Base64String,兼容Android,java,web,jpg(jpeg),png
- java图片格式全部转换成JPG格式
- 使用Java语言将 jpg 格式图片转为 ppm 格式图片
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- 用java将png图片转换成jpg格式的图片
- java读取tiff格式图片信息的方式。
- 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片
- VB6.0用GDI+保存图片为JPG、TIFF、PNG、GIF、BMP等格式
- 从文件对话框中读取一幅bmp图片,并解析图片信息(eg:获取分辨率)
- 利用ArcMap对tiff或jpg格式地图图片的配准步骤
- JAVA 实现jpg/tif/bmp 等图片格式互相转换,解决RenderedOp资源不能释放
- 利用jpedal进行pdf转换成jpeg,jpg,png,tiff,tif等格式的图片