Java generates thumbnails

Source: Internet
Author: User

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 ();
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.