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

Home
Java
1.2D Graphics GUI
2.2D Graphics GUI1
3.3D
4.Advanced Graphics
5.Ant
6.Apache Common
7.Chart
8.Class
9.Collections Data Structure
10.Data Type
11.Database SQL JDBC
12.Design Pattern
13.Development Class
14.EJB3
15.Email
16.Event
17.File Input Output
18.Game
19.Generics
20.GWT
21.Hibernate
22.I18N
23.J2EE
24.J2ME
25.JDK 6
26.JNDI LDAP
27.JPA
28.JSP
29.JSTL
30.Language Basics
31.Network Protocol
32.PDF RTF
33.Reflection
34.Regular Expressions
35.Scripting
36.Security
37.Servlets
38.Spring
39.Swing Components
40.Swing JFC
41.SWT JFace Eclipse
42.Threads
43.Tiny Application
44.Velocity
45.Web Services SOA
46.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » 2D Graphics GUI » ImageReaderScreenshots 
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.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.