[ZZ] Use JAI to extend Java image processing capabilities

xiaoxiao2021-03-06  103

Java's image processing capability is constantly growing, but writing a file such as a PNG or JPEG format or reading such an operation from disk is still a witchcraft. The solution is to use the Java Advanced Imaging (JAI) API. Jai can download in the Java website of Sun, which has been included in Javax.ImageIO package in JDK1.4.

The JAI installation file with an EXE format for the Windows platform can be downloaded, but there is no installation file for UNIX and Linux. Although JAI can run in pure Java mode, it also provides local libraries for Windows, Linux, and UNIX, using them to improve running speed.

The following example is installed on an OS X system installed on the Apple machine, so we have selected pure Java mode. To install Jai, you need a jai tar.gz file. Move the three important JAR files mlibwrapper_jar.jar, jar_codec.jar and jar_core.jar to your ClassPath environment variable specified in the directory specified. We recommend put them in your JDK's JRE / LIB / EXT directory.

In fact, JAI is a temporary system in Java. There is only one top class named JAI and a few auxiliary methods, and there are not many ways to learn. The first parameters in these methods are operands, so the code is like this:

SRC = ja.create ("fileload", ..);

Jai.create ("Extrema", SRC, ...);

Jai.create ("Histogram", SRC, ...);

Such systems can make it easy to combine with your own or third parties, and also make the coupling between types loose, which in turn makes it more difficult to develop this.

Convert an AWT graphic into a PNG file requires the following code segment:

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 demonstrates two ways to pass parameters to the Create method, where the newer method is to use a parameterblock containing all parameters, which we do this in the operation of the AWTIMAGE. The old method uses an overloaded method, which is now deprecated (ie, the old method, not recommended) method, but in this example, we use it for the Encode operation.

The above code will encode an image in PNG format and write to OutputStream. If you want to learn more about JAI, we strongly recommend you to read the JAI guide.

转载请注明原文地址:https://www.9cbs.com/read-103205.html

New Post(0)