Obtained in an article through, for example, the following two ways:
1, using ImageReader to obtain;
2, using BufferedImage to obtain;
and verifying that the imagereader takes much less time to operate than the bufferedimage operation, see the link to see the original
Then I make a slight change in the project based on my own application, which is recorded here:
<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;//Get the picture's long-width size public class Imageutil {/** * source picture path name such as: D:/zz/over.png */private String Srcpath = "D:/zz/over.png"; Public Imageutil () {}//local validation public static void main (string[] args) throws Exception {Imageutil util = New Imageutil (); Util.getimagesizebybufferedimage (Util.getsrcpath ()); The size of the corresponding picture according to the picture path public Map getsize (String srcpath) {imageutil util = new Imageutil (); Map<string, Integer> map=util.getimagesizebybufferedimage (Util.getsrcpath ()); return map; /** * Use ImageReader to get picture size * * @paraM src * Source picture path */public void Getimagesizebyimagereader (String src) {long beginTime = new Dat E (). 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 ("H Eight: "+ reader.getheight (0)); } catch (IOException e) {e.printstacktrace (); } Long EndTime = new Date (). GetTime ();//System.out.println ("Use [ImageReader] to get picture size time: [" + (Endtime-begintime ) + "]ms"); }///**//* Use BufferedImage to get picture size//*//* @param src//* source picture path//*///public void ge Timagesizebybufferedimage (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 Sour Ceimg = null;//try {//sourceimg = Javax.imageio.ImageIO.read (IS);//System.out.println ("wid Th: "+ sourceimg.getwidth ());//System.out.println (" Height: "+ sourceimg.getheight ());/} catch (Ioexce Ption E1) {//E1.printstacktrace ();//}//long endTime = new Date (). GetTime ();//System.ou T.PRINTLN ("Use [bufferedimage] to get picture size time: [" + (Endtime-begintime) + "]ms");/}/** * Use BufferedImage to get picture size * @return map<string,integer> return to the Map collection. Includes length and width * @param src * source picture path */public map<string,integer> getimagesizebybufferedimage (strin g 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>
based on the above method, it is assumed that the corresponding System.out.println () is added to the method to obtain the result:
Accumulate Kuibu, Poly stream------Java get the dimensions of the picture