Presents your JAVA E-NEWSLETTER for September 12, 2002 <-------------------------------------------> EXPAND JAVA IMAGE ABILITIES WITH JAI Java Image abilities are constantly growing, but writing one to disk or returning it as a PNG or JPEG is still black magic. The solution is to use the Java Advanced Imaging (JAI) API. JAI is available as a download from Sun's Java Web site, and it's included in JDK 1.4 in the javax.imageio package. http://java.sun.com/ An automatic JAI installation .exe file is available for Windows, but not for UNIX and Linux. Although JAI can run in a pure-java mode, there are also native libraries for Windows, Linux, and UNIX, which increase the speed. The following example was installed on Apple's OS X, so we opted for the pure-java mode. To install the JAI, you'll need one of the jai tar.gz files. Move the three important jar files, mlibwrapper_jai.jar, jai_codec.jar, and jai_core.jar, into your classpath. We recommend putting them in your JDK's jre/lib/ext directory. The JAI is quite an odd system for Java. Rather than having lots of methods to learn, there's just one top-level class named JAI and a handful of helper methods. The first argument to these methods is an operation name, so the code will look like this: source = JAI.create("fileload", .. ); JAI.create("extrema", src, ...); JAI.create("histogram", src, ...); While this system makes it very easy to plug in your own or third-party functionalities, it also loosens the typing and makes it more difficult to develop under. Turning an AWT Image into a PNG file requires the following snippets of code. import java.awt.Image; import java.awt.image.renderable.ParameterBlock; import javax.media.jai.JAI; import javax.media.jai.PlanarImage; ..... Image img = .... OutputStream out = .... ParameterBlock pb = new ParameterBlock().add(img); PlanarImage src = (PlanarImage) JAI.create("awtImage", pb); JAI.create("encode", src, out, "PNG", null); The above example shows the two ways in which arguments may be passed into the create method. The newer way is to use a ParameterBlock containing all the arguments. We do this for the awtImage operation. The older way is to use an overloaded method. This is now deprecated, but for the example, we do this for the encode operation. The code above will encode an Image to a PNG and write it to the OutputStream. To get further into the JAI, we strongly recommend reading the tutorial. ----------------------------------------