java 生成图片缩略图_java生成生成图片缩略图

/****/

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值