public class ImgPicCompression {
/**
* 压缩图片方法
* @param yFilePath原图片路径
* @param width 压缩宽
* @param height 压缩高
* @param quality 压缩清晰度 <b>建议为1.0</b>
* @param outFile 压缩图片后,图片名称路径
* @param percentage 是否等比压缩 若true宽高比率将将自动调整
*/
public static String doCompress(String yFilePath, int width, int height, float quality, String outFile, boolean percentage) {
if (oldFile != null && width > 0 && height > 0) {
Image srcFile=null;
try {
File file = new File(oldFile);
// 文件不存在
if (!file.exists()) {
return null;
}
/*读取图片信息*/
srcFile = ImageIO.read(file);
int new_w = width;
int new_h = height;
if (percentage) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;
double rate2 = ((double) srcFile.getHeight(null)) / (double) height + 0.1;
double rate = rate1 > rate2 ? rate1 : rate2;
new_w = (int) (((double) srcFile.getWidth(null)) / rate);
new_h = (int) (((double) srcFile.getHeight(null)) / rate);
}
/* 宽高设定*/
BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);
/*压缩之后临时存放位置*/
FileOutputStream out = new FileOutputStream(outFile);
ImageIO.write(tag, "JPG", out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
srcFile.flush();
}
return outFile;
} else {
return null;
}
}
}
java 图片获取缩略图
最新推荐文章于 2023-07-05 15:32:25 发布