Add Image IO Read Progress Listener to ImageReader
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriter;
import javax.imageio.event.IIOReadProgressListener;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
public class Main{
static public void main(String args[]) throws Exception {
FileInputStream fileInputStream = new FileInputStream("a.gif");
Iterator readers = ImageIO.getImageReadersBySuffix("GIF");
ImageReader imageReader = (ImageReader) readers.next();
ImageInputStream imageInputStream = ImageIO.createImageInputStream(fileInputStream);
imageReader.setInput(imageInputStream, false);
imageReader.addIIOReadProgressListener(new MyIIOReadProgressListener());
BufferedImage image = imageReader.read(0);
Iterator imageWriters = ImageIO.getImageWritersBySuffix("JPG");
ImageWriter imageWriter = (ImageWriter) imageWriters.next();
File file = new File("b.JPG");
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
imageWriter.setOutput(ios);
imageWriter.write(image);
}
}
class MyIIOReadProgressListener implements IIOReadProgressListener{
public void imageComplete(ImageReader source) {
System.out.println("image complete " + source);
}
public void imageProgress(ImageReader source, float percentageDone) {
System.out.println("image progress " + source + ": " + percentageDone + "%");
}
public void imageStarted(ImageReader source, int imageIndex) {
System.out.println("image #" + imageIndex + " started " + source);
}
public void readAborted(ImageReader source) {
System.out.println("read aborted " + source);
}
public void sequenceComplete(ImageReader source) {
System.out.println("sequence complete " + source);
}
public void sequenceStarted(ImageReader source, int minIndex) {
System.out.println("sequence started " + source + ": " + minIndex);
}
public void thumbnailComplete(ImageReader source) {
System.out.println("thumbnail complete " + source);
}
public void thumbnailProgress(ImageReader source, float percentageDone) {
System.out.println("thumbnail started " + source + ": " + percentageDone + "%");
}
public void thumbnailStarted(ImageReader source, int imageIndex, int thumbnailIndex) {
System.out.println("thumbnail progress " + source + ", " + thumbnailIndex + " of "
+ imageIndex);
}
}
Related examples in the same category
1. | Show Image with ImageReader | | |
2. | Read an Image from a file | | |
3. | Read an Image from inputStream | | |
4. | Read an Image from URL | | |
5. | Determining the Format of an Image in a File | | |
6. | Detect the file type of the input stream prior to reading the image | | |
7. | List the image formats that can be read and written | | |
8. | Get file format, image resolution, number of bits per pixel (JPEG, GIF, BMP, PCX, PNG, IFF, RAS, PBM, PGM, PPM, PSD and SWF files) | | |
9. | Image format info | | |
10. | Image observer blocks until the image is completely loaded. AWT defers the loading of images until they are painted on a graphic. | | |