<tlibversion> 1.0 </tlibversion>
<jspversion> 1.1 </jspversion>
<tag>
<name>imagesizer</name>
<tagclass>tags.ImageSizerTag</tagclass>
<bodycontent>empty</bodycontent>
<attribute>
<name>src</name>
<required>required</required>
</attribute>
<attribute>
<name>alt</name>
</attribute>
<attribute>
<name>quality</name>
</attribute>
</tag>
</taglib>
public int doEndTag() throws JspException {图 3. 重要的 javax.servlet.jsp.tagext 类
// Move request data to session.
int outputSize = 0;
String sizeVal = request.getParameter( REQUESTSIZEKEY );
if ( sizeVal != null ) {
session.setAttribute( REQUESTSIZEKEY, sizeVal );
sizeVal = (String) session.getAttribute( REQUESTSIZEKEY );
if ( sizeVal != null ) {
outputSize = Integer.parseInt( sizeVal );
}
}
// Get specified image locally.
String contextPath = getContextPath( request );
Image image = Toolkit.getDefaultToolkit().getImage(contextPath + src );
ImageSizer.waitForImage( image );
int imageWidth = image.getWidth( null );
int imageHeight = image.getHeight( null );
if (( imageWidth > 0 ) && ( imageHeight > 0 )) {
if (( outputSize > 0 ) && ( outputSize != imageWidth )) {
// Convert image to new size.
Image outputImage = ImageSizer.setSize( image, outputSize, -1 );
ImageSizer.waitForImage( outputImage );
int outputWidth = outputImage.getWidth( null );
int outputHeight = outputImage.getHeight( null );
if ( outputWidth > 0 && outputHeight > 0 ) {
// Change image file name to xxxx.size.jpg
String originalSrc = src;
int lastDot = src.lastIndexOf( ′.′ );
if ( lastDot > -1 ) {
src = src.substring( 0, lastDot + 1 );
}
setSrc( src + outputSize + ".jpg" );
// Write new size image to JPEG file.
File file = new File( contextPath + src );
if ( !file.exists() ) {
out.println( "" );
FileOutputStream fos = new FileOutputStream( contextPath + src );
ImageSizer.encodeJPEG( fos, outputImage, quality );
fos.close( ) ;
}
imageWidth = outputWidth;
imageHeight = outputHeight;
}
} // if outputSize
} // if image found
// Produce output tag.
out.print( "<img src="" + src + """ );
// Add alt text, if any
if ((alt != null ) && ( alt.length() > 0 )) {
out.print( " alt="" + alt + """ );
}
// Add proper width, height.
out.print( " width="" + imageWidth + "" height="" +
imageHeight + """ );
out.println( ">" );
return EVAL_PAGE;
} // doEndTag
// quality to the output stream.那就是重新调整图像大小及保存图像所需的全部。
public static void encodeJPEG
( OutputStream outputStream,
Image outputImage, float outputQuality )
throws java.io.IOException {
// Get a buffered image from the image.
BufferedImage bi = new BufferedImage
( outputWidth, outputHeight,
BufferedImage.TYPE_INT_RGB );
Graphics2D biContext =
bi.createGraphics( );
biContext.drawImage
( outputImage, 0, 0, null );
// Additional drawing code, such as
// watermarks or logos can be placed here.
// com.sun.image.codec.jpeg package
// is included in Sun and IBM sdk 1.3.
JPEGImageEncoder encoder =
JPEGCodec.createJPEGEncoder
( outputStream );
// The default quality is 0.75.
JPEGEncodeParam jep =
JPEGCodec.getDefaultJPEGEncodeParam
( bi );
jep.setQuality( outputQuality, true );
encoder.encode( bi, jep );
outputStream.flush();
} // encodeImage