Java by API/javax.imageio/ImageReader — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

ImageReader: addIIOReadProgressListener(IIOReadProgressListener listener)

  

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





ImageReader: getFormatName()

  
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class Main {
  public static void main(String[] argv) throws Exception {
    File file = new File("image.gif");
    System.out.println(getFormatName(file));
    InputStream is = new FileInputStream(file);
    is.close();
    System.out.println(getFormatName(is));
  }
  private static String getFormatName(Object o) {
    try {
      ImageInputStream iis = ImageIO.createImageInputStream(o);
      Iterator iter = ImageIO.getImageReaders(iis);
      if (!iter.hasNext()) {
        return null;
      }
      ImageReader reader = (ImageReader) iter.next();
      iis.close();
      return reader.getFormatName();
    } catch (IOException e) {
    }
    return null;
  }
}





ImageReader: getNumImages(boolean allowSearch)

  
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;
public class Main extends Panel{
  private BufferedImage images[];
  private int imageIndex = 0;
  public Main(String filename) throws Exception{
      FileInputStream fin = new FileInputStream(filename);
      String suffix = filename.substring(filename.lastIndexOf(".") + 1);
      System.out.println("suf " + suffix);
      Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
      ImageReader imageReader = (ImageReader) readers.next();
      ImageInputStream iis = ImageIO.createImageInputStream(fin);
      imageReader.setInput(iis, false);
      int num = imageReader.getNumImages(true);
      images = new BufferedImage[num];
      for (int i = 0; i < num; ++i) {
        images[i] = imageReader.read(i);
      }
      fin.close();
  }
  public void paint(Graphics g) {
    if (images == null)
      return;
    g.drawImage(images[imageIndex], 0, 0, null);
    imageIndex = (imageIndex + 1) % images.length;
  }
  static public void main(String args[]) throws Exception {
    JFrame frame = new JFrame("ShowImageIR.java");
    Panel panel = new Main(args[0]);
    frame.getContentPane().add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





ImageReader: read(int imageIndex)

  
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;
public class Main extends Panel{
  private BufferedImage images[];
  private int imageIndex = 0;
  public Main(String filename) throws Exception{
      FileInputStream fin = new FileInputStream(filename);
      String suffix = filename.substring(filename.lastIndexOf(".") + 1);
      System.out.println("suf " + suffix);
      Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
      ImageReader imageReader = (ImageReader) readers.next();
      ImageInputStream iis = ImageIO.createImageInputStream(fin);
      imageReader.setInput(iis, false);
      int num = imageReader.getNumImages(true);
      images = new BufferedImage[num];
      for (int i = 0; i < num; ++i) {
        images[i] = imageReader.read(i);
      }
      fin.close();
  }
  public void paint(Graphics g) {
    if (images == null)
      return;
    g.drawImage(images[imageIndex], 0, 0, null);
    imageIndex = (imageIndex + 1) % images.length;
  }
  static public void main(String args[]) throws Exception {
    JFrame frame = new JFrame("ShowImageIR.java");
    Panel panel = new Main(args[0]);
    frame.getContentPane().add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





ImageReader: setInput(Object input, boolean seekForwardOnly)

  

import java.awt.Graphics;
import java.awt.Panel;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.JFrame;
public class Main extends Panel{
  private BufferedImage images[];
  private int imageIndex = 0;
  public Main(String filename) throws Exception{
      FileInputStream fin = new FileInputStream(filename);
      String suffix = filename.substring(filename.lastIndexOf(".") + 1);
      System.out.println("suf " + suffix);
      Iterator readers = ImageIO.getImageReadersBySuffix(suffix);
      ImageReader imageReader = (ImageReader) readers.next();
      ImageInputStream iis = ImageIO.createImageInputStream(fin);
      imageReader.setInput(iis, false);
      int num = imageReader.getNumImages(true);
      images = new BufferedImage[num];
      for (int i = 0; i < num; ++i) {
        images[i] = imageReader.read(i);
      }
      fin.close();
  }
  public void paint(Graphics g) {
    if (images == null)
      return;
    g.drawImage(images[imageIndex], 0, 0, null);
    imageIndex = (imageIndex + 1) % images.length;
  }
  static public void main(String args[]) throws Exception {
    JFrame frame = new JFrame("ShowImageIR.java");
    Panel panel = new Main(args[0]);
    frame.getContentPane().add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}