Java Reference
In-Depth Information
Chapter 11
Image handling and processing
11.1 Introduction
In Chapter 6 we presented the basic techniques for loading image files into applets
and applications. We used instances of the
Image
class to hold images and
invoked the
drawImage()
method in the
Graphics
class to display them. In
this chapter we explore further the image handling and processing capabilities of
Java.
In the first few sections we look in greater detail at the
Image
class and
introduce its
BufferedImage
subclass, which offers many useful features. We
discuss how to monitor the loading of images, how to scale image displays, how
to create images, and how to save images to files.
We then switch to topics related to image processing. We show how to gain
access to the pixels of an image, how to modify them, how to make an image
from a pixel array, and how to use these techniques to create animations. We then
discuss the standard filters provided with Java 2D and also give an example of a
custom filter.
We give only a brief overview of the wide range of image tools available with
Java. The classes mentioned here, for example, hold many overloaded construc-
tors and methods that provide many options. See the Java 2 API Specifications
for thorough descriptions of the classes. Also, see the topic by Knudsen [1] and
the other resources for in-depth discussions of images in Java [2,3].
11.2 The
Image
and
BufferedImage
classes
The
Image
class is abstract so you normally deal with instances of platform-
specific subclasses obtained via methods such as
getImage()
in the
Applet
class. The
Image
class provides limited information about an image. The
Java 2D API, which came with Java 1.2, introduced
java.awt.image.
BufferedImage
,which is a non-abstract subclass of
Image
that provides much
greater access to and control of image data.
In Chapter 6 we discussed colors in Java and the
java.awt.Color
class.
Yo u might guess that an image in Java consists internally of a two-dimensional
array of
Color
objects each representing a pixel. This, however, is not practical
365
Search WWH ::
Custom Search