8. Create Image thumbnails in Java
Public void createthumbnail (string filename, int thumbwidth, <br/> int thumbheight, int quality, string outfilename) <br/> throws interruptedexception, filenotfoundexception, ioexception {<br/> // load image from filename <br/> image = toolkit. getdefatooltoolkit (). getimage (filename); <br/> mediatracker = new mediatracker (new container (); <br/> mediatracker. addimage (image, 0); <br/> mediatracker. waitforid (0); <br/> // use this to test for errors at this point: <br/> // system. out. println (mediatracker. iserrorany (); <br/> // determine thumbnail size from width and height <br/> double thumbratio = (double) thumbwidth/(double) thumbheight; <br/> int imagewidth = image. getwidth (null); <br/> int imageheight = image. getheight (null); <br/> double imageratio = (double) imagewidth/(double) imageheight; <br/> If (thumbratio <imageratio) {<br/> thumbheight = (INT) (thumbwidth/imageratio); <br/>}else {<br/> thumbwidth = (INT) (thumbheight * imageratio ); <br/>}</P> <p> // draw original image to thumbnail image object and <br/> // scale it to the new size on-the-fly <br/> bufferedimage thumbimage = new bufferedimage (thumbwidth, thumbheight, <br/> bufferedimage. type_int_rgb); <br/> graphics2d graphics2d = thumbimage. creategraphics (); <br/> graphics2d. setrenderinghint (renderinghints. key_interpolation, <br/> renderinghints. value_interpolation_bilinear); <br/> graphics2d. drawimage (image, 0, 0, thumbwidth, thumbheight, null ); </P> <p> // save thumbnail image to outfilename <br/> bufferedoutputstream out = new bufferedoutputstream (<br/> New fileoutputstream (outfilename )); <br/> required imageencoder encoder = required codec. createjpegencoder (out); <br/> export encodeparam Param = encoder. getdefajpegincluencodeparam (thumbimage); <br/> Quality = math. max (0, math. min (quality, 100); <br/> param. setquality (float) Quality/10010000f, false); <br/> encoder. setincluencodeparam (PARAM); <br/> encoder. encode (thumbimage); <br/> out. close (); <br/>}