java中ImageReader和BufferedImage获取图片尺寸实例

ImageReader 对象通常由特定格式的服务提供者接口 (SPI) 类实例化。服务提供者类(例如 ImageReaderSpi 的实例)向 IIORegistry 注册,后者使用前者进行格式识别和表示可用格式 reader 和 writer。

BufferedImage子类描述具有可访问图像数据缓冲区的Image。BufferedImage由图像数据的ColorModel和Raster组成。Raster的SampleModel中band的数量和类型必须与ColorModel所要求的数量和类型相匹配,以表示其颜色和alpha分量。所有BufferedImage对象的左上角坐标都为(0,0)。因此,用来构造BufferedImage的任何Raster都必须满足:minX=0且minY=0。

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

java” id=”highlighter_332431″>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
 *
 */
package com.b510;
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.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class ImageUtil {
    /**
   * 源图片路径名称如:c:/1.jpg
   */
    private String srcpath = "e:/pool.jpg";
    public ImageUtil() {
    }
    public static void main(String[] args) throws Exception {
        ImageUtil util = new ImageUtil();
        util.getImageSizeByImageReader(util.getSrcpath());
        util.getImageSizeByBufferedImage(util.getSrcpath());
    }
    /**
   * 使用ImageReader获取图片尺寸
   *
   * @param src
   *      源图片路径
   */
    public void getImageSizeByImageReader(String src) {
        long beginTime = new Date().getTime();
        File file = new File(src);
        try {
            Iterator 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");
    }
    public String getSrcpath() {
        return srcpath;
    }
    public void setSrcpath(String srcpath) {
        this.srcpath = srcpath;
    }
}

运行结果:

java中ImageReader和BufferedImage获取图片尺寸实例

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

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论