📄 writing image files.htm
字号:
sampling factors for YCC input images are set to {1, 2, 2} for both horizontal
and vertical axes.
<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.JPEGEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setHorizontalSubsampling(int component, int subsample)
<P></P></LI></UL></PRE>
<DL><A name=51559>
<DT>
<DD>sets the horizontal subsampling to be applied to an image band. Defaults
to 1 for grayscale and (1,2,2) for RGB.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD rowSpan=2><EM>Parameter</EM>:<EM></EM>
<P></P>
<TD><CODE>component</CODE>
<P></P>
<TD>The band for which to set horizontal subsampling.
<P></P>
<TR vAlign=top>
<TD><CODE>subsample</CODE>
<P></P>
<TD>The horizontal subsampling factor.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><PRE><UL>
<LI>void setVerticalSubsampling(int component, int subsample)
<P></P></LI></UL></PRE>
<DL><A name=51561>
<DT>
<DD>sets the vertical subsampling to be applied to an image band. Defaults
to 1 for grayscale and (1,2,2) for RGB. </A>
<P></P></DD></DL><PRE><UL>
<LI>int getHorizontalSubsampling(int component)
<P></P></LI></UL></PRE>
<DL><A name=51563>
<DT>
<DD>returns the horizontal subsampling factor for a band. </A>
<P></P></DD></DL><PRE><UL>
<LI>int getVerticalSubsampling(int component)
<P></P></LI></UL></PRE>
<DL><A name=51565>
<DT>
<DD>returns the vertical subsampling factor for a band. </A>
<P></P></DD></DL><A name=51700>
<H3>13.5.5 <IMG src="Writing Image Files.files/space.gif">Compression
Quality</H3></A>Compression quality specifies a factor that relates to the
desired tradeoff between image quality and the image data compression ratio.
The quality value is a <CODE>float</CODE> between 0.0 and 1.0. A setting of
1.0 produces the highest quality image at a lower compression ratio. A setting
of 0.0 produces the highest compression ratio, with a sacrifice to image
quality. The quality value is typically set to 0.75.
<P>The compression quality value controls image quality and compression ratio
by determining a scale factor the encoder will use in creating scaled versions
of the quantization tables. Some guidelines:
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TH><A name=56086>Quality Value </A>
<TH><A name=56088>Meaning </A>
<TR vAlign=top>
<TD><A name=56104>1.0</A><BR>
<TD><A name=56106>Highest quality, no compression</A><BR>
<TR vAlign=top>
<TD><A name=56090>0.75</A><BR>
<TD><A name=56092>High quality, good compression ratio</A><BR>
<TR vAlign=top>
<TD><A name=56094>0.5</A><BR>
<TD><A name=56096>Medium quality, medium compression ratio</A><BR>
<TR vAlign=top>
<TD><A name=56098>0.25</A><BR>
<TD><A name=56100>Low quality, high compression
ratio</A><BR></TR></TBODY></TABLE>
<P>
<HR noShade>
<B>Note:</B> The values stored in the quantization table also affect image
quality and compression ratio. See also <A
href="http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.html#51433">Section
13.5.3, "Quantization Table</A>."
<HR noShade>
<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.JPEGEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setQuality(float quality)
<P></P></LI></UL></PRE>
<DL><A name=51710>
<DT>
<DD>sets the compression quality factor. Creates new quantization tables
that replace the currently-installed quantization tables.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD><EM>Parameter</EM>:<EM></EM>
<P></P>
<TD><CODE>quality</CODE>
<P></P>
<TD>The desired quality level; a value of 0.0 to 1.0. The default
value is 0.75.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><PRE><UL>
<LI>float getQuality()
<P></P></LI></UL></PRE>
<DL><A name=56753>
<DT>
<DD>returns the quality setting for this encoding. This is a number between
0.0 and 1.0. </A>
<P></P></DD></DL><PRE><UL>
<LI>boolean isQualitySet()
<P></P></LI></UL></PRE>
<DL><A name=57956>
<DT>
<DD>tests if the quality parameter has been set in this
<CODE>JPEGEncodeParam</CODE>. </A>
<P></P></DD></DL><A name=57621>
<H3>13.5.6 <IMG src="Writing Image Files.files/space.gif">Restart
Interval</H3></A>JPEG images use restart markers to define multiple strips or
tiles. The restart markers are inserted periodically into the image data to
delineate image segments known as <EM>restart intervals</EM>. To limit the
effect of bitstream errors to a single restart interval, JAI provides methods
to set the restart interval in JPEG Minimum Coded Units (MCUs). The default is
zero (no restart interval markers).
<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.JPEGEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setRestartInterval(int restartInterval)
<P></P></LI></UL></PRE>
<DL><A name=57666>
<DT>
<DD>sets the restart interval in Minimum Coded Units (MCUs).
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD><EM>Parameter</EM>:<EM></EM>
<P></P>
<TD><CODE>restartInterval</CODE>
<P></P>
<TD>Number of MCUs between restart markers.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><PRE><UL>
<LI>int getRestartInterval()
<P></P></LI></UL></PRE>
<DL><A name=57676>
<DT>
<DD>returns the restart interval. </A>
<P></P></DD></DL><A name=56121>
<H3>13.5.7 <IMG src="Writing Image Files.files/space.gif">Writing an
Abbreviated JPEG Stream</H3></A>Normally, both the JPEG table data and
compressed (or uncompressed) image data is written to the output stream.
However, it is possible to write just the table data or just the image data.
The <CODE>setWriteTablesOnly</CODE> method instructs the encoder to write only
the table data to the output stream. The <CODE>setWriteImageOnly</CODE> method
instructs the encoder to write only the compressed image data to the output
stream.
<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.JPEGEncodeParam </CODE>
<HR>
</TD></TR></TBODY></TABLE><PRE><UL>
<LI>void setWriteTablesOnly(boolean tablesOnly)
<P></P></LI></UL></PRE>
<DL><A name=56152>
<DT>
<DD>instructs the encoder to write only the table data to the output stream.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD><EM>Parameter</EM>:<EM></EM>
<P></P>
<TD><CODE>tablesOnly</CODE>
<P></P>
<TD>If true, only the tables will be written.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><PRE><UL>
<LI>void setWriteImageOnly(boolean imageOnly)
<P></P></LI></UL></PRE>
<DL><A name=56177>
<DT>
<DD>instructs the encoder to write only the image data to the output stream.
<P>
<TABLE cellPadding=3 border=3>
<CAPTION><FONT size=-1><B></B></FONT></CAPTION>
<TBODY>
<TR vAlign=top>
<TD><EM>Parameter</EM>:<EM></EM>
<P></P>
<TD><CODE>imageOnly</CODE>
<P></P>
<TD>If true, only the compressed image will be written.
<P></P></TR></TBODY></TABLE></A>
<P></P></DD></DL><A name=54656>
<H3>13.5.8 <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#55335">Listing
13-3</A> shows a code sample for encoding a JPEG image.
<P><CAPTION><FONT size=-1><B><A name=55335>
<CENTER><FONT size=-1><B><I>Listing 13-3 </I><IMG
src="Writing Image Files.files/sm-blank.gif" border=0> Encoding a JPEG
Image</B></FONT></CENTER></A>
<P></B></FONT></CAPTION>
<HR>
<TR valign="top"><TD rowspan="9" colspan="1"><PRE> import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import java.io.*;
import javax.media.jai.*;
import javax.media.jai.widget.*;
import com.sun.media.jai.codec.*;
</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="7" colspan="1"><PRE> public class JPEGWriterTest extends WindowContainer {
</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> private ImageEncoder encoder = null;
private JPEGEncodeParam encodeParam = null;
</PRE><TR valign="top"><TR valign="top"><TR valign="top"><TD rowspan="11"
colspan="1"><PRE> // Create some Quantization tables.
private static int[] qtable1 = {
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,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"><TD rowspan="11" colspan="1"><PRE> private static int[] qtable2 = {
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2
};
</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"><TD rowspan="11" colspan="1"><PRE> private static int[] qtable3 = {
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -