Show Image with ImageReader : ImageReader « 2D Graphics GUI « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » 2D Graphics GUI » ImageReader 




Show Image with ImageReader
  

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() throws Exception {
    String filename = "a.png";
    FileInputStream inputStream = new FileInputStream(filename);
    String extensionName = filename.substring(filename.lastIndexOf('.'1);
    Iterator readers = ImageIO.getImageReadersBySuffix(extensionName);
    ImageReader imageReader = (ImageReaderreaders.next();
    ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
    imageReader.setInput(imageInputStream, false);
    int num = imageReader.getNumImages(true);
    images = new BufferedImage[num];
    for (int i = 0; i < num; ++i) {
      images[i= imageReader.read(i);
    }
    inputStream.close();
  }

  public void paint(Graphics g) {
    if (images == null)
      return;
    g.drawImage(images[imageIndex]00null);
    imageIndex = (imageIndex + 1% images.length;
  }

  static public void main(String args[]) throws Exception {
    JFrame frame = new JFrame();
    Panel panel = new Main();
    frame.add(panel);
    frame.setSize(400400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

   
    
  














Related examples in the same category
1.Add Image IO Read Progress Listener to 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.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.