![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
|
Developing Imaging Applications Using the Java2D[tm], JAI and New ImageIO APIsby C.K. Prasad(June 2002) We want to hear from you! Please send us your FEEDBACK. The following technical article may contain actual software programs in source code form. This source code is made available for developers to use as needed, pursuant to the terms and conditions of this license.
OverviewCurrently a Java[tm] platform developer can choose any one of the Java2D, Java[tm] Advanced Imaging ("JAI") or ImageIO API approaches to develop their application for imaging. In this article we will discuss how to use these image interfaces to perform common imaging operations. Early versions of the Java[tm] programming language Abstract Windowing Toolkit ("AWT") classes provided a simple rendering package suitable for rendering common HTML pages, but without the features necessary for complex imaging. The Java 2D API extended the early AWT by adding support for more general graphics and rendering operations. The Java Advanced Imaging (JAI) API further extends the Java platform (including the Java 2D API) by allowing sophisticated, high-performance image processing to be incorporated into Java programming language applets and applications. ImageIO is a new API for the Java[tm] 2 Platform, Standard Edition, version 1.4 . It provides a pluggable architecture for new image formats and uses image datatypes based on the Java2D API. Image IO is considered a simple, high-level API for ease of use; it is the migration path for com.sun.image.jpeg classes, and Java Advanced Imaging API codecs. First we will start with the usage of Java2D
API interfaces to perform some common imaging operations like: Next we will use the JAI
interfaces to perform operations like: Finally, we will use the
new ImageIO API in Java[tm] 2 SDK version 1.4 to: Setting Up Your System for the Code Samples This article includes several code samples that are designed to reinforce the following technical information. In order to run these code samples you will need to ensure that your system is set up correctly and is running a compatible version of the Java 2 platform. Please refer to System Setup Details. I) Using the Java2D APIThe Java 2D API provides enhanced two-dimensional graphics, text, and imaging capabilities for Java programs through extensions to the AWT. We start by importing all the necessary packages. import java.awt.*; Then we load our GIF or JPEG into an Image object using the Applet method getImage(). //read a jpeg image from an input file When a component needs to be displayed, its paint() or update() method is automatically invoked with an appropriate Graphics context. You can use the Graphics2D rendering hints attribute to specify whether you want objects to be rendered as quickly as possible or whether you prefer that the rendering quality be as high as possible. public void paint(Graphics g) {
.... BufferedImage can be used to hold and to manipulate image data retrieved from a file. To display the image, you need to set up the Graphics2D rendering context and then call one of the Graphics2D rendering methods. The graphic elements are rendered off-screen to the BufferedImage and are then copied to the screen through a call to Graphics2D drawImage(). BufferedImage bi = (BufferedImage) createImage(w, h); You need to create a Graphics2D context for the BufferedImage by using the createGraphics() method. Then you can use the drawImage() method from the Graphics2D class to draw the image into the buffered image. Graphics2D big = bi.createGraphics();
//draw some strings with
Font() on the BufferedImage //display the BufferedImage
on the screen Once the image is completely drawn, you can use the JPEGImageEncoder class of the non-standard com.sun.image.codec.jpeg package to encode the image. Or, if you use the Java[tm] 2 SDK, Standard Edition, v 1.4, you can use the standardImageIO class. //save the
BufferedImage as a jpeg
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
} catch (Exception ex)
{
Rotating Images You can construct a new AffineTransform and change the Graphics2D transform attribute by calling transform. We set the x, y coordinates of rotate() so that the image is rendered around an anchor point. double x = 0.5*w;
where bi is BufferedImage II) Using the Java Advanced Imaging APIImage acquisition and display are relatively easy in JAI. In this code example, we read a JPEG image, render into a BufferedImage and TIFF encode it. JAI packages that needs to be imported are: import javax.media.jai.*; The easiest way to load a local image file stored in JPEG or any other supported image file format is to use the "fileload" operation as follows: private static RenderedImage img; // Read the image from
the designated path.
Call RenderedImageAdapter to convert the image into PlanarImages. The PlanarImage implements the java.awt.image.RenderedImage interface. RenderedImageAdapter ria = new RenderedImageAdapter(img); or we could simply call // PlanarImage image = JAI.create("fileload", fileName); Use getAsBufferedImage on the PlanarImage to produce
a BufferedImage. BufferedImage bi = ria.getAsBufferedImage(); Once we get the buffered image, the rest of the code is quite similar to the Java2D API discussed previously. Graphics2D big = bi.createGraphics(); big.setFont(new
Font("Dialog", Font.PLAIN, 10));
g2.drawImage(bi, 0, 0, this); //Define the destination file name File file = new
File("images", "test.tiff");
Then we create the image encoder and encode the buffered image in TIFF. TIFFEncodeParam params =
new TIFFEncodeParam();
III) Using the New ImageIO APIThe Java programming language Image I/O API provides a pluggable architecture for working with images stored in files and accessed across the network. The javax.imageio.ImageIO class provides a set of static convenience methods that perform most simple Image I/O operations. ImageIO packages that needs to be imported are: import javax.imageio.*; Reading a JPEG, GIF or PNG image is simple as shown below. The format of the image will be auto-detected by the API based on the contents of the file.
File f = new File("duke.jpg");
Writing an image in a supported format is equally simple:
File file = new File("images", "test14.jpng");
The list of supported formats may be obtained by calling ImageIO.getWriterFormatNames(). Summary In this article we saw how to develop Java platform applications using the Java2D API, JAI and ImageIO Interfaces by covering the various APIs. We discussed each API, and learned about the Graphics context, BufferedImage operations, reading images, image encoding among various supported image formats, rotating images, displaying images on the screen and saving them to an image file in one of the supported formats. Through the code samples contained here, we have seen some practical uses of the Imaging APIs, although we will not be using them to their full potential until they are used to perform sophisticated imaging operations. For Further Information Java2D Tutorial
Sources: JPEGJ2d.java
Note: These samples have been modified
from the tutorials above for illustration purpose. | |||||||||||||||||||||||||||||||||||||||
![]() |
![]() |