图片压缩操作工具类
工具 操作 图片 压缩
2023-09-27 14:29:11 时间
package com.ljq.durian.test; import java.awt.Graphics; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.math.MathContext; import java.util.ArrayList; import javax.imageio.ImageIO; import org.apache.log4j.Logger; /** * 图片压缩操作工具类 * * @author jqlin * */ public class ImageHelper { private static Logger log = Logger.getLogger(ImageHelper.class); public static void main(String[] args) { ImageHelper.scaleImage("C:/Users/ASUS/Desktop/0000.jpg", "C:/Users/ASUS/Desktop/1111.jpg", 1.8, "jpg"); ImageHelper.scaleImageWithParams("C:/Users/ASUS/Desktop/0000.jpg", "C:/Users/ASUS/Desktop/2222.jpg", 200, 150, true, "jpg"); } /*** * 按指定的比例缩放图片 * * @param sourceImage 源图片,如:C:/test.jpg * @param destImage 目标图片,即压缩后的图片,如:C:/new_test.jpg * @param scale 缩放比例,如1.2 * @param format 图片后缀名 */ public static void scaleImage(String sourceImage, String destImage, double scale, String format) { File file = new File(sourceImage); BufferedImage bufferedImage; try { bufferedImage = ImageIO.read(file); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); width = parseDoubleToInt(width * scale); height = parseDoubleToInt(height * scale); Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH); BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = outputImage.getGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); ImageIO.write(outputImage, format, new File(destImage)); } catch (IOException e) { log.error("按指定的比例缩放图片出错了", e); } } /*** * 将图片缩放到指定的高度或者宽度 * * @param sourceImage 图片源地址 * @param destImage 压缩完图片的地址 * @param width 缩放后的宽度 * @param height 缩放后的高度 * @param auto 是否自动保持图片的原高宽比例 * @param format 图片格式,如:jpg */ public static void scaleImageWithParams(String sourceImage, String destImage, int width, int height, boolean auto, String format) { try { File file = new File(sourceImage); BufferedImage bufferedImage = null; bufferedImage = ImageIO.read(file); if (auto) { ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage, width, height); width = paramsArrayList.get(0); height = paramsArrayList.get(1); log.info("自动调整比例,width=" + width + " height=" + height); } Image image = bufferedImage.getScaledInstance(width, height, Image.SCALE_DEFAULT); BufferedImage outputImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics graphics = outputImage.getGraphics(); graphics.drawImage(image, 0, 0, null); graphics.dispose(); ImageIO.write(outputImage, format, new File(destImage)); } catch (Exception e) { log.error("将图片缩放到指定的高度或者宽度出错了", e); } } /** * 将double类型的数据转换为int,四舍五入原则 * * @param sourceDouble * @return */ private static int parseDoubleToInt(double sourceDouble) { int result = 0; result = (int) sourceDouble; return result; } /*** * * @param bufferedImage * 要缩放的图片对象 * @param width_scale * 要缩放到的宽度 * @param height_scale * 要缩放到的高度 * @return 一个集合,第一个元素为宽度,第二个元素为高度 */ private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage, int width_scale, int height_scale) { ArrayList<Integer> arrayList = new ArrayList<Integer>(); int width = bufferedImage.getWidth(); int height = bufferedImage.getHeight(); double scale_w = getDot2Decimal(width_scale, width); System.out.println("getAutoWidthAndHeight width=" + width + "scale_w=" + scale_w); double scale_h = getDot2Decimal(height_scale, height); if (scale_w < scale_h) { arrayList.add(parseDoubleToInt(scale_w * width)); arrayList.add(parseDoubleToInt(scale_w * height)); } else { arrayList.add(parseDoubleToInt(scale_h * width)); arrayList.add(parseDoubleToInt(scale_h * height)); } return arrayList; } /*** * 返回两个数a/b的小数点后三位的表示 * * @param a * @param b * @return */ private static double getDot2Decimal(int a, int b) { BigDecimal bigDecimal_1 = new BigDecimal(a); BigDecimal bigDecimal_2 = new BigDecimal(b); BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2, new MathContext(4)); Double double1 = new Double(bigDecimal_result.toString()); return double1; } }
相关文章
- Linux下列格式化工具 - column
- pandarallel 是一个简单而有效的工具,可以在所有可用的 CPUs 上并行执行 pandas 操作
- JDBC概述,主要API,CRUD操作,封装jdbcutil工具类,注入攻击问题, Statement与PreparedStatement的区别,数据库连接池,事物操作,批处理
- 使用ThreadLocal、Apache的dbutils的QueryRunner和dbcp2数据库连接池的BasicDataSource封装操作数据库工具
- Poetry:Python依赖管理和打包工具【最好用的Python虚拟环境Poetry】【Poetry、conda、vscode混合使用】
- iOS 10越狱有“后悔药”了:越狱移除工具发布
- easyrecovery免费数据恢复工具操作简单一键恢复数据
- F12谷歌开发者工具preserve log
- Linux命令行工具使用小贴士及技巧(一)
- 在线工具
- 【成果展示】算法工具
- Codeforces命令行工具
- onnxexplorer神经网络可视化工具
- 版本控制工具(下)——Git的远程仓库、分支管理与其它操作
- JavaScript之子类构建工具
- 数字格式化工具
- jTessBoxEditor工具安装和使用操作
- win10系统优化方法及chkdsk工具使用
- WebRTC系列-工具系列之RTC_DCHECK、RTC_CHECK,字符串操作
- IIS8中安装和使用URL重写工具(URL Rewrite)的方法
- 仿照hibernate封装的一个对数据库操作的jdbc工具类
- 用Puppet和Docker构建工具来自动化容器产品部署
- ubuntu安装mysql可视化工具MySQL-workbench及简单操作
- .NET Core(C#) 反射类的属性(PropertyInfo)、字段(FieldInfo)和方法(MethodInfo)相关操作扩展方法工具类
- java操作redis集群配置[可配置密码]和工具类(比较好用)
- Redis操作List工具类封装,Java Redis List命令封装
- ZooKeeper系列3:ZooKeeper命令、命令行工具及简单操作
- 2019-9-2-C#命令行解析工具
- Java集合框架:Collections工具类