Java控制图片按比例缩放- (注意内存释放)
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控制图片按比例缩放- (注意内存释放)的更多相关文章
- javascript控制图片等比例缩放
<SCRIPT language="JavaScript"> function DrawImage(ImgD,FitWidth,FitHeight){ var imag ...
- css技术之用最高和最宽的限制“max-height和max-width”做图片同比例缩放,达到图片不变形目的,做出批量打印图片功能,页面打印“window.print()”
一.简介 他们是为流而生的,像width/height这种定死的砖头式布局,min-width/max-width就没有存在的意义 ,min-width/max-width一定是自适应布局或流体布局中 ...
- C#图片按比例缩放
C#图片按比例缩放: // 按比例缩放图片 public Image ZoomPicture(Image SourceImage, int TargetWidth, int TargetHeight) ...
- 理解CSS3中的background-size(对响应性图片等比例缩放)
理解CSS3中的background-size(对响应性图片等比例缩放) 阅读目录 background-size的基本属性 给图片设置固定的宽度和高度的 固定宽度400px和高度200px-使用ba ...
- 让ie6也支持max-width,和max-height实现图片等比例缩放
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- (转载)让ie6也支持max-width,和max-height实现图片等比例缩放
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】微信小程序开发之图片等比例缩放 获取屏幕尺寸图片尺寸 自适应
原文[https://blog.csdn.net/qq_31383345/article/details/53127804] 早上在论坛上看到有人写了关于图片等比例缩放的文章,只是判断了图片宽是否大于 ...
- css 如何实现图片等比例缩放
在进行布局的时候,很多PM都要求图片等比例缩放,而且要求图片不失真,不变形,但是UI设计好了这个div的宽度又不能随意更改,而后台传过来的图片也不是等比例的图片,这就比较难受了,写成 width: 1 ...
- CSS让DIV按照背景图片的比例缩放,并让背景图片填充整个元素(转)
目的是:通过background的一系列属性,让DIV按照背景图片的比例缩放,并让背景图片填充整个DIV 首先我们需要让背景图片在指定的DIV中全部填充显示 之前看有用类似 background-at ...
随机推荐
- Rafy 框架 - 执行SQL或存储过程
有时候,开发者不想通过实体来操作数据库,而是希望通过 SQL 语句或存储过程来直接访问数据库.Rafy 也提供了一组 API 来方便实现这类需求. IDbAccesser 接口 为了尽量屏蔽各数据库中 ...
- 我是如何进行Spring MVC文档翻译项目的环境搭建、项目管理及自动化构建工作的
感兴趣的同学可以关注这个翻译项目 . 我的博客原文 和 我的Github 前段时间翻译的Spring MVC官方文档完成了第一稿,相关的文章和仓库可以点击以下链接.这篇文章,主要是总结一下这个翻译项目 ...
- 『.NET Core CLI工具文档』(六)dotnet 命令
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet command 翻译:dotnet 命令 名称 dotnet -- 运行命令行命令的一般驱动程序 概要 d ...
- WinForm构造函数的作用
最近练习C#项目:何问起收藏夹(HoverTreeSCJ),实现编辑网址时,遇到这个问题:比如打开窗口后,要自动显示数据.解决方法:那么可以通过窗体的构造函数传递参数. 比如窗体类: public p ...
- C#开发微信门户及应用(7)-微信多客服功能及开发集成
最近一直在弄微信的集成功能开发,发现微信给认证账户开通了一个多客服的功能,对于客户的咨询,可以切换至客服处理的方式,而且可以添加多个客服进行处理,这个在客户咨询比较多的时候,是一个不错的营销功能.微信 ...
- ef
现阶段使用回溯 entityframework作为.net平台自己的一个orm的框架,之前在项目中也有使用,主要采用了table和model first的方式,此两种感觉使用上也是大同小异.在项目中经 ...
- (原创)解决.net 下使用uploadify,在火狐浏览器下的error 302
简单粗劣说下哈,通过uploadify中flash在火狐下上传,造成了erroe 302, 是因为其session丢失,并修改了其sessionID. 网上有很多案列,可并没有这么直接.感觉绕了点弯. ...
- Yii 2.x Behavior - 类图
yii\base\Component 继承这个类的类都具备扩展行为的能力
- POI操作Excel
POI和Excel简介 JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI .jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel.而poi可以操作 ...
- 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method
异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...