Generate thumbnails for image files

Source: Internet
Author: User
Package imagetest; import Java. AWT. graphics2d; import Java. AWT. renderinghints; import Java. AWT. geom. affinetransform; import Java. AWT. image. bufferedimage; import Java. AWT. image. colormodel; import Java. AWT. image. writableraster; import Java. io. file; import Java. io. ioexception; import javax. imageIO. imageIO; public class test {/*** generate a thumbnail for the image file * @ Param ARGs */public static void main (string [] ARGs) {try {// parameter 1 (source image path), parameter 2 (thumbnail path), parameter 3 (thumbnail width) parameter 4 (thumbnail height) test. saveimageasjpg ("E: \ qq20120612151094.gif", "E: \ test.jpg", 400,400);} catch (ioexception e) {e. printstacktrace ();}} /*** generate a thumbnail ** @ Param fromfilestr source image path * @ Param savetofilestr thumbnail path * @ Param width * @ Param hight height of the thumbnail * @ throws ioexception */Public static void saveimageasjpg (string fromfilestr, string savetofilestr, int width, int hight) throws ioexception {bufferedimage srcimage; string imgtype = "Jpeg"; if (fromfilestr. tolowercase (). endswith (". PNG ") {imgtype =" PNG ";} file SaveFile = new file (savetofilestr); file fromfile = new file (fromfilestr); srcimage = ImageIO. read (fromfile); If (width> 0 | hight> 0) {srcimage = resize (srcimage, width, hight);} ImageIO. write (srcimage, imgtype, SaveFile );} /*** generate a thumbnail for the bufferedimage object of the source image * @ Param source the bufferedimage object of the source image * @ Param targetw the width of the thumbnail * @ Param targeth the height of the thumbnail * @ return */ private Static bufferedimage resize (bufferedimage source, int targetw, int targeth) {int type = source. getType (); bufferedimage target = NULL; double SX = (double) targetw/source. getwidth (); double Sy = (double) targeth/source. getheight (); If (SX> Sy) {SX = sy; targetw = (INT) (SX * Source. getwidth ();} else {Sy = SX; targeth = (INT) (SY * Source. getheight ();} If (type = bufferedimage. type_custom) {colormodel CM = source. getcolormodel (); writableraster raster = cm. createcompatiblewritableraster (targetw, targeth); Boolean alphapremultiplied = cm. isalphapremultiplied (); target = new bufferedimage (CM, raster, alphapremultiplied, null);} else {target = new bufferedimage (targetw, targeth, type);} graphics2d G = target. creategraphics (); G. setrenderinghint (renderinghints. key_rendering, renderinghints. value_render_quality); G. drawrenderedimage (source, affinetransform. getscaleinstance (sx, Sy); G. dispose (); Return target ;}}

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.