O'Reilly logo

Java Examples in a Nutshell, 3rd Edition by David Flanagan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Image Processing

Both Java 1.0 and 1.1 included a complex API for filtering images on the fly as they were downloaded over a network connection. Although this API is still available in later versions of Java, it is not commonly used, nor is it demonstrated in this book. Java 2D defines a simpler API based on the BufferedImageOp interface of the java.awt.image package. This package also includes several versatile implementations of the interface that can generate the image-processing effects illustrated in Figure 12-11. Example 12-13 shows the code used to produce Figure 12-11. The code is straightforward: to process a BufferedImage, simply pass it to the filter( ) method of a BufferedImageOp.

Image processing with BufferedImageOp

Figure 12-11. Image processing with BufferedImageOp

Example 12-13. ImageOps.java

package je3.graphics; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; import java.awt.color.*; /** A demonstration of various image processing filters */ public class ImageOps implements GraphicsExample { static final int WIDTH = 600, HEIGHT = 675; // Size of our example public String getName( ) {return "Image Processing";}// From GraphicsExample public int getWidth( ) { return WIDTH; } // From GraphicsExample public int getHeight( ) { return HEIGHT; } // From GraphicsExample Image image; /** This constructor loads the image we will manipulate */ public ImageOps( ) { java.net.URL imageurl = this.getClass( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required