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

2023-03-02,,,,

问题描述:怎样快速获取一个20MB图片的分辨率

程序代码:

 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类包含一些用来查找 ImageReaderImageWriter
以及执行简单编码和解码的静态便捷方法。ImageReader用来解析和解码图像的抽象超类。

java快速获取大图片的分辨率(大图片格式JPG,tiff ,eg)的相关教程结束。