在一篇文章中获取到通过例如以下两种方式进行获取:
1、使用ImageReader进行获取;
2、使用BufferedImage进行获取;
而且经过验证ImageReader进行操作的耗时远远低于BufferedImage操作的耗时,详情可查看链接查看原文
然后依据应用我自己在项目中略做修改,在这里记录下:
<span style="font-size:14px;">package com.jzba.utils; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; //获取图片的长宽尺寸 public class ImageUtil { /** * 源图片路径名称如:D:/zz/over.png */ private String srcpath = "D:/zz/over.png"; public ImageUtil() { } //进行本地验证 public static void main(String[] args) throws Exception { ImageUtil util = new ImageUtil(); util.getImageSizeByBufferedImage(util.getSrcpath()); } //依据图片路径获取对应图片的尺寸 public Map getSize(String srcPath){ ImageUtil util = new ImageUtil(); Map<String, Integer> map=util.getImageSizeByBufferedImage(util.getSrcpath()); return map; } /** * 使用ImageReader获取图片尺寸 * * @param src * 源图片路径 */ public void getImageSizeByImageReader(String src) { long beginTime = new Date().getTime(); File file = new File(src); try { Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg"); ImageReader reader = (ImageReader) readers.next(); ImageInputStream iis = ImageIO.createImageInputStream(file); reader.setInput(iis, true); // System.out.println("width:" + reader.getWidth(0)); // System.out.println("height:" + reader.getHeight(0)); } catch (IOException e) { e.printStackTrace(); } long endTime = new Date().getTime(); // System.out.println("使用[ImageReader]获取图片尺寸耗时:[" + (endTime - beginTime)+"]ms"); } // /** // * 使用BufferedImage获取图片尺寸 // * // * @param src // * 源图片路径 // */ // public void getImageSizeByBufferedImage(String src) { // long beginTime = new Date().getTime(); // File file = new File(src); // FileInputStream is = null; // try { // is = new FileInputStream(file); // } catch (FileNotFoundException e2) { // e2.printStackTrace(); // } // BufferedImage sourceImg = null; // try { // sourceImg = javax.imageio.ImageIO.read(is); // System.out.println("width:" + sourceImg.getWidth()); // System.out.println("height:" + sourceImg.getHeight()); // } catch (IOException e1) { // e1.printStackTrace(); // } // long endTime = new Date().getTime(); // System.out.println("使用[BufferedImage]获取图片尺寸耗时:[" + (endTime - beginTime)+"]ms"); // } /** * 使用BufferedImage获取图片尺寸 * @return Map<String,Integer> 返回map集合。包括长和宽 * @param src * 源图片路径 */ public Map<String,Integer> getImageSizeByBufferedImage(String src) { File file = new File(src); FileInputStream is = null; Map<String,Integer> map=new HashMap<String, Integer>(); try { is = new FileInputStream(file); } catch (FileNotFoundException e2) { e2.printStackTrace(); } BufferedImage sourceImg = null; try { sourceImg = javax.imageio.ImageIO.read(is); map.put("width", sourceImg.getWidth()); map.put("height", sourceImg.getHeight()); } catch (IOException e1) { e1.printStackTrace(); } return map; } public String getSrcpath() { return srcpath; } public void setSrcpath(String srcpath) { this.srcpath = srcpath; } }</span>
依据以上方法,假设在方法中增加对应的System.out.println();则能够获得一下结果:
时间: 2024-10-28 05:24:11