/****/
packagecom.fkhwl.fkhserver.core.utils;importjava.awt.Image;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.InputStream;importjavax.imageio.ImageIO;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;/*** @ClassName: ThumbnailTools
* @Description: 缩略图生成工具
*@author* @date 2014年9月25日 下午5:18:33*/
public classThumbnailTools {private intfileSize;private String inPath; //输入图路径
private String outPath; //输出图路径
private int width = 100; //默认输出图片宽
private int height = 100; //默认输出图片高
private String inFileName; //输入图文件名
private String outFileName; //输出图文件名
private boolean proportion = true; //是否等比缩放标记(默认为等比缩放)
private String prefix = "thumbnail_";public intgetWidth() {returnwidth;
}public void setWidth(intwidth) {this.width =width;
}public intgetHeight() {returnheight;
}public void setHeight(intheight) {this.height =height;
}public ThumbnailTools setSize(intsize){this.width =size;this.height =size;return this;
}public ThumbnailTools setSize(int width, intheight){this.width =width;this.height =height;return this;
}publicString getInPath() {returninPath;
}public voidsetInPath(String inPath) {this.inPath =inPath;
}publicString getOutPath() {returnoutPath;
}public voidsetOutPath(String outPath) {this.outPath =outPath;
}public booleanisProportion() {returnproportion;
}public void setProportion(booleanproportion) {this.proportion =proportion;
}publicThumbnailTools(String path){this.inPath =path;this.outPath =path;
}publicThumbnailTools(String inPath, String outPath){this.inPath =inPath;this.outPath =outPath;
}/*** 生成缩略图
*@paramfileName 文件名
*@returnboolean*/
public boolean generate(String fileName) throwsException{this.generate(fileName, null);returnBoolean.TRUE;
}/*** 生成缩略图
*@paramfileName 文件名
*@paramoutFileName 输出文件名
*@returnboolean*/
public boolean generate(String fileName, String outFileName) throwsException{
File file= new File(inPath+fileName);this.inFileName =fileName;this.outFileName = null == outFileName ? prefix+inFileName : outFileName;this.execute(newFileInputStream(file));returnBoolean.TRUE;
}public boolean generate(InputStream inputStream, String outPath) throwsException{this.execute(inputStream);returnBoolean.TRUE;
}private void execute(InputStream inputStream) throwsException {this.fileSize =inputStream.available();
BufferedImage oldImage=ImageIO.read(inputStream);if (oldImage.getWidth() == -1) {
System.out.println("Input image cant't read or format error.");return;
}intnewWidth;intnewHeight;//是否是等比缩放
if (this.proportion == true) {//为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) oldImage.getWidth()) / (double) width;double rate2 = ((double) oldImage.getHeight()) / (double) height;//根据缩放比率大的进行缩放控制
double rate = rate1 > rate2 ?rate1 : rate2;
newWidth= (int) (((double) oldImage.getWidth()) /rate);
newHeight= (int) (((double) oldImage.getHeight()) /rate);
}else{
newWidth= width; //输出的图片宽度
newHeight = height; //输出的图片高度
}
BufferedImage tag= new BufferedImage((int) newWidth, (int) newHeight, BufferedImage.TYPE_INT_RGB);//Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
tag.getGraphics().drawImage(oldImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
String newFilePath= outPath+outFileName;
File newFile= newFile(newFilePath);
FileOutputStream out= newFileOutputStream(newFile);//JPEGImageEncoder可适用于其他图片类型的转换
JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
inputStream.close();
out.close();
System.out.println(newFilePath+" ok, size: "+(fileSize/1024)+"kb to "+(newFile.length()/1024)+"kb");
}public static voidmain(String[] args) {try{new ThumbnailTools("D:/").setSize(300, 200).generate("1.jpg", "2.jpg");
}catch(Exception e) {
e.printStackTrace();
}
}
}