zoukankan      html  css  js  c++  java
  • java开发_比较使用ImageReader和BufferedImage获取图片尺寸总结

    多的不用说,先上代码,大家伙儿看看

     1 /**
     2  * 
     3  */
     4 package com.b510;
     5 
     6 import java.awt.image.BufferedImage;
     7 import java.io.File;
     8 import java.io.FileInputStream;
     9 import java.io.FileNotFoundException;
    10 import java.io.IOException;
    11 import java.util.Date;
    12 import java.util.Iterator;
    13 
    14 import javax.imageio.ImageIO;
    15 import javax.imageio.ImageReader;
    16 import javax.imageio.stream.ImageInputStream;
    17 
    18 public class ImageUtil {
    19 
    20     /**
    21      * 源图片路径名称如:c:/1.jpg
    22      */
    23     private String srcpath = "e:/pool.jpg";
    24 
    25     public ImageUtil() {
    26 
    27     }
    28 
    29     public static void main(String[] args) throws Exception {
    30         ImageUtil util = new ImageUtil();
    31         util.getImageSizeByImageReader(util.getSrcpath());
    32         util.getImageSizeByBufferedImage(util.getSrcpath());
    33     }
    34 
    35     /**
    36      * 使用ImageReader获取图片尺寸
    37      * 
    38      * @param src
    39      *            源图片路径
    40      */
    41     public void getImageSizeByImageReader(String src) {
    42         long beginTime = new Date().getTime();
    43         File file = new File(src);
    44         try {
    45             Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
    46             ImageReader reader = (ImageReader) readers.next();
    47             ImageInputStream iis = ImageIO.createImageInputStream(file);
    48             reader.setInput(iis, true);
    49             System.out.println("" + reader.getWidth(0));
    50             System.out.println("height:" + reader.getHeight(0));
    51         } catch (IOException e) {
    52             e.printStackTrace();
    53         }
    54         long endTime = new Date().getTime();
    55         System.out.println("使用[ImageReader]获取图片尺寸耗时:[" + (endTime - beginTime)+"]ms");
    56     }
    57 
    58     /**
    59      * 使用BufferedImage获取图片尺寸
    60      * 
    61      * @param src
    62      *            源图片路径
    63      */
    64     public void getImageSizeByBufferedImage(String src) {
    65         long beginTime = new Date().getTime();
    66         File file = new File(src);
    67         FileInputStream is = null;
    68         try {
    69             is = new FileInputStream(file);
    70         } catch (FileNotFoundException e2) {
    71             e2.printStackTrace();
    72         }
    73         BufferedImage sourceImg = null;
    74         try {
    75             sourceImg = javax.imageio.ImageIO.read(is);
    76             System.out.println("" + sourceImg.getWidth());
    77             System.out.println("height:" + sourceImg.getHeight());
    78         } catch (IOException e1) {
    79             e1.printStackTrace();
    80         }
    81         long endTime = new Date().getTime();
    82         System.out.println("使用[BufferedImage]获取图片尺寸耗时:[" + (endTime - beginTime)+"]ms");
    83     }
    84 
    85     public String getSrcpath() {
    86         return srcpath;
    87     }
    88 
    89     public void setSrcpath(String srcpath) {
    90         this.srcpath = srcpath;
    91     }
    92 
    93 }

    运行结果:

    大家可以很容易明白...应该选择哪个方法了吧...

  • 相关阅读:
    返回顶部
    C# 对文本文件的几种读写方法
    cocos2dx 锁定30帧设置
    AndroidManifest.xml 屏幕上下反转
    粒子系统主
    CCParticleSystem粒子系统
    精灵的优化
    cocos2dx 菜单按钮回调方法传参 tag传参
    cocos2dx跨平台使用自定义字体
    ios7 Cocos2dx 隐藏状态栏设置
  • 原文地址:https://www.cnblogs.com/hongten/p/hongten_java_ImageReader_BufferedImage.html
Copyright © 2011-2022 走看看