java生成缩略图

/**
* @param sourceFilePath
* @param targetFilePath
* @param width 目标宽
* @param height 目标高
* @param per 百分比
*/
private static void thumb(String sourceFilePath, String targetFilePath, int width, int height,
float per) throws Exception {
Image src;
try {
src = javax.imageio.ImageIO.read(new File(sourceFilePath)); //构造Image对象

//得到源图宽,高
int oldWidth = src.getWidth(null);
int oldHeight = src.getHeight(null);
int newWidth = 0;
int newHeight = 0;

double tmpWidth = (oldWidth * 1.00) / (width * 1.00);
double tmpHeight = (oldHeight * 1.00) / (height * 1.00);

// 如果图片目标尺寸与原图相同或者目标尺寸不是正方形则不留白处理
if (oldWidth != width && oldHeight != height) {
//图片跟据长宽留白,成一个正方形图。
BufferedImage oldpic;
if (oldWidth > oldHeight) {
oldpic = new BufferedImage(oldWidth, oldWidth, BufferedImage.TYPE_INT_RGB);
} else if (oldWidth < oldHeight) {
oldpic = new BufferedImage(oldHeight, oldHeight, BufferedImage.TYPE_INT_RGB);
} else {
oldpic = new BufferedImage(oldWidth, oldHeight, BufferedImage.TYPE_INT_RGB);
}
Graphics2D g = oldpic.createGraphics();
g.setColor(Color.white);
if (oldWidth > oldHeight) {
g.fillRect(0, 0, oldWidth, oldWidth);
g.drawImage(src, 0, (oldWidth - oldHeight) / 2, oldWidth, oldHeight,
Color.white, null);
} else if (oldWidth < oldHeight) {
g.fillRect(0, 0, oldHeight, oldHeight);
g.drawImage(src, (oldHeight - oldWidth) / 2, 0, oldWidth, oldHeight,
Color.white, null);
} else {
g.drawImage(src.getScaledInstance(oldWidth, oldHeight, Image.SCALE_SMOOTH), 0,
0, null);
}
g.dispose();
src = oldpic;
}
//图片调整为方形结束
//计算新图宽,高
if (oldWidth > width) {
newWidth = (int) Math.round(oldWidth / tmpWidth);
} else if (oldWidth < width) {
newWidth = (int) Math.round(oldWidth / tmpWidth);
} else {
newWidth = oldWidth;
}
if (oldHeight > height) {
newHeight = (int) Math.round(oldHeight / tmpHeight);
} else if (oldHeight < height) {
newHeight = (int) Math.round(oldHeight / tmpHeight);
} else {
newHeight = oldHeight;
}
BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图
//tag.getGraphics().drawImage(src,0,0,new_w,new_h,null);
tag.getGraphics().drawImage(
src.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null);
//输出文件流
FileOutputStream outPutStream = new FileOutputStream(targetFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outPutStream);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 压缩质量 */
jep.setQuality(per, true);
encoder.encode(tag, jep);
//近JPEG编码
//encoder.encode(tag);
outPutStream.flush();
outPutStream.close();
} catch (IOException e) {
logger.error("生成缩略图异常 [sourceFilePath = " + sourceFilePath + ", targetFilePath = "
+ targetFilePath + ", width = " + width + ", height = " + height
+ ", per = " + per + "]", e);
throw e;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值