資源描述:
《任意格式圖片生成縮略圖》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、該文檔所編寫的JAVA類,用于生成各種格式圖片的縮略圖。importjava.awt.Image;importjava.awt.Graphics2D;importjava.awt.geom.AffineTransform;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.io.OutputStream;importjava.io.FileOutputStream;importjavax.swing.ImageIcon;importcom.sun.imag
2、e.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;importjava.io.FileNotFoundException;importjava.awt.Toolkit;importjava.io.BufferedOutputStream;importcom.sun.image.codec.jpeg.JPEGEncodeParam;importjava.awt.image.MemoryImageSource;importjava.io.FileInputSt
3、ream;importcn.js.fan.util.StrUtil;publicclassThumbnail{/***Readsanimageinafileandcreates*athumbnailinanotherfile.*@paramorigThenameofimagefile.*@paramthumbThenameofthumbnailfile.*Willbecreatedifnecessary.*@parammaxDimThewidthandheightof*thethumbnailmust*bemaxDimpixelsorless.*
4、/publicstaticvoidcreateThumbnail(StringsrcPath,StringthumbPath,intmaxDim){try{//Gettheimagefromafile.if(srcPath.toLowerCase().endsWith(".bmp")){inImage=getBMPImage(srcPath);}else{inImage=newImageIcon(srcPath).getImage();}//Determinethescale.doublescale=(double)maxDim/(double)
5、inImage.getHeight(null);if(inImage.getWidth(null)>inImage.getHeight(null)){scale=(double)maxDim/(double)inImage.getWidth(null);}//Determinesizeofnewimage.//Oneofthem//shouldequalmaxDim.intscaledW=(int)(scale*inImage.getWidth(null));intscaledH=(int)(scale*inImage.getHeight(nul
6、l));//Createanimagebufferin//whichtopainton.BufferedImageoutImage=newBufferedImage(scaledW,scaledH,BufferedImage.TYPE_INT_RGB);//Setthescale.AffineTransformtx=newAffineTransform();//Iftheimageissmallerthan//thedesiredimagesize,//don'tbotherscaling.if(scale<1.0d){tx.scale(scal
7、e,scale);}//Paintimage.Graphics2Dg2d=outImage.createGraphics();g2d.drawImage(inImage,tx,null);g2d.dispose();//JPEG-encodetheimage//andwritetofile.OutputStreamos=newFileOutputStream(thumbPath);JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(os);encoder.encode(outImage);os.
8、close();}catch(IOExceptione){e.printStackTrace();}}/***根據(jù)限定的寬度生成縮略圖(