多功能图片压缩工具类
2015-08-06 15:18
225 查看
在网上看了一些图片压缩的方法,在这里我利用重载,编写了多种功能的图片压缩工具类
import java.awt.Color; import java.awt.Graphics2D; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * < p>< strong>图片压缩工具类< /strong>< /p> * < p>功能支持:< br> * 1)将源路径的图片/源图片文件,按照指定的图片尺寸、图片质量(默认图片质量为1)压缩,存放到目标路径下< br> * 2)将源路径的图片/源图片文件,按照指定的图片比例、图片质量(默认图片质量为1)压缩,存放到目标路径下< br> * 3)将源路径的图片/源图片文件,按照指定的最大长度进行、图片质量(默认图片质量为1)压缩,存放到目标路径下< br> * * < p>该工具类还包含其他方法:< br> * (一)读取图片文件< br> * (二)输出图片文件(根据指定的图片质量输出)< br> * (三)压缩图片(其中压缩图片包含三种重构:1)按照指定的尺寸 2)按照指定的比例 3)按照指定的最大长度进行)< br> * < /p> * @version 1.0.0 */ public class ImageCompressionUtil { /** * 将源路径的图片,按照指定的图片尺寸、图片质量压缩,存放到目标路径下 * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param new_width 压缩后的长度 * @param new_height 压缩后的高度 * @param percentage 图片质量百分比 * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, int new_width, int new_height, float percentage) throws ImageFormatException, IOException { //得到图片 BufferedImage sourceImage = inputImage(sourcePath); //压缩图片 BufferedImage targetImage = getCompressionedImage(sourceImage, new_width, new_height); //输出图片 outputImage(targetPath,targetImage,percentage); } /** * 将源路径的图片,按照指定的图片尺寸压缩,存放到目标路径下(默认图片质量为1) * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param new_width 压缩后的长度 * @param new_height 压缩后的高度 * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, int new_width, int new_height) throws ImageFormatException, IOException { compression(sourcePath, targetPath, new_width, new_height, 1F); } /** * 将源路径的图片,按照指定的图片比例、图片质量压缩,存放到目标路径下 * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param ratio 压缩百分比 * @param percentage 图片质量百分比 * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, float ratio, float percentage) throws ImageFormatException, IOException { BufferedImage sourceImage = inputImage(sourcePath); BufferedImage targetImage = getCompressionedImage(sourceImage, ratio); outputImage(targetPath,targetImage,percentage); } /** * 将源路径的图片,按照指定的图片比例压缩,存放到目标路径下(图片质量默认为1) * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param ratio 压缩百分比 * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, float ratio) throws ImageFormatException, IOException { compression(sourcePath, targetPath, ratio, 1F); } /** * 将源图片文件,按照指定的图片尺寸、图片质量压缩,存放到目标路径下 * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param new_width 压缩后的长度 * @param new_height 压缩后的高度 * @param percentage 图片质量百分比 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, int new_width, int new_height, float percentage) throws ImageFormatException, IOException { BufferedImage targetImage = getCompressionedImage(sourceImage, new_width, new_height); outputImage(targetPath,targetImage,percentage); } /** * 将源图片文件,按照指定的图片尺寸压缩,存放到目标路径下(图片质量默认为1) * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param new_width 压缩后的长度 * @param new_height 压缩后的高度 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, int new_width, int new_height) throws ImageFormatException, IOException { compression(sourceImage, targetPath, new_width, new_height, 1F); } /** * 将源图片文件,按照指定的图片比例、图片质量压缩,存放到目标路径下 * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param ratio 压缩百分比 * @param percentage 图片质量百分比 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, float ratio, float percentage) throws ImageFormatException, IOException { BufferedImage targetImage = getCompressionedImage(sourceImage, ratio); outputImage(targetPath,targetImage,percentage); } /** * 将源图片文件,按照指定的图片比例压缩,存放到目标路径下(图片质量默认为1) * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param ratio 压缩百分比 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, float ratio) throws ImageFormatException, IOException { compression(sourceImage, targetPath, ratio, 1F); } /** * 将源路径的图片,按照指定的最大长度进行、图片质量压缩,存放到目标路径下 * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param maxLength 压缩后的最大长度(长/高) * @param percentage 图片质量百分比 * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, int maxLength, float percentage) throws ImageFormatException, IOException { BufferedImage sourceImage = inputImage(sourcePath); BufferedImage targetImage = getCompressionedImage(sourceImage, maxLength); outputImage(targetPath,targetImage,percentage); } /** * 将源路径的图片,按照指定的最大长度进行压缩,存放到目标路径下(图片质量默认为1) * @param sourcePath 源图片路径 * @param targetPath 目标路径 * @param maxLength 压缩后的最大长度(长/高) * @version 1.0.0 * @throws ImageFormatException * @throws IOException */ public void compression(String sourcePath, String targetPath, int maxLength) throws ImageFormatException, IOException { compression(sourcePath, targetPath, maxLength, 1F); } /** * 将源图片文件,按照指定的最大长度进行、图片质量压缩,存放到目标路径下 * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param maxLength 压缩后的最大长度(长/高) * @param percentage 图片质量百分比 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, int maxLength, float percentage) throws ImageFormatException, IOException { BufferedImage targetImage = getCompressionedImage(sourceImage, maxLength); outputImage(targetPath,targetImage,percentage); } /** * 将源图片文件,按照指定的最大长度进行压缩,存放到目标路径下(图片质量默认为1) * @param sourceImage 源图片文件 * @param targetPath 目标路径 * @param maxLength 压缩后的最大长度(长/高) * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void compression(BufferedImage sourceImage, String targetPath, int maxLength) throws ImageFormatException, IOException { compression(sourceImage, targetPath, maxLength, 1F); } /** * 读取源图片 * @param sourcePath 源图片路径 * @version 1.0.0 * @throws IOException */ public BufferedImage inputImage(String sourcePath) throws IOException { BufferedImage sourceImage = null; //读取文件 File file = new File(sourcePath); //获取文件流 FileInputStream fis; fis = new FileInputStream(file); //字节 byte[] b = new byte[5]; fis.read(b); sourceImage = javax.imageio.ImageIO.read(file); fis.close(); return sourceImage; } /** * 将图片文件输出到目标路径,并设置压缩质量 * @param targetPath 目标路径 * @param targetImage 指定图片文件 * @param percentage 图片质量百分比 * @version 1.0.0 * @throws IOException * @throws ImageFormatException */ public void outputImage(String targetPath,BufferedImage targetImage, float percentage) throws ImageFormatException, IOException{ File file = new File(targetPath); //判断输出的文件夹路径是否存在,不存在则创建 if(!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } //输出到文件流 FileOutputStream fos = new FileOutputStream(targetPath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(targetImage); //压缩质量 jep.setQuality(percentage, true); encoder.encode(targetImage, jep); fos.close(); } /** * 将源图片进行压缩,得到压缩后的图片 * @param sourceImage 源图片文件 * @param new_width 压缩后的长度 * @param new_height 压缩后的高度 * @version 1.0.0 */ public BufferedImage getCompressionedImage(BufferedImage sourceImage,int new_width, int new_height){ System.out.println("[" + new Date().toString() + "]-正在压缩图片..."); //得到原图片的长和高 int old_width = sourceImage.getWidth(); int old_height = sourceImage.getHeight(); //根据原图大小,创建一个空白画布 BufferedImage blankCanvas = new BufferedImage(old_width,old_height, BufferedImage.TYPE_INT_RGB); //在新的画布上从生成原图的缩略图 Graphics2D g2D = blankCanvas.createGraphics(); g2D.setColor(Color.white); //填充,参数x/y分别表示起始坐标(左上角坐标),w/h分别表示长和高(单位是像素) g2D.fillRect(0, 0, old_width, old_height); g2D.drawImage(sourceImage, 0, 0, old_width, old_height, Color.white, null); g2D.dispose(); BufferedImage targetImage = new BufferedImage(new_width, new_height, BufferedImage.TYPE_INT_RGB); //缩放图片,getScaledInstance方法实现缩放 targetImage.getGraphics().drawImage(blankCanvas.getScaledInstance( new_width, new_height, Image.SCALE_SMOOTH), 0, 0, null); System.out.println("[" + new Date().toString() + "]-压缩完成"); return targetImage; } /** * 根据比例对图片进行压缩 * @param sourceImage 源图片文件 * @param ratio 百分比 * @version 1.0.0 */ public BufferedImage getCompressionedImage(BufferedImage sourceImage,float ratio){ int old_w = sourceImage.getWidth(); int old_h = sourceImage.getHeight(); return getCompressionedImage(sourceImage, (int) Math.round(old_w * ratio), (int) Math.round(old_h * ratio)); } /** * 根据最大长度或宽对图片进行压缩 * @param sourceImage 源图片文件 * @param maxLength 长或者宽的最大值 * @version 1.0.0 */ public BufferedImage getCompressionedImage(BufferedImage sourceImage,int maxLength){ int old_width = sourceImage.getWidth(); int old_height = sourceImage.getHeight(); int new_width = 0; int new_height = 0; // 根据图片尺寸压缩比得到新图的尺寸 if (old_width > old_height) { // 图片要缩放的比例 new_width = maxLength; new_height = (int) Math.round(old_height * ((float) maxLength / old_width)); } else { new_width = (int) Math.round(old_width * ((float) maxLength / old_height)); new_height = maxLength; } return getCompressionedImage(sourceImage, new_width, new_height); } }
相关文章推荐
- try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,还是在return之后执行?
- 使用HttpClient实现文件的上传下载
- iOS学习之UITabBarController简单介绍
- POJ 3107 Godfather(树形DP)
- Quicksum
- MyBatis学习总结(三)——优化MyBatis配置文件中的配置
- PADS蛇形走线技巧V0[1].2
- 理解CSS3 transform中的Matrix(矩阵)
- tornado 反向代理后 获取真实客户端IP
- Microsoft Visual Studio 企业应用开发高效编程
- 图解Spring框架的设计理念与设计模式
- android无渠道号推广的细分统计
- void类型指针使用
- 记忆的品质
- MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
- Jackson 框架,轻易转换JSON
- 数据结构基础(8) --单链表的设计与实现(1)之基本操作
- Eclipse 4.5.0 离线安装 Veloeclipse 插件
- Adb refused a command 解决方法
- AX2009报表发送邮件(一)