Accumulate Kuibu, Poly stream------Java get the dimensions of the picture

Source: Internet
Author: User

In an article, get it in one of the following two ways:

1, using ImageReader to obtain;

2, using BufferedImage to obtain;

and verified that the ImageReader takes much less time to operate than the bufferedimage operation, see the link to see the original

Then according to the application I make a slight change in the project, 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> Returns the Map collection, containing the length and width * @param src * source picture path */Public map<string,i        nteger> 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>
according to the above method, if the corresponding System.out.println () is added to the method, the result can be obtained:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Accumulate Kuibu, Poly stream------Java get the dimensions of the picture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.