ImageReader objects are usually instantiated by Service Provider Interface (SPI) classes in a specific format. The service provider class (such as an instance of ImageReaderSpi) is registered with IIORegistry, which uses the former to format recognition and represent available formats reader and writer.
The BufferedImage subclass describes an Image with an accessible image data buffer. BufferedImage consists of ColorModel and Raster of image data. The number and type of band in Raster's SampleModel must match the required number and type of ColorModel to represent its color and alpha components. The upper left coordinates of all BufferedImage objects are (0,0). Therefore, any Raster used to construct a BufferedImage must satisfy: minX=0 and minY=0.
Needless to say, let's take a look at it.
/** */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 {/** * The name of the source image path is as follows: 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());}/** * Use ImageReader to get the image size* * @param src * Source image path*/public void getImageSizeByImageReader(String src) { long beginTime = new Date().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("height:" + reader.getHeight(0));}catch (IOException e) {e.printStackTrace();}long endTime = new Date().getTime();System.out.println("Time to use [ImageReader] to get image size: [" + (endTime - beginTime)+"]ms");}/** * Use BufferedImage to get image size* * @param src * Source image path*/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("Time to use [BufferedImage] to get image size: [" + (endTime - beginTime)+"]ms");}public String getSrcpath() {return srcpath;}public void setSrcpath(String srcpath) {this.srcpath = srcpath;}}Running results:
It's easy for you to understand... which method should you choose...
Summarize
The above is all the content of this article about obtaining image size instances in ImageReader and BufferedImage in Java. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!