Package com. hunuo. Common;
Import java. AWT. image;
Import java. AWT. image. bufferedimage;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import javax. ImageIO. ImageIO;
Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. encode imageencoder;
Public class imageutil {
/**
* Generating thumbnails
* @ Param srcimagefile the file instance file of the source image file = new file ("file name ");
* @ Param dstimagefilename complete path of the thumbnail to be generated (in the format of image/JPEG );
* @ Throws exception
*/
Public static void makesmallimage (File srcimagefile, string dstimagefilename) throws exception {
Fileoutputstream = NULL;
Required imageencoder encoder = NULL;
Bufferedimage tagimage = NULL;
Image srcimage = NULL;
Try {
Srcimage = ImageIO. Read (srcimagefile );
Int srcwidth = srcimage. getwidth (null); // Original Image Width
Int srcheight = srcimage. getheight (null); // Original Image Height
Int dstmaxsize = 120; // The maximum width/height of the target thumbnail. The width and height are abbreviated proportionally.
Int dstwidth = srcwidth; // The width of the thumbnail.
Int dstheight = srcheight; // The height of the thumbnail.
Float scale = 0;
// Calculate the width and height of the thumbnail.
If (srcwidth> dstmaxsize ){
Dstwidth = dstmaxsize;
Scale = (float) srcwidth/(float) dstmaxsize;
Dstheight = math. Round (float) srcheight/scale );
}
Srcheight = dstheight;
If (srcheight> dstmaxsize ){
Dstheight = dstmaxsize;
Scale = (float) srcheight/(float) dstmaxsize;
Dstwidth = math. Round (float) dstwidth/scale );
}
// Generate a thumbnail
Tagimage = new bufferedimage (dstwidth, dstheight, bufferedimage. type_int_rgb );
Tagimage. getgraphics (). drawimage (srcimage, 0, 0, dstwidth, dstheight, null );
Fileoutputstream = new fileoutputstream (dstimagefilename );
Encoder = export codec. createjpegencoder (fileoutputstream );
Encoder. encode (tagimage );
Fileoutputstream. Close ();
Fileoutputstream = NULL;
} Finally {
If (fileoutputstream! = NULL ){
Try {
Fileoutputstream. Close ();
} Catch (exception e ){
}
Fileoutputstream = NULL;
}
Encoder = NULL;
Tagimage = NULL;
Srcimage = NULL;
System. GC ();
}
}
}