📄 writing image files.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0091)http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html -->
<HTML><HEAD><TITLE>Writing Image Files</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content=exclude 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="Writing Image Files.files/contents.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Client-server.doc.html"><IMG
alt=Previous src="Writing Image Files.files/previous.gif"></A> <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Extension.doc.html"><IMG
alt=Next src="Writing Image Files.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="Writing Image Files.files/sm-space.gif">13</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>Writing Image Files</FONT></TD></TR></TBODY></TABLE></A></CENTER>
<BLOCKQUOTE>
<P><BR><BR><BR>
<P><FONT size=7><B>T</B></FONT>HIS chapter describes JAI's codec system for
writing image data files.
<P><A name=57309>
<H2>13.1 <IMG
src="Writing Image Files.files/space.gif">Introduction</H2></A>The JAI codec
system supports a variety of image formats for writing an image to a file or
to an <CODE>OutputStream</CODE> for further manipulation. For writing an image
to a file, the <CODE>FileStore</CODE> operation (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56452">Section
13.2, "Writing to a File</A>") writes an image to a specified file in the
specified format. For encoding an image to an <CODE>OutputStream</CODE>, the
<CODE>Encode</CODE> operation (see <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56483">Section
13.3, "Writing to an Output Stream</A>") writes an image to a given
<CODE>OutputStream</CODE> in a specified format using the encoding parameters
supplied via the <CODE>ImageEncodeParam</CODE> operation parameter.
<P><A name=56452>
<H2>13.2 <IMG src="Writing Image Files.files/space.gif">Writing to a
File</H2></A>The <CODE>FileStore</CODE> operation writes an image to a given
file in a specified format using the specified encoding parameters. This
operation is much simpler than the encoders described in the remainder of this
chapter.
<P>The <CODE>FileStore</CODE> operation takes one rendered source image and
three parameters:
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=56518>Parameter </A>
<TH><A name=56520>Type </A>
<TH><A name=56522>Description </A>
<TR vAlign=top>
<TD><A name=56500>filename</A><BR>
<TD><A name=56502>String</A><BR>
<TD><A name=56504>The path of the file to write to.</A><BR>
<TR vAlign=top>
<TD><A name=56506>format</A><BR>
<TD><A name=56508>String</A><BR>
<TD><A name=56510>The format of the file.</A><BR>
<TR vAlign=top>
<TD><A name=57052>param</A><BR>
<TD><A name=57054>ImageEncodeParam</A><BR>
<TD><A name=57056>The encoding parameters.</A><BR></TR></TBODY></TABLE>
<P>The <CODE>filename</CODE> parameter must be supplied or the operation will
not be performed. Also, the specified file path must be writable.
<P>The <CODE>format</CODE> parameter defaults to <CODE>tiff</CODE> if no value
is provided. <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56610">Table
13-1</A> lists the recognized JAI file formats.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B><A name=56610><I>Table 13-1 </I><IMG
src="Writing Image Files.files/sm-blank.gif" border=0> JAI Writable File
Formats </A></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=56614>File Format </A>
<TH><A name=56616>Description </A>
<TR vAlign=top>
<TD><A name=56618>BMP</A><BR>
<TD><A name=56620>Microsoft Windows bitmap image file</A><BR>
<TR vAlign=top>
<TD><A name=56622>JPEG</A><BR>
<TD><A name=56648>A file format developed by the Joint Photographic
Experts Group</A><BR>
<TR vAlign=top>
<TD><A name=56626>PNG</A><BR>
<TD><A name=56628>Portable Network Graphics</A><BR>
<TR vAlign=top>
<TD><A name=56630>PNM</A><BR>
<TD><A name=56632>Portable aNy Map file format. Includes PBM, PGM, and
PPM</A><BR>
<TR vAlign=top>
<TD><A name=56634>TIFF</A><BR>
<TD><A name=56636>Tag Image File Format</A><BR></TR></TBODY></TABLE>
<P>The <CODE>param</CODE> parameter must either be null or an instance of an
<CODE>ImageEncodeParam</CODE> subclass appropriate to the format.
<P><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#58219">Listing
13-1</A> shows a code sample demonstrating the use of both the
<CODE>Encode</CODE> and <CODE>FileStore</CODE> operations.
<P><A name=56483>
<H2>13.3 <IMG src="Writing Image Files.files/space.gif">Writing to an Output
Stream</H2></A>The <CODE>Encode</CODE> operation writes an image to a given
<CODE>OutputStream</CODE> in a specified format using the encoding parameters
supplied via the <CODE>ImageEncodeParam</CODE> operation parameter.
<P>The <CODE>Encode</CODE> operation takes one rendered source image and three
parameters:
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=51210>Parameter </A>
<TH><A name=51212>Type </A>
<TH><A name=51214>Description </A>
<TR vAlign=top>
<TD><A name=51222>stream</A><BR>
<TD><A name=51224>OutputStream</A><BR>
<TD><A name=51226>The OutputStream to write to.</A><BR>
<TR vAlign=top>
<TD><A name=57025>format</A><BR>
<TD><A name=57027>String</A><BR>
<TD><A name=57029>The format of the created file.</A><BR>
<TR vAlign=top>
<TD><A name=51228>param</A><BR>
<TD><A name=51230>ImageEncodeParam</A><BR>
<TD><A name=51232>The encoding parameters.</A><BR></TR></TBODY></TABLE>
<P>The <CODE>param</CODE> parameter must either be null or an instance of an
<CODE>ImageEncodeParam</CODE> subclass appropriate to the specified image
format. The image encode parameter depends on the type of image file to be
encoded. This parameter contains all of the information about the file type
that the encoder needs to create the file. For example, the BMP format
requires two parameter values, as described in the <CODE>BMPEncodeParam</CODE>
class:
<P>
<UL>
<LI>Version number - One of three values: <CODE>VERSION_2</CODE>,
<CODE>VERSION_3</CODE>, or <CODE>VERSION_4</CODE>.
<P></P></LI></UL>
<UL>
<LI>Data layout - One of two values: <CODE>TOP_DOWN</CODE> or
<CODE>BOTTOM_UP</CODE>.
<P></P></LI></UL>These parameters are described in detail in <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#51259">Section
13.4, "Writing BMP Image Files</A>."
<P><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#58219">Listing
13-1</A> shows a code sample demonstrating the use of both the
<CODE>Encode</CODE> and <CODE>FileStore</CODE> operations.
<P><CAPTION><FONT size=-1><B><A name=58219>
<CENTER><FONT size=-1><B><I>Listing 13-1 </I><IMG
src="Writing Image Files.files/sm-blank.gif" border=0> Writing an OutputStream
and a File</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> // Define the source and destination file names.
String inputFile = /images/FarmHouse.tif
String outputFile = /images/FarmHouse.bmp
</PRE><TR valign="top"><TD><PRE> // Load the input image.
RenderedOp src = JAI.create("fileload", inputFile);
</PRE><TR valign="top"><TD><PRE> // Encode the file as a BMP image.
FileOutputStream stream =
new FileOutputStream(outputFile);
JAI.create("encode", src, stream, BMP, null);
</PRE><TR valign="top"><TD><PRE> // Store the image in the BMP format.
JAI.create("filestore", src, outputFile, BMP, null);
</PRE>
<HR>
<P><A name=51259>
<H2>13.4 <IMG src="Writing Image Files.files/space.gif">Writing BMP Image
Files</H2></A>As described above, the encoding of BMP images requires the
specification of two parameters: version and data layout. By default, these
values are:
<P>
<UL>
<LI>Version - VERSION_3
<P></P></LI></UL>
<UL>
<LI>Data layout - pixels are stored in bottom-up order
<P></P></LI></UL>The JAI BMP encoder does not support compression of BMP image
files.
<P><A name=57416>
<H3>13.4.1 <IMG src="Writing Image Files.files/space.gif">BMP
Version</H3></A>JAI currently reads and writes Version2, Version3, and some of
the Version 4 images. The BMP version number is read and specified with
<CODE>getVersion</CODE> and <CODE>setVersion</CODE> methods in the
<CODE>BMPEncodeParam</CODE> class. The BMP version parameters are as follows:
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=51265>Parameter </A>
<TH><A name=51267>Description </A>
<TR vAlign=top>
<TD><A name=51269>VERSION_2</A><BR>
<TD><A name=51271>Specifies BMP Version 2</A><BR>
<TR vAlign=top>
<TD><A name=51273>VERSION_3</A><BR>
<TD><A name=51275>Specifies BMP Version 3</A><BR>
<TR vAlign=top>
<TD><A name=51277>VERSION_4</A><BR>
<TD><A name=51279>Specifies BMP Version 4</A><BR></TR></TBODY></TABLE>
<P>If not specifically set, <CODE>VERSION_3</CODE> is the default version.
<P>
<TABLE border=0>
<TBODY>
<TR>
<TD><IMG src="Writing Image Files.files/cistine.gif"></TD>
<TD>
<HR>
<B>API:</B> <CODE>com.sun.media.jai.codec.BMPEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setVersion(int versionNumber)
<P></P></LI></UL></PRE>
<DL><A name=56243>
<DT>
<DD>sets the BMP version to be used. </A>
<P></P></DD></DL><PRE><UL>
<LI>int getVersion()
<P></P></LI></UL></PRE>
<DL><A name=57353>
<DT>
<DD>returns the BMP version to be used. </A>
<P></P></DD></DL><A name=56260>
<H3>13.4.2 <IMG src="Writing Image Files.files/space.gif">BMP Data
Layout</H3></A>The scan lines in the BMP bitmap are stored from the bottom up.
This means that the first byte in the array represents the pixels in the
lower-left corner of the bitmap, and the last byte represents the pixels in
the upper-right corner.
<P>The in-memory layout of the image data to be encoded is specified with
<CODE>getDataLayout</CODE> and <CODE>setDataLayout</CODE> methods in the
<CODE>BMPEncodeParam</CODE> class.
<P>
<TABLE border=0>
<TBODY>
<TR>
<TD><IMG src="Writing Image Files.files/cistine.gif"></TD>
<TD>
<HR>
<B>API:</B> <CODE>com.sun.media.jai.codec.BMPEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setTopDown(boolean topDown)
<P></P></LI></UL></PRE>
<DL><A name=56290>
<DT>
<DD>sets the data layout to be top down. </A>
<P></P></DD></DL><A name=56293>
<H3>13.4.3 <IMG src="Writing Image Files.files/space.gif">Example
Code</H3></A><A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#56298">Listing
13-2</A> shows a code sample for encoding a BMP image.
<P><CAPTION><FONT size=-1><B><A name=56298>
<CENTER><FONT size=-1><B><I>Listing 13-2 </I><IMG
src="Writing Image Files.files/sm-blank.gif" border=0> Encoding a BMP
Image</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD><PRE> OutputStream os = new FileOutputStream(fileToWriteTo);
BMPEncodeParam param = new BMPEncodeParam();
ImageEncoder enc = ImageCodec.createImageEncoder("BMP", os,
param);
enc.encode(op);
os.close();
</PRE>
<HR>
<P><A name=51358>
<H2>13.5 <IMG src="Writing Image Files.files/space.gif">Writing JPEG Image
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -