?? programming in java advanced imaging3.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0104)http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html -->
<HTML><HEAD><TITLE>Programming in Java Advanced Imaging</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content=reference name=collection>
<META content="MSHTML 6.00.2900.3132" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff>
<CENTER><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html"><IMG
alt=Contents src="Programming in Java Advanced Imaging3.files/contents.gif"></A>
<A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/J2D-concepts.doc.html"><IMG
alt=Previous src="Programming in Java Advanced Imaging3.files/previous.gif"></A>
<A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Acquisition.doc.html"><IMG
alt=Next src="Programming in Java Advanced Imaging3.files/next.gif"></A>
<P><FONT size=5><I>Programming in Java Advanced Imaging</I></FONT> </CENTER><BR>
<CENTER><A name=47227>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right><FONT size=3>C H A P T E R</FONT><FONT size=7><IMG
src="Programming in Java Advanced Imaging3.files/sm-space.gif">3</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<CENTER><A name=47285>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right>
<HR noShade SIZE=7>
<FONT size=6>Programming in Java Advanced Imaging
</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<BLOCKQUOTE>
<P><BR><BR><BR>
<P><FONT size=7><B>T</B></FONT>HIS chapter describes how to get started
programming with the Java Advanced Imaging (JAI) API.
<P><A name=53111>
<H2>3.1 <IMG
src="Programming in Java Advanced Imaging3.files/space.gif">Introduction</H2></A>An
imaging operation within JAI is summarized in the following four steps:
<P>
<UL>1. Obtain the source image or images. Images may be obtained in one of
three ways (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Acquisition.doc.html#81550">Chapter
4, "Image Acquisition and Display</A>"):
<P>
<UL>a. Load from an image file such as GIF, TIFF, or JPEG
<P>b. Fetch the image from another data source, such as a remote server
<P>c. Generate the image internally
<P></P></UL>2. Define the imaging graph. This is a two part process:
<P>
<UL>a. Define the image operators (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#55112">Section
3.6, "JAI API Operators</A>")
<P>b. Define the parent/child relationship between sources and sinks
<P></P></UL>3. Evaluate the graph using one of three execution models:
<P>
<UL>a. Rendered execution model (Immediate mode - see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#56858">Section
3.3.1, "Rendered Graphs</A>")
<P>b. Renderable execution model (Deferred mode - see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#55932">Section
3.3.2, "Renderable Graphs</A>")
<P>c. Remote execution model (Remote mode - see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#56364">Section
3.4, "Remote Execution</A>")
<P></P></UL>4. Process the result. There are four possible destinations:
<P>
<UL>a. Save the image in a file
<P>b. Display the image on the screen
<P>c. Print the image on a printer or other output device
<P>d. Send the image to another API, such as Swing
<P></P></UL></UL><A name=53105>
<H2>3.2 <IMG src="Programming in Java Advanced Imaging3.files/space.gif">An
Overview of Graphs</H2></A>In JAI, any operation is defined as an object. An
operator object is instantiated with zero or more image sources and other
parameters that define the operation. Two or more operators may be strung
together so that the first operator becomes an image source to the next
operator. By linking one operator to another, we create an imaging
<EM>graph</EM> or <EM>chain</EM>.
<P>In its simplest form, the imaging graph is a chain of operator objects with
one or more image sources at one end and an image <EM>sinc</EM> (or "user") at
the other end. The graph that is created is commonly known as a <EM>directed
acyclic graph</EM> (DAG), where each object is a <EM>node</EM> in the graph
and object references form the <EM>edges</EM> (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#54895">Figure
3-1</A>).
<P><A name=54894>
<HR>
<CENTER><IMG
src="Programming in Java Advanced Imaging3.files/Programming-environ.doc.anc.gif"></CENTER>
<HR>
</A><A name=54895>
<CENTER><FONT size=-1><B><I>Figure 3-1 </I><IMG
src="Programming in Java Advanced Imaging3.files/sm-blank.gif" border=0> An
Example DAG</B></FONT></CENTER></A>
<P>Most APIs simply leave the DAG structure of images and operators implicit.
However, JAI makes the notion of a <EM>processing graph</EM> explicit and
allows such graphs to be considered as entities in their own right. Rather
than thinking only of performing a series of operations in sequence, you can
consider the graph structure produced by the operations. The graph form makes
it easier to visualize the operations.
<P>A directed acyclic graph is a graph containing no cycles. This means that
if there is a route from node A to node B then there should be no way back.
Normally, when creating a graph by instantiating new nodes one at a time,
cycles are easily avoided. However, when reconfiguring a graph, you must be
careful not to introduce cycles into the graph.
<P><A name=55910>
<H2>3.3 <IMG
src="Programming in Java Advanced Imaging3.files/space.gif">Processing
Graphs</H2></A>JAI extends rendering independence, which was introduced in the
Java 2D API. With rendering independence, you have the ability to describe an
image as you want it to appear, independent of any specific instance of it.
<P>In most imaging APIs, the application must know the exact resolution and
size of the source image before it can begin any imaging operations on the
image. The application must also know the resolution of the output device
(computer monitor or color printer) and the color and tonal quality of the
original image. A rendering-independent description is concerned with none of
these. Rendering-independent sources and operations permit operations to be
specified in resolution-independent coordinates.
<P>Think of rendering independence a bit like how a PostScript file is handled
in a computer. To display a PostScript file on a monitor or to print the file
to a high-resolution phototypesetter, you don't need to know the resolution of
the output device. The PostScript file is essentially rendering independent in
that it displays properly no matter what the resolution of the output device
is.
<P>JAI has a "renderable" mode in which it treats all image sources as
rendering independent. You can set up a graph (or chain) of renderable
operations without any concern for the source image resolution or size; JAI
takes care of the details of the operations.
<P>JAI introduces two different types of graphs: rendered and renderable.
<P>
<HR noShade>
<B>Note:</B> The following two sections, "<A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#56858">Rendered
Graphs</A>" and "<A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#55932">Renderable
Graphs</A>," are for advanced JAI users. Most programmers will use JAI's
Rendered mode and don't really need to know about the Renderable mode.
<HR noShade>
<A name=56858>
<H3>3.3.1 <IMG
src="Programming in Java Advanced Imaging3.files/space.gif">Rendered
Graphs</H3></A>Rendered graphs are the simplest form of rendering in JAI.
Although Renderable graphs have the advantage of rendering-independence,
eliminating the need to deal directly with pixels, Rendered graphs are useful
when it is necessary to work directly with the pixels.
<P>A Rendered graph processes images in immediate mode. For any node in the
graph, the image source is considered to have been evaluated at the moment it
is instantiated and added to the graph. Or, put another way, as a new
operation is added to the chain, it appears to compute its results
immediately.
<P>A Rendered graph is composed of Rendered object nodes. These nodes are
usually instances of the <CODE>RenderedOp</CODE> class, but could belong to
any subclass of <CODE>PlanarImage</CODE>, JAI's version of
<CODE>RenderedImage</CODE>.
<P>Image sources are objects that implement the <CODE>RenderedImage</CODE>
interface. These sources are specified as parameters in the construction of
new image objects.
<P>Let's take a look at an example of a rendered graph in <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Programming-environ.doc.html#61982">Listing
3-1</A>. This example, which is a code fragment rather than an entire class
definition, creates two constant images and then adds them together.
<P><CAPTION><FONT size=-1><B><A name=61982>
<CENTER><FONT size=-1><B><I>Listing 3-1 </I><IMG
src="Programming in Java Advanced Imaging3.files/sm-blank.gif" border=0>
Rendered Chain Example </B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="4" colspan="1"><PRE> import javax.jai.*;
import javax.jai.widget.*;
import java.awt.Frame;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="2" colspan="1"><PRE> public class AddExample extends Frame {
</PRE><TR valign="top"><TR valign="top"><TD rowspan="4" colspan="1"><PRE> // ScrollingImagePanel is a utility widget that
// contains a Graphics2D (i.e., is an image sink).
ScrollingImagePanel imagePanel1;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="5" colspan="1"><PRE> // For simplicity, we just do all the work in the
// class constructor.
public AddExample(ParameterBlock param1,
ParameterBlock param2) {
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="3" colspan="1"><PRE> // Create a constant image
RenderedOp im0 = JAI.create("constant", param1);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="4"
colspan="1"><PRE> // Create another constant image.
RenderedOp im1 = JAI.create("constant", param2);
// Add the two images together.
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="2" colspan="1"><PRE> RenderedOp im2 = JAI.create("add", im0, im1);
</PRE><TR valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Display the original in a scrolling window
imagePanel1 = new ScrollingImagePanel(im2, 100, 100);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="4"
colspan="1"><PRE> // Add the display widget to our frame.
add(imagePanel1);
}
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top">
<HR>
<P>The first three lines of the example code specify which classes to import.
The classes prefixed with <CODE>javax.jai</CODE> are the Java Advanced Imaging
classes. The <CODE>java.awt</CODE> prefix specifies the core Java API classes.
<P><PRE> import javax.jai.*;
import javax.jai.widget.*;
import java.awt.Frame;
</PRE>The next line declares the name of the program and that it runs in a
<CODE>Frame</CODE>, a window with a title and border.
<P><PRE> public class AddExample extends Frame {
</PRE>The next line of code creates a <CODE>ScrollingImagePanel</CODE>, which
is the ultimate destination of our image:
<P><PRE> ScrollingImagePanel imagePanel1;
</PRE>Next, a <CODE>ParameterBlock</CODE> for each source image is defined.
The parameters specify the image height, width, origin, tile size, and so on.
<P><PRE> public AddExample(ParameterBlock param1,
ParameterBlock param2) {
</PRE>The next two lines define two operations that create the two "constant"
images that will be added together to create the destination image (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Acquisition.doc.html#72170">Section
4.7, "Creating a Constant Image</A>").
<P><PRE> RenderedOp im0 = JAI.create("constant", param1);
RenderedOp im1 = JAI.create("constant", param2);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -