package mytiny.com.common;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class ImageZipUtil {

public static void main(String[] args) {
zipWidthHeightImageFile(new File("D:\\3.png"), new File("D:\\3.png"), 375, 180, 0.8f);

// System.out.println("ok");
}

/**
* 根据设置的宽高等比例压缩图片文件<br>
* 先保存原文件,再压缩、上传
*
* @param oldFile
* 要进行压缩的文件
* @param newFile
* 新文件
* @param width
* 宽度 //设置宽度时(高度传入0,等比例缩放)
* @param height
* 高度 //设置高度时(宽度传入0,等比例缩放)
* @param quality
* 质量
* @return 返回压缩后的文件的全路径
*/
public static String zipImageFile(File oldFile, File newFile, int width, int height, float quality) {
if (oldFile == null) {
return null;
}
try {
/** 对服务器上的临时文件进行处理 */
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
int h = srcFile.getHeight(null);
double bili;
if (width > 0) {
bili = width / (double) w;
height = (int) (h * bili);
} else {
if (height > 0) {
bili = height / (double) h;
width = (int) (w * bili);
}
}

String srcImgPath = newFile.getAbsoluteFile().toString();
// System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());

BufferedImage buffImg = null;
if (subfix.equals("png")) {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}

Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

ImageIO.write(buffImg, subfix, new File(srcImgPath));

} catch (Exception e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}

/**
* 按设置的宽度高度压缩图片文件<br>
* 先保存原文件,再压缩、上传
*
* @param oldFile
* 要进行压缩的文件全路径
* @param newFile
* 新文件
* @param width
* 宽度
* @param height
* 高度
* @param quality
* 质量
* @return 返回压缩后的文件的全路径
*/
public static boolean zipWidthHeightImageFile(File oldFile, File newFile, int width, int height, float quality) {
if (oldFile == null) {
return false;
}
Image srcFile = null;
BufferedImage buffImg = null;
try {
/** 对服务器上的临时文件进行处理 */
srcFile = ImageIO.read(oldFile);

String srcImgPath = newFile.getAbsoluteFile().toString();
// System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());

if (subfix.equals("png")) {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
} else {
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}

Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255, 255, 255));
graphics.setColor(new Color(255, 255, 255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);

ImageIO.write(buffImg, subfix, new File(srcImgPath));
return true;

} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (srcFile != null) {
srcFile.flush();
}
if (buffImg != null) {
buffImg.flush();
}

}

}

}

Java控制图片按比例缩放- (注意内存释放)的更多相关文章

  1. javascript控制图片等比例缩放

    <SCRIPT language="JavaScript"> function DrawImage(ImgD,FitWidth,FitHeight){ var imag ...

  2. css技术之用最高和最宽的限制“max-height和max-width”做图片同比例缩放,达到图片不变形目的,做出批量打印图片功能,页面打印“window.print()”

    一.简介 他们是为流而生的,像width/height这种定死的砖头式布局,min-width/max-width就没有存在的意义 ,min-width/max-width一定是自适应布局或流体布局中 ...

  3. C#图片按比例缩放

    C#图片按比例缩放: // 按比例缩放图片 public Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight) ...

  4. 理解CSS3中的background-size(对响应性图片等比例缩放)

    理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...

  5. 让ie6也支持max-width,和max-height实现图片等比例缩放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. (转载)让ie6也支持max-width,和max-height实现图片等比例缩放

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 【转】微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应

    原文[https://blog.csdn.net/qq_31383345/article/details/53127804] 早上在论坛上看到有人写了关于图片等比例缩放的文章,只是判断了图片宽是否大于 ...

  8. css 如何实现图片等比例缩放

    在进行布局的时候,很多PM都要求图片等比例缩放,而且要求图片不失真,不变形,但是UI设计好了这个div的宽度又不能随意更改,而后台传过来的图片也不是等比例的图片,这就比较难受了,写成 width: 1 ...

  9. CSS让DIV按照背景图片的比例缩放,并让背景图片填充整个元素(转)

    目的是:通过background的一系列属性,让DIV按照背景图片的比例缩放,并让背景图片填充整个DIV 首先我们需要让背景图片在指定的DIV中全部填充显示 之前看有用类似 background-at ...

随机推荐

  1. EF 在controller弹出提示消息

    第一种方式: return Content("<script>alert('此名称课程再次班级中已经存在!');window.location.href = 'Course/Cr ...

  2. 查看Sql Server被锁的表以及解锁

    查看被锁表: select spId from master..SysProcesses where db_Name(dbID) = '数据库名称' and spId <> @@SpId ...

  3. C#开发微信门户及应用(13)-使用地理位置扩展相关应用

    本文继续上一篇<C#开发微信门户及应用(12)-使用语音处理>,继续介绍微信的相关应用.我们知道,地理位置信息可以用来做很多相关的应用,除了我们可以知道用户所在的位置,还可以关联出一些地理 ...

  4. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  5. java中的switch case

    switch-case语句格式如下 switch(变量){ case 变量值1: //; break; case 变量值2: //...; break; ... case default: //... ...

  6. 【转】推荐10款最热门jQuery UI框架

    推荐10款最热门jQuery UI框架 原创 在进行Web开发时,并非所有的库都适合你的项目,但你仍需要收藏一些Web UI设计相关的库或框架,以在你需要的时候,加快你的开发效率.本文为你推荐10款非 ...

  7. Android Weekly Notes Issue #222

    Android Weekly Issue #222 September 11th, 2016 Android Weekly Issue #222 ARTICLES & TUTORIALS Fo ...

  8. 下载本 WebEnh博客 安卓APP

    暂时还在学习开发安卓和苹果APP应用,写得一般,以后会更新的,谢谢大家关注.对了这个是用HTML5+写的哦.不太难,但是要搞懂还是要多花点时间了,有时间就会更新的 ... ...

  9. nfs挂载配置

    nfs挂载步骤 服务器端 1.安装nfs-utils rpcbind $sudo yum –y install nfs-utils rpcbind 2.文件开放出去配置/etc/exports 例子: ...

  10. XSS 防御方法总结

    1. XSS攻击原理 XSS原称为CSS(Cross-Site Scripting),因为和层叠样式表(Cascading Style Sheets)重名,所以改称为XSS(X一般有未知的含义,还有扩 ...