ImageShrink.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package com.cnd3b.utility;
  2. //
  3. //import com.sun.image.codec.jpeg.JPEGCodec;
  4. //import com.sun.image.codec.jpeg.JPEGImageEncoder;
  5. import javax.imageio.ImageIO;
  6. import java.awt.*;
  7. import java.awt.image.BufferedImage;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileOutputStream;
  11. import java.io.InputStream;
  12. /**
  13. * 图片收缩工具
  14. */
  15. public class ImageShrink {
  16. // /**
  17. // * 获取图片宽度和高度
  18. // *
  19. // * @param file 图片路径
  20. // * @return 返回图片的宽度
  21. // */
  22. // private static int[] getImgWidthHeight(File file) {
  23. // InputStream is = null;
  24. // BufferedImage src = null;
  25. // int result[] = {0, 0};
  26. // try {
  27. // // 获得文件输入流
  28. // is = new FileInputStream(file);
  29. // // 从流里将图片写入缓冲图片区
  30. // src = ImageIO.read(is);
  31. // // 得到源图片宽
  32. // result[0] = src.getWidth(null);
  33. // // 得到源图片高
  34. // result[1] = src.getHeight(null);
  35. // is.close(); //关闭输入流
  36. // } catch (Exception ef) {
  37. // ef.printStackTrace();
  38. // }
  39. // return result;
  40. // }
  41. //
  42. // public static void main(String[] args) {
  43. // String url = "C:\\wildfly-9.0.2s\\standalone\\deployments\\pic.war\\DSB\\tarchives\\default\\";
  44. // for (File file : new File(url).listFiles()) {
  45. // String filename = file.getName();
  46. // if (filename.endsWith("jpg") || filename.endsWith("JPG") || filename.endsWith("PNG") || filename.endsWith("png")) {
  47. // ImageShrink.reduceImg(url + filename, url + "show_" + filename);
  48. // }
  49. // }
  50. // }
  51. //
  52. // /**
  53. // * 指定图片宽度和高度和压缩比例对图片进行压缩
  54. // *
  55. // * @param imgsrc 源图片地址
  56. // * @param imgdist 目标图片地址
  57. // */
  58. // private static void reduceImg(String imgsrc, String imgdist) {
  59. // try {
  60. // File srcfile = new File(imgsrc);
  61. // // 检查图片文件是否存在
  62. // if (!srcfile.exists()) {
  63. // System.err.println("文件不存在");
  64. // }
  65. // int[] results = getImgWidthHeight(srcfile);
  66. //
  67. // int widthdist = results[0];
  68. // int heightdist = results[1];
  69. //
  70. //
  71. // float newwidth = 400f;
  72. // float newheight;
  73. // if (widthdist > newwidth) {
  74. // newheight = newwidth * ((float) heightdist / (float) widthdist);
  75. // } else {
  76. // newwidth = widthdist;
  77. // newheight = heightdist;
  78. // }
  79. //
  80. // // 开始读取文件并进行压缩
  81. // Image src = ImageIO.read(srcfile);
  82. //
  83. // // 构造一个类型为预定义图像类型之一的 BufferedImage
  84. // BufferedImage tag = new BufferedImage((int) newwidth, (int) newheight, BufferedImage.TYPE_INT_RGB);
  85. //
  86. // // 这边是压缩的模式设置
  87. // tag.getGraphics().drawImage(src.getScaledInstance((int) newwidth, (int) newheight, Image.SCALE_SMOOTH), 0, 0, null);
  88. //
  89. // //创建文件输出流
  90. // FileOutputStream out = new FileOutputStream(imgdist);
  91. // //将图片按JPEG压缩,保存到out中
  92. // JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  93. // encoder.encode(tag);
  94. // //关闭文件输出流
  95. // out.close();
  96. // } catch (Exception ef) {
  97. // ef.printStackTrace();
  98. // }
  99. // }
  100. }