任意格式图片生成缩略图

任意格式图片生成缩略图

ID:38653453

大小:42.50 KB

页数:6页

时间:2019-06-17

任意格式图片生成缩略图_第1页
任意格式图片生成缩略图_第2页
任意格式图片生成缩略图_第3页
任意格式图片生成缩略图_第4页
任意格式图片生成缩略图_第5页
资源描述:

《任意格式图片生成缩略图》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

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();}}/***根据限定的宽度生成缩略图(

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。