Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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 fin = new FileInputStream("a.gif");
        Iterator readers = ImageIO.getImageReadersBySuffix("GIF");
        ImageReader imageReader = (ImageReader) readers.next();
        ImageInputStream iis = ImageIO.createImageInputStream(fin);
        imageReader.setInput(iis, false);

        imageReader.addIIOReadProgressListener(new 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);
            }
        });

        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);
    }
}