ImageZipUtil.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.lsw.commons.utils;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.IOException;
  9. import javax.imageio.ImageIO;
  10. public class ImageZipUtil {
  11. /**
  12. * 根据设置的宽高等比例压缩图片文件<br>
  13. * 先保存原文件,再压缩、上传
  14. *
  15. * @param oldFile
  16. * 要进行压缩的文件
  17. * @param newFile
  18. * 新文件
  19. * @return 返回压缩后的文件的全路径
  20. */
  21. public static String zipImageFile(File oldFile, File newFile) {
  22. if (oldFile == null) {
  23. return null;
  24. }
  25. try {
  26. /** 对服务器上的临时文件进行处理 */
  27. Image srcFile = ImageIO.read(oldFile);
  28. int w = srcFile.getWidth(null);
  29. int h = srcFile.getHeight(null);
  30. int w1=w;
  31. int h1=h;
  32. if(w<800&&w>200){
  33. w1=(int) (w-w*0.3);
  34. h1=(int) (h-h*0.3);
  35. }else if (w<1600&&w>800) {
  36. w1=(int) (w-w*0.4);
  37. h1=(int) (h-h*0.4);
  38. }else if (w<2500&&w>1600) {
  39. w1=(int) (w-w*0.45);
  40. h1=(int) (h-h*0.45);
  41. }else if (w<4100&&w>2500) {
  42. w1=(int) (w-w*0.5);
  43. h1=(int) (h-h*0.5);
  44. }
  45. String srcImgPath = newFile.getAbsoluteFile().toString();
  46. String subfix = "jpg";
  47. subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());
  48. BufferedImage buffImg = null;
  49. if (subfix.equals("png")) {
  50. buffImg = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);
  51. } else {
  52. buffImg = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
  53. }
  54. Graphics2D graphics = buffImg.createGraphics();
  55. graphics.setBackground(new Color(255, 255, 255));
  56. graphics.setColor(new Color(255, 255, 255));
  57. graphics.fillRect(0, 0, w1, h1);
  58. graphics.drawImage(srcFile.getScaledInstance(w1, h1, Image.SCALE_SMOOTH), 0, 0, null);
  59. ImageIO.write(buffImg, subfix, new File(srcImgPath));
  60. } catch (FileNotFoundException e) {
  61. e.printStackTrace();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. return newFile.getAbsolutePath();
  66. }
  67. /**
  68. * 根据设置的宽高等比例压缩图片文件<br>
  69. * 先保存原文件,再压缩、上传
  70. *
  71. * @param oldFile
  72. * 要进行压缩的文件
  73. * @param newFile
  74. * 新文件
  75. * @return 返回压缩后的文件的全路径
  76. */
  77. public static String zipImageFile(File oldFile, File newFile,double display) {
  78. if (oldFile == null) {
  79. return null;
  80. }
  81. try {
  82. /** 对服务器上的临时文件进行处理 */
  83. Image srcFile = ImageIO.read(oldFile);
  84. int w = srcFile.getWidth(null);
  85. int h = srcFile.getHeight(null);
  86. int w1=w;
  87. int h1=h;
  88. w1=(int) (w-w*display);
  89. h1=(int) (h-h*display);
  90. String srcImgPath = newFile.getAbsoluteFile().toString();
  91. String subfix = "jpg";
  92. subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());
  93. BufferedImage buffImg = null;
  94. if (subfix.equals("png")) {
  95. buffImg = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_ARGB);
  96. } else {
  97. buffImg = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB);
  98. }
  99. Graphics2D graphics = buffImg.createGraphics();
  100. graphics.setBackground(new Color(255, 255, 255));
  101. graphics.setColor(new Color(255, 255, 255));
  102. graphics.fillRect(0, 0, w1, h1);
  103. graphics.drawImage(srcFile.getScaledInstance(w1, h1, Image.SCALE_SMOOTH), 0, 0, null);
  104. ImageIO.write(buffImg, subfix, new File(srcImgPath));
  105. } catch (FileNotFoundException e) {
  106. e.printStackTrace();
  107. } catch (IOException e) {
  108. e.printStackTrace();
  109. }
  110. return newFile.getAbsolutePath();
  111. }
  112. /**
  113. * 按设置的宽度高度压缩图片文件<br>
  114. * 先保存原文件,再压缩、上传
  115. *
  116. * @param oldFile
  117. * 要进行压缩的文件全路径
  118. * @param newFile
  119. * 新文件
  120. * @param width
  121. * 宽度
  122. * @param height
  123. * 高度
  124. * @param quality
  125. * 质量
  126. * @return 返回压缩后的文件的全路径
  127. */
  128. public static String zipWidthHeightImageFile(File oldFile, File newFile, int width, int height, float quality) {
  129. if (oldFile == null) {
  130. return null;
  131. }
  132. String newImage = null;
  133. try {
  134. /** 对服务器上的临时文件进行处理 */
  135. Image srcFile = ImageIO.read(oldFile);
  136. String srcImgPath = newFile.getAbsoluteFile().toString();
  137. System.out.println(srcImgPath);
  138. String subfix = "jpg";
  139. subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".") + 1, srcImgPath.length());
  140. BufferedImage buffImg = null;
  141. if (subfix.equals("png")) {
  142. buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  143. } else {
  144. buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  145. }
  146. Graphics2D graphics = buffImg.createGraphics();
  147. graphics.setBackground(new Color(255, 255, 255));
  148. graphics.setColor(new Color(255, 255, 255));
  149. graphics.fillRect(0, 0, width, height);
  150. graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
  151. ImageIO.write(buffImg, subfix, new File(srcImgPath));
  152. } catch (FileNotFoundException e) {
  153. e.printStackTrace();
  154. } catch (IOException e) {
  155. e.printStackTrace();
  156. }
  157. return newImage;
  158. }
  159. }