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