public static void main(String[] args) throws IOException {File originalImage = new File("C:\\Users\\Win\\Desktop\\faceImages\\14.jpg");System.out.println("源文件大小" + originalImage.length());File resizedImg1 = new File("C:\\Users\\Win\\Desktop\\productImages\\14.jpg");resize(originalImage, resizedImg1, 64, 0.7f);System.out.println("0.7转换后文件大小" + resizedImg1.length());}
## 遍历文件夹下图片批量压缩public static void main(String[] args) throws IOException {File folder = new File("C:\\Users\\Win\\Desktop\\faceImages");//文件夹路径File[] listOfFiles = folder.listFiles();for (int i = 0; i < listOfFiles.length; i++) {if (listOfFiles[i].isFile()) {File originalImage = listOfFiles[i];System.out.println("源文件大小" + originalImage.length());String fileName = originalImage.getName();File resizedImg1 = new File("C:\\Users\\Win\\Desktop\\productImages\\" + fileName);resize(originalImage, resizedImg1, 64, 0.9f);System.out.println(fileName + "0.8转换后文件大小" + resizedImg1.length());} else if (listOfFiles[i].isDirectory()) {System.out.println("Directory " + listOfFiles[i].getName());}}}/*** 缩放图片(压缩图片质量,改变图片尺寸)* 若原图宽度小于新宽度,则宽度不变!* @param newWidth 新的宽度* @param quality  图片质量参数 0.7f 相当于70%质量*/public static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException {if (quality > 1) {}ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());Image i = ii.getImage();Image resizedImage = null;int iWidth = i.getWidth(null);int iHeight = i.getHeight(null);if (iWidth < newWidth) {newWidth = iWidth;}if (iWidth > iHeight) {resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);} else {resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);}Image temp = new ImageIcon(resizedImage).getImage();BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB);Graphics g = bufferedImage.createGraphics();g.setColor(Color.white);g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));g.drawImage(temp, 0, 0, null);g.dispose();float softenFactor = 0.05f;float[] softenArray = {0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0};Kernel kernel = new Kernel(3, 3, softenArray);ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);bufferedImage = cOp.filter(bufferedImage, null);FileOutputStream out = new FileOutputStream(resizedFile);JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);param.setQuality(quality, true);encoder.setJPEGEncodeParam(param);encoder.encode(bufferedImage);}

java 实现图片像素大小调整并压缩图片相关推荐

  1. java采用降低图片分辨率大小来压缩图片大小

    我看了网上大多数对图片的压缩都是基于对图片width和hight进行裁剪来降低图片实际大小,但这种方式容易导致图片内容的丢失,所以推荐大家采用以下这种方式 import java.awt.image. ...

  2. 简单的压缩图片的方法,压缩图片大小的步骤

    我们在日常生活中也会遇到比较大的图片,比较大的图片文件比较麻烦,上传到网站论坛做头像都传不上去,大家可以将图片文件进行简单的压缩,然后进行上传,教给大家一种简单的压缩图片的方法. 1:先要将自己的图片 ...

  3. linux压缩图片脚本,说明Ubuntu压缩图片脚本批量方法

    以下就是Ubuntu压缩图片批量方法,这些Ubuntu压缩图片方法是我学了很久的心得,希望对大家有帮助.用DC拍出来的很多照片尺寸都很大,有时候我们需要缩小一下,最近google了一圈,发现了一些有用 ...

  4. 如何使用python批量压缩图片_Python实现批量压缩图片

    # -*- coding: utf-8 -*- """ __author__= 'Du' __creation_time__= '2018/1/5 10:06' &quo ...

  5. android压缩图片不失真,Android压缩图片到100K以下并保持不失真的高效方法 - feicien的博客 - eoe移动开发者社区...

    1.获取原始图片的长和宽 1 2 3 4 5 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDe ...

  6. 怎样将png格式的图片缩小?如何快速压缩图片的大小?

    Png 格式的图片相比jpg格式的图片要更加的清晰,因此也就导致了png格式的图片在相同的情况下比jpg格式的图片体积更大,由于这个原因,所以我们在使用一些png格式图片的时候就会遇到因图片体积过大而 ...

  7. js优化上传大图片显示问题(压缩图片展示)

    亲测有效,下面是上传后的图片加载时间以及压缩图片加载时间. 一.解决方案 上传文件时,发现上传4M的图片,上传时有进度条,但是进度条结束之后,加载图片耗时917ms,当用户焦急的盯着进度条结束后,还要 ...

  8. 电脑上怎么压缩jpg图片?什么软件压缩图片好用?

    随着现在拍照设备的越来越好,大家拍摄出来的jpg图片体积也越来越大,动不动就十几M,储存和使用都非常的不方便.有时候需要通过jpg格式压缩软件来缩小图片大小,需要下载安装的,操作步骤也十分复杂,浪费了 ...

  9. 图片转 base64,压缩图片体积

    背景:在项目开发中,有个场景用到图片转base64的场景,并压缩图片体积.所以记录一下相关工具函数.前端目前使用的vue3(3.2.33)+ts. src\utils\file\base64Conve ...

最新文章

  1. Android Dialog 全屏、Dialog 底部显示
  2. 还是来说class,什么鬼,类会生宝宝
  3. 重学前端-学习笔记-JavaScript对象
  4. python | ^ ~
  5. 驱动列举进程输出到应用层
  6. 华三实现vlan通过
  7. mysql数据库建立的数据库在哪个文件夹?
  8. java的smalltalk规则,Smalltalk相当于Java的静态是什么?
  9. Python 科学计算
  10. 由简入难学习3d机械制图软件顺序?Pro/Engineer 与CREO有何不同。
  11. 利用记事本编写html代码和word实现A4信笺纸(信签纸)电子版的两种设计法
  12. day09 文件操作相关
  13. Windows 系统服务优化指南
  14. Coolpad(酷派) 进入手机工厂模式
  15. 【OpenGL】笔记二十七、几何着色器
  16. c语言贪心算法找零问题,贪心算法-找零问题-实验报告
  17. 【分享】ArcGIS 根据DEM生成等高线以及带高程转换为Auto CAD数据
  18. 上海域格LTE模块CLM920_JC3贴片SIM卡双卡切换
  19. link和@import的区别:
  20. PS怎么更换背景?进来学习一下这些方法

热门文章

  1. Manjaro双网卡网速很慢
  2. 2017 微信数据报告:你需要了解这些数据
  3. HbuilderX+Android studio做原生安卓应用
  4. UE4 置换贴图 曲面细分
  5. 洛谷 P3258 [JLOI2014]松鼠的新家(树链剖分)
  6. sql模糊查询及通配符使用
  7. 所有国产手机都用鸿蒙os,华为官方霸气表态!所有国产手机都能用鸿蒙OS系统:想用就支持...
  8. 民安汇智帮助某医院开展第三方满意度调查
  9. 苹果手机怎么取消优酷自动续费_优酷:优酷公众号怎么取消自动续费会员
  10. 湘东职业中等专业学校20计算机的图片,萍乡湘东职业中等专业学校中专