Creating a image directly rather than loading a file is often useful.
Obviously, saving an image object to a file is also quite handy.
Here we show ways to do both with BufferedImage
objects.
Creating
Images
We discussed creation of Image
objects in the Chapter
11: Java section. The BufferedImage
class provides a constructor in which you pass the desired dimensions
and a parameter indicating the type of object.
BufferedImage buffered_image =
new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);
g = buffered_image.getGraphics ();
paint (g);
The overridden getGraphics()
method for BufferedImage
returns a Graphics2D
object typed as its superclass Graphics
type and so must be cast to Graphics2D
to use its methods. This method is present for backward compatibility.
The preferred alternative is to use createGraphics(),
which explicitly returns a Graphics2D
type.
The Java2D API offers many image tools that work primarily with
the BufferedImage
class. You might encounter a situation where you get an Image
object but you want to apply Java 2D operations to the image. You
can obtain a BufferedImage
object from an Image
object by drawing the image onto the graphics context of the BufferedImage.
Graphics2D buf_image_graphics = buffered_image.createGraphics
();
buf_image_graphics.drawImage (image, 0, 0, null);
Another approach to image creation is to build them directly from
pixel arrays. We discussed this for Image
objects in Chapter
11: Java : Pixel Handling. We also discuss in this section how
to build BufferedImage
objects from pixel arrays.
Saving
& Reading
Images
Although the core Java packages always allowed for
reading GIF and JPEG files, not until Java 1.4 did it provide for
saving images to disk files in JPEG format. (Java 1.4 also provided
for reading and writing images in the PNG format.) Previously, Sun
had provided the package com.sun.image.codec.jpeg
for writing to JPEG files but it belonged to the category of extension
package, which meant that it was only available for a limited number
of platforms.
With Java 5.0 came BMP/WBMP image reading and writing.
GIF files can be read but not saved. The GIF encoder involves the
patented LZW compression algorithm so GIF encoding was not included
in the standard Java packages. GIF encoders can be obtained from
third party sources (see below).
JavaTM
Image I/O includes the javax.imageio.*
packages. The javax.imageio.ImageIO
class provides a number of static methods for image reading and
writing with several encoding formats. For example,
...
BufferedImage bi = myImageAnalysis ();
File f = new File ("c:\images\anImage.jpg");
ImageIO.write (bi, "jpeg", f);
The second argument of write
is a string that corresponds to a supported format. A list of supported
formats for output can be obtained with
String[] formatNames
= ImageIO.getWriterFormatNames();
The ImageIO
class similarly offers several read()
methods to obtain images from files via a URL,
File,
and input streams. For example, this code reads an image file via
a File object:
File f = getMyImage();
BufferedImage bi = ImageIO.read(f);
Note that the read method will return only after the image data
is completely read. There is no image input monitoring such as with
a MediaTracker
involved. If you want to use an
ImageIO read methods to obtain a file over the network that
may take considerable time to download, then you might want to do
this in a thread process since the read()
will block until it completes the operation.
A list of the supported image formats for input can be obtained
with
String[] formatNames = ImageIO.getReaderFormatNames();
The Image I/O framework also provide for more sophisticated
image handling such as creating plug-ins for reading and writing
custom image formats. See the Image
I/O API Guide and Java API Specifications for more about these
classes.
Other Java Image Processing
Tools & Techniques
The Java
Advanced Imaging (JAI) API also provides for I/O for a number
of formats
including JPEG, BMP, PNG, PNM, and uncompressed TIFF. (Includes
a reader but no writer for GIF). See the JAI
API Specifications. for the javax.media.jai.*
and com.sun.media.jai.*
packages.
This API, however, is an extension and not part of
standard Java, though it should run correctly with a Java. 1.2 or
later JVM. (A new JAI release will only be compatible with 1.3)
Some platforms provide lower level native code libraries that accelerate
the JAI processing. If the JAI code does not find these libraries,
the programs will run with purely Java classes.
Other third party classes in Java, and thus portable,
can be found for saving images in various formats:
- ImageJ
- Image Processing in Java - open source by Wayne Rasband of the
NIH - supports many formats for both reading/writing.
- Acme.JPM
- includes GIF & JPEG encoders/decoders
- Image Processing in Java
by Douglas Lyon. Java source code comes with the book and provides
for GIF, PPM encoders/decoders
- 24
bit bitmap file - a 1998 JavaWorld tip with source code. (Also,
see the tip for reading MS
BMP files.)
A crude approach for saving/reading images is to use
the file I/O techniques discussed in a Chapter
9 : Java : Binary file. You can simply write an image pixel
array directly to a local disk file. Then you would read the pixel
array and recreate the images as discussed above. To take up less
space, the Java Zip or Gzip I/O can provide some compression.
References & Web
Resources
Last update: Dec. 5, 2004
|