📄 program examples.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0093)http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Examples.doc.html -->
<HTML><HEAD><TITLE>Program Examples</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="Program Examples.files/contents.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Extension.doc.html"><IMG
alt=Previous src="Program Examples.files/previous.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/API-summary.doc.html"><IMG
alt=Next src="Program Examples.files/next.gif"></A>
<P><FONT size=5><I>Programming in Java Advanced Imaging</I></FONT> </CENTER><BR>
<CENTER><A name=72445>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right><FONT size=3>A P P E N D I X <IMG
src="Program Examples.files/sm-space.gif"></FONT><FONT
size=7>A</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<CENTER><A name=72446>
<TABLE width="90%" border=0>
<TBODY>
<TR>
<TD align=right>
<HR noShade SIZE=7>
<FONT size=6>Program Examples</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<BLOCKQUOTE>
<P><BR><BR><BR>
<P><FONT size=7><B>T</B></FONT>HIS appendix contains fully-operational JAI
program examples.
<P>The examples in this appendix are provided to demonstrate how to create
simple programs using JAI. Although these examples can be compiled and run,
they are not intended to be used that way since they are pretty simple and
would not be particularly interesting, visually.
<P><A name=72767>
<H2>A.1 <IMG src="Program Examples.files/space.gif">Lookup Operation
Example</H2></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Examples.doc.html#73865">Listing
A-1</A> shows an example of the <CODE>Lookup</CODE> operation. This example
program decodes a TIFF image file into a <CODE>RenderedImage</CODE>. If the
TIFF image is an unsigned short type image, the program performs a
<CODE>Lookup</CODE> operation to convert the image into a byte type image.
Finally, the program displays the byte image.
<P><CAPTION><FONT size=-1><B><A name=73865>
<CENTER><FONT size=-1><B><I>Listing A-1 </I><IMG
src="Program Examples.files/sm-blank.gif" border=0> <STRONG>Example Lookup
Program</STRONG></B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="13" colspan="1"><PRE> import java.awt.Frame;
import java.awt.RenderingHints;
import java.awt.image.DataBuffer;
import java.awt.image.renderable.ParameterBlock;
import java.io.IOException;
import javax.media.jai.JAI;
import javax.media.jai.LookupTableJAI;
import javax.media.jai.RenderedOp;
import com.sun.media.jai.codec.FileSeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import javax.media.jai.widget.ScrollingImagePanel;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD><PRE> public class LookupSampleProgram {
</PRE><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // The main method.
public static void main(String[] args) {
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="7"
colspan="1"><PRE> // Validate input.
if (args.length != 1) {
System.out.println("Usage: java LookupSampleProgram " +
"TIFF_image_filename");
System.exit(-1);
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="12" colspan="1"><PRE> // Create an input stream from the specified file name to be
// used with the TIFF decoder.
FileSeekableStream stream = null;
try {
stream = new FileSeekableStream(args[0]);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="7" colspan="1"><PRE> // Store the input stream in a ParameterBlock to be sent to
// the operation registry, and eventually to the TIFF
// decoder.
ParameterBlock params = new ParameterBlock();
params.add(stream);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="7" colspan="1"><PRE> // Specify to TIFF decoder to decode images as they are and
// not to convert unsigned short images to byte images.
TIFFDecodeParam decodeParam = new TIFFDecodeParam();
decodeParam.setDecodePaletteAsShorts(true);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Create an operator to decode the TIFF file.
RenderedOp image1 = JAI.create("tiff", params);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="9"
colspan="1"><PRE> // Find out the first image's data type.
int dataType = image1.getSampleModel().getDataType();
RenderedOp image2 = null;
if (dataType == DataBuffer.TYPE_BYTE) {
// Display the byte image as it is.
System.out.println("TIFF image is type byte.");
image2 = image1;
} else if (dataType == DataBuffer.TYPE_USHORT) {
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="2" colspan="1"><PRE> // Convert the unsigned short image to byte image.
System.out.println("TIFF image is type ushort.");
</PRE><TR valign="top"><TR valign="top"><TD rowspan="7" colspan="1"><PRE> // Setup a standard window-level lookup table. */
byte[] tableData = new byte[0x10000];
for (int i = 0; i < 0x10000; i++) {
tableData[i] = (byte)(i >> 8);
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="6" colspan="1"><PRE> // Create a LookupTableJAI object to be used with the
// "lookup" operator.
LookupTableJAI table = new LookupTableJAI(tableData);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TD rowspan="3" colspan="1"><PRE> // Create an operator to lookup image1.
image2 = JAI.create("lookup", image1, table);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="6"
colspan="1"><PRE> } else {
System.out.println("TIFF image is type " + dataType +
", and will not be displayed.");
System.exit(0);
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TD rowspan="4" colspan="1"><PRE> // Get the width and height of image2.
int width = image2.getWidth();
int height = image2.getHeight();
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="4" colspan="1"><PRE> // Attach image2 to a scrolling panel to be displayed.
ScrollingImagePanel panel = new ScrollingImagePanel(
image2, width, height);
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="7" colspan="1"><PRE> // Create a frame to contain the panel.
Frame window = new Frame("Lookup Sample Program");
window.add(panel);
window.pack();
window.show();
}
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top">
<HR>
<P><A name=74136>
<H2>A.2 <IMG src="Program Examples.files/space.gif">Adding an
OperationDescriptor Example</H2></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Extension.doc.html#47285">Chapter
14, "Extending the API</A>," describes how to extend the API by writing custom
OperationDescriptors. <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Examples.doc.html#74163">Listing
A-2</A> shows the construction of an <CODE>OperationDescriptor</CODE>, called
<CODE>SampleDescriptor</CODE>, that is both an
<CODE>OperationDescriptor</CODE> and a <CODE>RenderedImageFactory</CODE>. The
operation created here is called <CODE>Sample</CODE> and takes two parameters
for the operation.
<P><CAPTION><FONT size=-1><B><A name=74163>
<CENTER><FONT size=-1><B><I>Listing A-2 </I><IMG
src="Program Examples.files/sm-blank.gif" border=0> Example
OperationDescriptor</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="16" colspan="1"><PRE> import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.ComponentSampleModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.Raster;
import java.awt.image.RenderedImage;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import java.awt.image.renderable.ParameterBlock;
import java.awt.image.renderable.RenderedImageFactory;
import javax.media.jai.ImageLayout;
import javax.media.jai.OperationDescriptorImpl;
import javax.media.jai.OpImage;
import javax.media.jai.PointOpImage;
import javax.media.jai.RasterAccessor;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="2" colspan="1"><PRE> // A single class that is both an OperationDescriptor and
// a RenderedImageFactory along with the one OpImage it is
// capable of creating. The operation implemented is a variation
// on threshold, although the code may be used as a template for
// a variety of other point operations.
public class SampleDescriptor extends OperationDescriptorImpl
implements RenderedImageFactory {
</PRE><TR valign="top"><TR valign="top"><TD rowspan="15" colspan="1"><PRE> // The resource strings that provide the general documentation
// and specify the parameter list for the "Sample" operation.
private static final String[][] resources = {
{"GlobalName", "Sample"},
{"LocalName", "Sample"},
{"Vendor", "com.mycompany"},
{"Description", "A sample operation that thresholds source
pixels"},
{"DocURL", "http://www.mycompany.com/
SampleDescriptor.html"},
{"Version", "1.0"},
{"arg0Desc", "param1"},
{"arg1Desc", "param2"}
};
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="8" colspan="1"><PRE> // The parameter names for the "Sample" operation. Extenders may
// want to rename them to something more meaningful.
private static final String[] paramNames = {
"param1", "param2"
};
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="9" colspan="1"><PRE> // The class types for the parameters of the "Sample" operation.
// User defined classes can be used here as long as the fully
// qualified name is used and the classes can be loaded.
private static final Class[] paramClasses = {
java.lang.Integer.class, java.lang.Integer.class
};
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="8" colspan="1"><PRE> // The default parameter values for the "Sample" operation
// when using a ParameterBlockJAI.
private static final Object[] paramDefaults = {
new Integer(0), new Integer(255)
};
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TD
rowspan="5" colspan="1"><PRE> // Constructor.
public SampleDescriptor() {
super(resources, 1, paramClasses, paramNames, paramDefaults);
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TD rowspan="15" colspan="1"><PRE> // Creates a SampleOpImage with the given ParameterBlock if the
// SampleOpImage can handle the particular ParameterBlock.
public RenderedImage create(ParameterBlock paramBlock,
RenderingHints renderHints) {
if (!validateParameters(paramBlock)) {
return null;
}
return new SampleOpImage(paramBlock.getRenderedSource(0),
new ImageLayout(),
(Integer)paramBlock.getObjectParameter(0),
(Integer)paramBlock.getObjectParameter(1));
}
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TR valign="top"><TR
valign="top"><TR valign="top"><TR valign="top"><TD rowspan="18" colspan="1"><PRE> // Checks that all parameters in the ParameterBlock have the
// correct type before constructing the SampleOpImage
public boolean validateParameters(ParameterBlock paramBlock) {
for (int i = 0; i < this.getNumParameters(); i++) {
Object arg = paramBlock.getObjectParameter(i);
if (arg == null) {
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -