Java图片批量压缩像素的实现方法是什么
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,Java图片批量压缩像素的实现方法是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。图片压缩大法为了防止用户流量的丢失
千家信息网最后更新 2025年11月08日Java图片批量压缩像素的实现方法是什么
Java图片批量压缩像素的实现方法是什么,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
图片压缩大法
为了防止用户流量的丢失,即使在5g 即将来临的情况下,压缩算法依旧是很有必要的,额跑题了,不好意思,今天介绍的不是压缩算法,讲啥呢?主要讲讲如何通过 java 将图片进行压缩,尽可能的控制压缩损比,不仅仅是为了减少存储,其目的是快速呈现给用户,只有良好的体验,才会在当今这个急躁的年代减少流量的损失。
最近因为公司要需要xxx认证上传测试用例功能的具体截图、发现有大小限制、所以就进行了图片压缩,简单记录一下。
压缩前大小:

压缩后大小:

具体代码实现:
main方法测试:
public static void main(String[] args) throws IOException { String modpath = "C:\\Users\\Administrator\\Desktop\\鲲鹏认证\\test\\"; getFiles("C:\\Users\\Administrator\\Desktop\\鲲鹏认证\\测试用例清单", modpath, 160);//将图片压缩至100宽 }文件大小处理
/** * @param srcPath 原图片路径 * @param desPath 转换大小后图片路径 * @param width 转换后图片宽度 * @param height 转换后图片高度 */ public static void resizeImage(String srcPath, String desPath, int width, int height) throws IOException { File srcFile = new File(srcPath); Image srcImg = ImageIO.read(srcFile); BufferedImage buffImg = null; buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); //使用TYPE_INT_RGB修改的图片会变色 buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); String filePath=""; if (srcFile.getName().contains("#")) { filePath = srcFile.getName().replace("#", ""); }else{ filePath=srcFile.getName(); } ImageIO.write(buffImg, "PNG", new File(desPath + filePath)); }获取目录文件信息
/** * @param scaleSize 图片的修改比例,目标宽度 */ public static void getFiles(String path, String modPath, int scaleSize) throws IOException { ArrayList files = new ArrayList(); File file = new File(path); File[] tempList = file.listFiles(); //循环读取目录下图片 for (int i = 0; i < tempList.length; i++) { String filePath = tempList[i].getName(); if (tempList[i].isFile()) { System.out.println("文件:" + filePath + "-" + tempList[i].getAbsolutePath().replaceAll("\\\\", "/")); String[] imagePath = tempList[i].getAbsolutePath().replaceAll("\\\\", "/").split("/"); String imageNumber = null; FileUtil.resizeImage(tempList[i].getAbsolutePath().replaceAll("\\\\", "/"), modPath, 160, 160); files.add(tempList[i].toString()); } if (tempList[i].isDirectory()) { System.out.println("文件夹:" + tempList[i]); } } System.out.println(path + "下文件数量:" + files.size()); } 控制台目录压缩成功保存到盘符:
附:利用Graphics类如何进行压缩图像
Graphics类提供基本绘图方法,Graphics类提供基本的几何图形绘制方法,主要有:画线段、画矩形、画圆、画带颜色的图形、画椭圆、画圆弧、画多边形、画字符串等。 这里不做一一赘述, 进重点介绍一下,利用Graphics类如何进行压缩图像。不多说直接上代码。
/** * compressImage * * @param imageByte * Image source array * @param ppi * @return */ public static byte[] compressImage(byte[] imageByte, int ppi) { byte[] smallImage = null; int width = 0, height = 0; if (imageByte == null) return null; ByteArrayInputStream byteInput = new ByteArrayInputStream(imageByte); try { Image image = ImageIO.read(byteInput); int w = image.getWidth(null); int h = image.getHeight(null); // adjust weight and height to avoid image distortion double scale = 0; scale = Math.min((float) ppi / w, (float) ppi / h); width = (int) (w * scale); width -= width % 4; height = (int) (h * scale); if (scale >= (double) 1) return imageByte; BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(buffImg, "png", out); smallImage = out.toByteArray(); return smallImage; } catch (IOException e) { log.error(e.getMessage()); throw new RSServerInternalException(""); } }其实,关键点就两处
BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);buffImg.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。
图片
大小
文件
方法
目录
测试
认证
代码
图像
图形
宽度
流量
用户
算法
路径
鲲鹏
帮助
控制
像素
不好意思
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
在创建数据库后应该怎么做
迷你世界怎么免费开服务器电脑版
网络安全知识英语竞赛入口
海典软件数据库
计算机网络技术基测
青年文明号网络安全
网络安全证书有哪些
数据库打开根目录命令
吐鲁番网络安全攻防
计算机网网络技术是学什么
网页服务器怎么消毒
五大互联网科技公司
杰理蓝牙芯片软件开发工具
如何提取数据库中相似数据
网络安全测评资质怎么办
数据库产业
软件公司服务器管理
数据库的应用和重要性
高科技与互联网企业名单
物质半数致死量数据库
怎么用云服务器给游戏开服
深圳坐标软件开发怎样
软件开发延期补充协议
江苏沙钢网络安全教育平台
网络安全自学难吗
指挥中心网络安全事故
宁波学习软件开发
软件开发跟岗实习小结
hana数据库判断重复数据
网络诈骗属于网络安全隐患吗