
The article shows you how to use Image input/output library. Image input is not too interesting because there are lot of ways to read an image file in Java. The interesting part is how to write images that you created to files.
Before the existence of Java ImageIO library. A programmer has to use third party package (e.g. ACME gif encoder) to save an image.

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;
public class Test extends JFrame {
BufferedImage bi;
Graphics big; // stands for Buffered Image Graphics
Toolkit toolkit;
MediaTracker tracker;
int width;
int height;
public Test() {
toolkit = Toolkit.getDefaultToolkit();
tracker = new MediaTracker(this);
Image image = toolkit.getImage("mandel.gif");
tracker.addImage(image, 0);
try {
// load all the image for later use
tracker.waitForAll();
} catch (InterruptedException ex) {
}
width = image.getWidth(this);
height = image.getHeight(this);
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
big = bi.getGraphics();
big.drawImage(image, 0, 0, this);
}
public void paint(Graphics g) {
setBackground(Color.white);
Graphics2D g2 = (Graphics2D)g;
TexturePaint paint = new TexturePaint(bi,
new Rectangle2D.Double(0,0,width,height));
g2.setPaint(paint);
g2.fill(new Ellipse2D.Double(25,50,255,125));
}
public static void main(String[] args) {
JFrame f = new Test();
f.setSize(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;
public class ReadTest1 extends JFrame {
BufferedImage bi;
int width;
int height;
public ReadTest1() {
try {
bi = ImageIO.read(new File("mandel.gif"));
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
height = bi.getHeight();
width = bi.getWidth();
}
public void paint(Graphics g) {
setBackground(Color.white);
Graphics2D g2 = (Graphics2D)g;
TexturePaint paint = new TexturePaint(bi,
new Rectangle2D.Double(0,0,width,height));
g2.setPaint(paint);
g2.fill(new Ellipse2D.Double(25,50,255,125));
}
public static void main(String[] args) {
JFrame f = new ReadTest1();
f.setSize(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
ImageIO class has the following read method
read method
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;
public class ReadTest2 extends JFrame {
BufferedImage bi;
int width;
int height;
public ReadTest2() {
try {
InputStream in = new FileInputStream("mandel.gif");
bi = ImageIO.read(in);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
height = bi.getHeight();
width = bi.getWidth();
}
public void paint(Graphics g) {
setBackground(Color.white);
Graphics2D g2 = (Graphics2D)g;
TexturePaint paint = new TexturePaint(bi,
new Rectangle2D.Double(0,0,width,height));
g2.setPaint(paint);
g2.fill(new Ellipse2D.Double(25,50,255,125));
}
public static void main(String[] args) {
JFrame f = new ReadTest2();
f.setTitle("ReadTest2");
f.setSize(300,200);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
|
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;
public class WriteTest1 {
public static void main(String[] args) {
int width = 300;
int height = 300;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0,0, width, height);
g2.setStroke(new BasicStroke(10.0f));
g2.setPaint(new GradientPaint(0,0, Color.green, 50,50, Color.yellow, true));
g2.fill(new Ellipse2D.Float(50,50,200,200));
File f = new File("image.png");
try {
// png is an image format (like gif or jpg)
ImageIO.write(bi, "png", f);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
|
png is an image format. It is similar to jpg or gif. You can replace "png" by "jpg". There is no gif writter provided in Sun's java.
So be careful.write methods.
static boolean write(RenderedImage im, String formatName, File output) throws IOExceptionstatic boolean write(RenderedImage im, String formatName, OutputStream output) throws IOExceptionstatic boolean write(RenderedImage im, String formatName, ImageOutputStream output) throws IOExceptionimage.png. Here is the output file
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.imageio.*;
import java.io.*;
public class WriteTest2 {
public static void main(String[] args) {
int width = 300;
int height = 300;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setColor(Color.white);
g2.fillRect(0,0, width, height);
g2.setStroke(new BasicStroke(10.0f));
g2.setPaint(new GradientPaint(0,0, Color.green, 50,50, Color.yellow, true));
g2.fill(new Ellipse2D.Float(50,50,200,200));
try {
FileOutputStream out = new FileOutputStream("image.jpg");
ImageIO.write(bi, "jpg", out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
|
import javax.imageio.*;
public class GetList {
public static void main(String args[]) {
String readerNames[] =
ImageIO.getReaderFormatNames();
printlist(readerNames, "Reader names:");
String readerMimes[] =
ImageIO.getReaderMIMETypes();
printlist(readerMimes, "Reader MIME types:");
String writerNames[] =
ImageIO.getWriterFormatNames();
printlist(writerNames, "Writer names:");
String writerMimes[] =
ImageIO.getWriterMIMETypes();
printlist(writerMimes, "Writer MIME types:");
}
private static void printlist(String names[], String title) {
System.out.println(title);
for (int i=0, n=names.length; i<n; i++) {
System.out.println("\t" + names[i]);
}
}
}
|
Reader names:
jpeg
gif
JPG
png
jpg
JPEG
Reader MIME types:
image/png
image/jpeg
image/x-png
image/gif
Writer names:
jpeg
JPG
png
jpg
PNG
JPEG
Writer MIME types:
image/png
image/jpeg
image/x-png
|
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;
import javax.imageio.stream.*;
public class ImageReaderTest1 {
public static void main(String[] args) {
// get all the reader that can read the format gif
Iterator readers = ImageIO.getImageReadersByFormatName("gif");
// get the first one among these reader
ImageReader reader = (ImageReader)readers.next();
Object source; // File or InputStream
// first source
source = new File("Bart.gif");
// create image input stream
ImageInputStream iis = null;
try {
// source is a file or an input stream
iis = ImageIO.createImageInputStream(source);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
// attached to the reader by calling
reader.setInput(iis, true);
// example
try {
System.out.println(reader.getHeight(0));
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
}
}
|

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;
import java.util.*;
import javax.imageio.stream.*;
public class ImageWriterTest1 {
public static void main(String[] args) {
// get all the writers that can write png format
Iterator writers = ImageIO.getImageWritersByFormatName("png");
// get the first one
ImageWriter writer = (ImageWriter)writers.next();
// Once an ImageWriter has been obtained, its destination must be set to an ImageOutputStream:
File f = new File("xxx.png");
ImageOutputStream ios;
try {
ios = ImageIO.createImageOutputStream(f);
writer.setOutput(ios);
} catch (IOException ex) {
ex.printStackTrace();
System.exit(0);
}
// Finally, the image may be written to the output stream:
BufferedImage bi = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.setPaint(new GradientPaint(0,0, Color.orange, 0,50, Color.blue , true));
g2.fillRect(0,0, 200, 200);
g2.setPaint(new GradientPaint(0,0, Color.cyan, 50,50, Color.red, true));
g2.fill(new Ellipse2D.Float(50,50,200,200));
try {
writer.write(bi);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
|