JAVA生成缩略图

package com.my.image;

import javax.imageio.ImageIO;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ImageUtils {

public static void main(String[] args) throws FileNotFoundException, IOException {
File picture = new File("C:/Users/Administrator/Desktop/buys/1.png");
BufferedImage sourceImg =ImageIO.read(new FileInputStream(picture));
System.out.println(picture.getPath().substring(picture.getPath().lastIndexOf(".")));
System.out.println(picture.length()/1024.0);
System.out.println("width:"+sourceImg.getWidth()+"px");
System.out.println("height:"+sourceImg.getHeight()+"px");
System.out.println(picture.getName().substring(0,picture.getName().lastIndexOf("."))+"_pic"+picture.getName().substring(picture.getName().lastIndexOf(".")));
try {
String[] rets = ImageUtils.saveImage("C:/Users/Administrator/Desktop/buys/1_1.png","C:/Users/Administrator/Desktop/buys/1_3.png", 200);
System.out.println("code:"+rets[0]+",Msg:"+rets[1]);
} catch (Exception e) {
e.printStackTrace();
}

//System.out.println(Double.parseDouble(String.format("%.1f",1000d/600)));

}

/**
* 生成文件
* @param image 源文件流
* @param newWidth 缩小宽度
* @param newHeigh 缩小高度
* @return
*/
public static BufferedImage resize(BufferedImage image, int newWidth,int newHeigh) {
int type = image.getType();
BufferedImage newImage = null;
double sx = (double) newWidth / image.getWidth();
double sy = (double) newHeigh / image.getHeight();
if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = image.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(newWidth,newHeigh);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
newImage = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
newImage = new BufferedImage(newWidth, newHeigh, type);
Graphics2D g = newImage.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(image, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
}

return newImage;
}

/**
* 保存缩略图
* @param fromFilePath 源文件路径
* @param saveToFilePath 生成新文件路径
* @param size 限制尺寸
* @return
* @throws Exception
*/
public static String[] saveImage(String fromFilePath, String saveToFilePath,int size) throws Exception {
String[] rets = new String[2];
BufferedImage srcImage;
String imgType = "JPEG";
double maxLeng = 0d;
double ratio = 0;
if (fromFilePath.toLowerCase().endsWith(".png")) {
imgType = "PNG";
}
else if (fromFilePath.toLowerCase().endsWith(".jpg")) {
imgType = "JPEG";
}
else if (fromFilePath.toLowerCase().endsWith(".bmp")) {
imgType = "BMP";
}
else if (fromFilePath.toLowerCase().endsWith(".gif")) {
imgType = "GIF";
} else {
rets[0]="0";
rets[1]="不支持此图片格式!";
return rets;
}
File saveFile = new File(saveToFilePath);
File fromFile = new File(fromFilePath);
srcImage = ImageIO.read(fromFile);
int fileSize = 2048;
if (fromFile.length()/1024.0>fileSize) {
rets[0]="0";
rets[1]="图片过大,请上传不大于2M的图片!";
return rets;
}
double width = (double)srcImage.getWidth();
double height = (double)srcImage.getHeight();
if (width > 0 || height > 0) {
//计算比例
if (width > height) {
maxLeng = width;
ratio = maxLeng/size;
} else {
maxLeng = height;
ratio = maxLeng/size;
}
if (maxLeng > size) {
int newWidth = (int) (width/ratio);
int newHeight = (int) (height/ratio);
srcImage = resize(srcImage, newWidth, newHeight);
}
}
boolean isSuccess = ImageIO.write(srcImage, imgType, saveFile);
if (isSuccess) {
rets[0]="1";
rets[1]="上传成功!";
return rets;
}else {
rets[0]="0";
rets[1]="上传发生错误!";
return rets;
}
}

}

以上代码复制可用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值