Java to quickly obtain the resolution of large images (large image formats: JPG, tiff, and eg)

Source: Internet
Author: User

Question: How can I quickly obtain the resolution of a 20 mb image? Program code: 1 package test; 2 3 import java. awt. dimension; 4 import java. awt. image. bufferedImage; 5 import java. io. file; 6 import java. io. IOException; 7 import java. util. iterator; 8 import javax. imageio. imageIO; 9 import javax. imageio. imageReader; 10 import javax. imageio. stream. fileImageInputStream; 11 import javax. imageio. stream. imageInputStream; 12 13/** 14 * the resolution for reading large images is 15*16 * @ author bruce 17*18 */19 public class ReadResolution {20/** 21 * Get 22 * @ param file 23 * @ return image resolution 24 * @ throws IOException 25 */26 public through BufferedImage static String getResolution1 (File file) throws IOException {27 BufferedImage image = ImageIO. read (file); 28 return image. getWidth () + "x" + image. getHeight (); 29} 30 31/** 32 * Get the image resolution 33*34 * @ param path 35 * @ return 36 */37 public static Dim Ension getImageDim (String path) {38 Dimension result = null; 39 String suffix = getFileSuffix (path); 40 // decode 41 Iterator with a given suffix <ImageReader> iter = ImageIO. getImageReadersBySuffix (suffix); 42 System. out. println (ImageIO. getImageReadersBySuffix (suffix); 43 if (iter. hasNext () {44 ImageReader reader = iter. next (); 45 try {46 ImageInputStream stream = new FileImageInputStream (new File (47 path )); 48 reader. setInput (stream); 49 int width = reader. getWidth (reader. getMinIndex (); 50 int height = reader. getHeight (reader. getMinIndex (); 51 result = new Dimension (width, height); 52} catch (IOException e) {53 e. printStackTrace (); 54} finally {55 reader. dispose (); 56} 57} 58 System. out. println ("getImageDim:" + result); 59 return result; 60} 61 62/** 63 * Get the image suffix 64 * @ param path 65 * @ r Eturn 66 */67 private static String getFileSuffix (final String path) {68 String result = null; 69 if (path! = Null) {70 result = ""; 71 if (path. lastIndexOf ('.')! =-1) {72 result = path. substring (path. lastIndexOf ('. '); 73 if (result. startsWith (". ") {74 result = result. substring (1); 75} 76} 77} 78 System. out. println ("getFileSuffix:" + result); 79 return result; 80} 81 82/** 83 * capture the Dimension object to obtain the resolution 84 * @ param path 85*86 * @ return 87 */88 public static String getResolution2 (String path) {89 String s = getImageDim (path ). toString (); 90 s = s. substr Ing (s. indexOf ("[") + 1, s. indexOf ("]"); 91 String w = s. substring (s. indexOf ("=") + 1, s. indexOf (","); 92 String h = s. substring (s. lastIndexOf ("=") + 1); 93 String result = w + "x" + h; 94 System. out. println ("getResolution:" + result); 95 return result; 96} 97 98/** 99 * test 100*101 * @ param args102 * @ throws IOException103 */104 public static void main (String [] args) throws IOException {105 String path = "d :\\ abc.. JPG "; 106 File file = new File (path); 107 System. out. println ("1st method time used:"); 108 long s1 = System. currentTimeMillis (); 109 System. out. println (getResolution1 (file); 110 long l1 = System. currentTimeMillis (); 111 System. out. println (l1-s1) + "ms"); 112 System. out. println ("******************************"); 113 System. out. println ("2nd method time used:"); 114 long s2 = System. currentTimeMillis (); 1 15 getResolution2 (path); 116 long l2 = System. currentTimeMillis (); 117 System. out. println (l2-s2) + "ms"); 118} 119} running result: time used by the 1st methods: 11935x85544867ms ***************************** 2nd method use time: getFileSuffix: JPGjavax. imageio. imageIO $ ImageReaderIterator @ 11 ddcdegetImageDim: java. awt. dimension [width = 11935, height = 8554] getResolution: 11935x85540 ms Note: because the first method is to read images into the memory at a time, you need to set the JVM runtime memory, the specific method is run icon drop-down-Run Configu Rations-fill in VM arguments-Xms256m-Xmx1024m explanation: the Dimension class encapsulates the width and height of components in a single object (precise to an integer ). The ImageIO class contains static and convenient methods for searching ImageReader and ImageWriter, and performing simple encoding and decoding. ImageReader is used to parse and decode abstract superclasses of images.

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.