在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

详解Java中使用ImageIO类对图片进行压缩的方法

2016-7-29 15:41| 发布者: zhangjf| 查看: 608| 评论: 0

摘要: 最近做项目需要图片压缩处理,网上找的方法大都使用了 com.sun.image.codec.jpeg.* 这个包中的JPEGImageEncoder类,引入这个包后一直报错,各种google百度,尝试了各种方法,包括手动引jre中的rt.jar,以及在eclipse ...

最近做项目需要图片压缩处理,网上找的方法大都使用了 com.sun.image.codec.jpeg.* 这个包中的JPEGImageEncoder类,引入这个包后一直报错,各种google百度,尝试了各种方法,包括手动引jre中的rt.jar,以及在eclipse中把受访问限制的API提示从ERROR改为WARNING,等等,然而这些都是不好使的,因为后来我发现我的java-7-openjdk-amd64中的rt.jar里边根本就没有com.sun.image.*,貌似这个类在java7中已经被彻底remove了,至少我这个版本是没有了。然后搜了个使用ImageIO类来进行处理的替代方案,代码如下:
可以压缩为任意大小,压缩后高清,不变形(留白),可以改后缀名,可以修改压缩分辨率。
可能有朋友也有这个需要,参考一下吧,有问题还请指证!

  1. package cn.com.images;
  2. import java.awt.Graphics;
  3. import java.awt.Image;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.math.BigDecimal;
  8. import java.math.MathContext;
  9. import java.util.ArrayList;
  10. import javax.imageio.ImageIO;
  11. /***
  12. * 对图片进行操作
  13. *
  14. * @author chenzheng_java
  15. * @since 2011/7/29
  16. *
  17. */
  18. public class ImageHelper {
  19. private static ImageHelper imageHelper = null;
  20. public static ImageHelper getImageHelper() {
  21. if (imageHelper == null) {
  22. imageHelper = new ImageHelper();
  23. }
  24. return imageHelper;
  25. }
  26. /***
  27. * 按指定的比例缩放图片
  28. *
  29. * @param sourceImagePath
  30. * 源地址
  31. * @param destinationPath
  32. * 改变大小后图片的地址
  33. * @param scale
  34. * 缩放比例,如1.2
  35. */
  36. public static void scaleImage(String sourceImagePath,
  37. String destinationPath, double scale,String format) {
  38. File file = new File(sourceImagePath);
  39. BufferedImage bufferedImage;
  40. try {
  41. bufferedImage = ImageIO.read(file);
  42. int width = bufferedImage.getWidth();
  43. int height = bufferedImage.getHeight();
  44. width = parseDoubleToInt(width * scale);
  45. height = parseDoubleToInt(height * scale);
  46. Image image = bufferedImage.getScaledInstance(width, height,
  47. Image.SCALE_SMOOTH);
  48. BufferedImage outputImage = new BufferedImage(width, height,
  49. BufferedImage.TYPE_INT_RGB);
  50. Graphics graphics = outputImage.getGraphics();
  51. graphics.drawImage(image, 0, 0, null);
  52. graphics.dispose();
  53. ImageIO.write(outputImage, format, new File(destinationPath));
  54. } catch (IOException e) {
  55. System.out.println("scaleImage方法压缩图片时出错了");
  56. e.printStackTrace();
  57. }
  58. }
  59. /***
  60. * 将图片缩放到指定的高度或者宽度
  61. * @param sourceImagePath 图片源地址
  62. * @param destinationPath 压缩完图片的地址
  63. * @param width 缩放后的宽度
  64. * @param height 缩放后的高度
  65. * @param auto 是否自动保持图片的原高宽比例
  66. * @param format 图图片格式 例如 jpg
  67. */
  68. public static void scaleImageWithParams(String sourceImagePath,
  69. String destinationPath, int width, int height, boolean auto,String format) {
  70. try {
  71. File file = new File(sourceImagePath);
  72. BufferedImage bufferedImage = null;
  73. bufferedImage = ImageIO.read(file);
  74. if (auto) {
  75. ArrayList<Integer> paramsArrayList = getAutoWidthAndHeight(bufferedImage,width,height);
  76. width = paramsArrayList.get(0);
  77. height = paramsArrayList.get(1);
  78. System.out.println("自动调整比例,width="+width+" height="+height);
  79. }
  80. Image image = bufferedImage.getScaledInstance(width, height,
  81. Image.SCALE_DEFAULT);
  82. BufferedImage outputImage = new BufferedImage(width, height,
  83. BufferedImage.TYPE_INT_RGB);
  84. Graphics graphics = outputImage.getGraphics();
  85. graphics.drawImage(image, 0, 0, null);
  86. graphics.dispose();
  87. ImageIO.write(outputImage, format, new File(destinationPath));
  88. } catch (Exception e) {
  89. System.out.println("scaleImageWithParams方法压缩图片时出错了");
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * 将double类型的数据转换为int,四舍五入原则
  95. *
  96. * @param sourceDouble
  97. * @return
  98. */
  99. private static int parseDoubleToInt(double sourceDouble) {
  100. int result = 0;
  101. result = (int) sourceDouble;
  102. return result;
  103. }
  104. /***
  105. *
  106. * @param bufferedImage 要缩放的图片对象
  107. * @param width_scale 要缩放到的宽度
  108. * @param height_scale 要缩放到的高度
  109. * @return 一个集合,第一个元素为宽度,第二个元素为高度
  110. */
  111. private static ArrayList<Integer> getAutoWidthAndHeight(BufferedImage bufferedImage,int width_scale,int height_scale){
  112. ArrayList<Integer> arrayList = new ArrayList<Integer>();
  113. int width = bufferedImage.getWidth();
  114. int height = bufferedImage.getHeight();
  115. double scale_w =getDot2Decimal( width_scale,width);
  116. System.out.println("getAutoWidthAndHeight width="+width + "scale_w="+scale_w);
  117. double scale_h = getDot2Decimal(height_scale,height);
  118. if (scale_w<scale_h) {
  119. arrayList.add(parseDoubleToInt(scale_w*width));
  120. arrayList.add(parseDoubleToInt(scale_w*height));
  121. }
  122. else {
  123. arrayList.add(parseDoubleToInt(scale_h*width));
  124. arrayList.add(parseDoubleToInt(scale_h*height));
  125. }
  126. return arrayList;
  127. }
  128. /***
  129. * 返回两个数a/b的小数点后三位的表示
  130. * @param a
  131. * @param b
  132. * @return
  133. */
  134. public static double getDot2Decimal(int a,int b){
  135. BigDecimal bigDecimal_1 = new BigDecimal(a);
  136. BigDecimal bigDecimal_2 = new BigDecimal(b);
  137. BigDecimal bigDecimal_result = bigDecimal_1.divide(bigDecimal_2,new MathContext(4));
  138. Double double1 = new Double(bigDecimal_result.toString());
  139. System.out.println("相除后的double为:"+double1);
  140. return double1;
  141. }
  142. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 12:23

Copyright 2015-2025 djqfx

返回顶部