图片压缩
来源:互联网 发布:游戏优化器 编辑:程序博客网 时间:2024/05/01 02:24
/**
* 压缩图片方法
*
* @param oldFile 将要压缩的图片
* @param quality 压缩清晰度 <b>建议为1.0</b>
* @param percentage 是否等比压缩 若true宽高比率将将自动调整
* @return 如果处理正确返回压缩后的文件名 null则参数可能有误
*/
public String doCompress(String oldFile, Long pictrueSizeId, float quality, boolean percentage) {
ProductPictureSize productPictureSize = (ProductPictureSize) productPictureSizeService.findById(pictrueSizeId);
if (oldFile != null && productPictureSize != null) {
Image srcFile=null;
String newImage = null;
try {
File file = new File(oldFile);
// 文件不存在
if (!file.exists()) {
return null;
}
/*读取图片信息*/
srcFile = ImageIO.read(file);
int new_w = srcFile.getWidth(null);
int new_h = srcFile.getHeight(null);
if (percentage) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) srcFile.getWidth(null)) / (double) productPictureSize.getWidth();
double rate2 = ((double) srcFile.getHeight(null)) / (double) productPictureSize.getHeight();
double rate = rate1 > rate2 ? rate1 : rate2;
if (rate > 1.0d) {
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);
/*压缩后的文件名 */
String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.'));
newImage = filePrex + myFmt.format(new Date()) + oldFile.substring(filePrex.length());
/*压缩之后临时存放位置*/
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
srcFile.flush();
}
return newImage;
} else {
return null;
}
}
* 压缩图片方法
*
* @param oldFile 将要压缩的图片
* @param quality 压缩清晰度 <b>建议为1.0</b>
* @param percentage 是否等比压缩 若true宽高比率将将自动调整
* @return 如果处理正确返回压缩后的文件名 null则参数可能有误
*/
public String doCompress(String oldFile, Long pictrueSizeId, float quality, boolean percentage) {
ProductPictureSize productPictureSize = (ProductPictureSize) productPictureSizeService.findById(pictrueSizeId);
if (oldFile != null && productPictureSize != null) {
Image srcFile=null;
String newImage = null;
try {
File file = new File(oldFile);
// 文件不存在
if (!file.exists()) {
return null;
}
/*读取图片信息*/
srcFile = ImageIO.read(file);
int new_w = srcFile.getWidth(null);
int new_h = srcFile.getHeight(null);
if (percentage) {
// 为等比缩放计算输出的图片宽度及高度
double rate1 = ((double) srcFile.getWidth(null)) / (double) productPictureSize.getWidth();
double rate2 = ((double) srcFile.getHeight(null)) / (double) productPictureSize.getHeight();
double rate = rate1 > rate2 ? rate1 : rate2;
if (rate > 1.0d) {
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);
/*压缩后的文件名 */
String filePrex = oldFile.substring(0, oldFile.lastIndexOf('.'));
newImage = filePrex + myFmt.format(new Date()) + oldFile.substring(filePrex.length());
/*压缩之后临时存放位置*/
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
/* 压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
srcFile.flush();
}
return newImage;
} else {
return null;
}
}
- 图片压缩。大小压缩
- 图片压缩按比例压缩
- 压缩图片
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 图片压缩
- 压缩图片
- 图片压缩
- get请求方式中可能出现的乱码问题的解决方案
- gtk多线程更新界面
- Android APK反编译详解
- ubuntu win7双系统ubuntu挂载win7下分区方法
- GTK多线程界面更新(续)
- 图片压缩
- android 反编译工具
- iOS 消息推送 (待续)
- 内存管理FAQ(freshground )
- 【文本分类】 特征抽取之信息增益
- 在.cpp文件中,memset struct类型 所引发的segmentation fault
- 有关Dynamic Web TWAIN的一些问题
- c++基础---C++之模板
- Python扩展方法及工具比较