Friday, June 19, 2009

Loading and Saving Image with the Image I/O Library

Introduced in J2SE 1.4, the javax.imageio package is the primary package for the Java Image I/O API. As its name implies, this package helps you read and write image files. You might wonder what's so important about this package. The fact is that you could read images with the getImage method of various classes like Toolkit and Applet since the initial release of the Java platform. But there is more to the javax.imageio package than simply reading images. One important point is that there was no writeImage or putImage previous to the Image I/O library. Now there is a way to write images. Also, you can now set properties such as compression level when you save images.

The first question many people ask when working with the Image I/O libraries is what formats are supported? With Sun's reference implementation, you get a specific set. However, the API is flexible enough so that you can install your own formats by extending the necessary classes in the javax.imageio.spi library. For the moment, let's put that aspect of the library aside. To discover the installed set of readers and writers, you simply ask the ImageIO class through its getReaderFormatNames() and getWriterFormatNames() methods (or getReaderMIMETypes() and getWriterMIMETypes() if you want to work directly with MIME types). Here's an example:

// show image and saving image with difference extension file or name file
import java.awt.image.*;
import javax.imageio.*;
import java.awt.*;
import javax.swing.*;

public class Demo {
private static class FrameShower implements Runnable {
final Frame frame;

public FrameShower(Frame frame) {
this.frame = frame;

public void run() {;

public static void main(String args[]) throws IOException {

// Read
//File file = new File(args[0]);
File file = new File("e:/campuran/ada/nyoba.jpg"); //path file
BufferedImage input =;
File outputFile = new File("e:/campuran/ada/image.png"); //path file
ImageIO.write(input, "PNG", outputFile);
// Convert ukuran
Kernel sharpKernel = new Kernel(3, 3, new float[] { 0.0f, -1.0f, 0.0f,
-1.0f, 5.0f, -1.0f, 0.0f, -1.0f, 0.0f });
ConvolveOp convolveOp = new ConvolveOp(sharpKernel,
ConvolveOp.EDGE_NO_OP, null);
int width = input.getWidth();
int height = input.getHeight();
BufferedImage output = new BufferedImage(width, height,
convolveOp.filter(input, output);
// membuat tampilan
Icon icon = new ImageIcon(output);
JLabel label = new JLabel(icon);
JFrame frame = new JFrame("Gambare");
frame.getContentPane().add(label, BorderLayout.CENTER);
// show image
Runnable runner = new FrameShower(frame);

You can check list from IO library javax.imageio.spi with :

import javax.imageio.*;

public class GetList {
public static void main(String args[]) {
String readerNames[] =
printlist(readerNames, "Reader names:");
String readerMimes[] =
printlist(readerMimes, "Reader MIME types:");
String writerNames[] =
printlist(writerNames, "Writer names:");
String writerMimes[] =
printlist(writerMimes, "Writer MIME types:");
private static void printlist(String names[],
String title) {
for (int i=0, n=names.length; i < n; i++) {
System.out.println("\t" + names[i]);

good luck to try

No comments:

Post a Comment