1. import java.awt.Image;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6.  
  7. import com.sun.image.codec.jpeg.JPEGCodec;
  8. import com.sun.image.codec.jpeg.JPEGImageEncoder;
  9.  
  10. /**
  11. * 制作图片缩略图
  12. *
  13. * @author seawind
  14. *
  15. */
  16. public class PicUtils {
  17. private String srcFile;
  18. private String destFile;
  19. private int width;
  20. private int height;
  21. private Image img;
  22.  
  23. public static void main(String[] args) throws IOException {
  24. PicUtils picUtils1 = new PicUtils(
  25. "WebRoot/upload/5/15/7e46c2f9-534c-4a8d-9e3b-4058f18c1429.jpg");
  26. PicUtils picUtils2 = new PicUtils(
  27. "WebRoot/upload/7/12/571a5dfb-237e-4539-840f-5c59be0c5d93.jpg");
  28.  
  29. picUtils1.resize(100, 100);
  30. picUtils2.resize(100, 100);
  31. }
  32.  
  33. /**
  34. * 构造函数
  35. *
  36. * @param fileName
  37. * String
  38. * @throws IOException
  39. */
  40. public PicUtils(String fileName) throws IOException {
  41. File _file = new File(fileName); // 读入文件
  42. this.srcFile = fileName;
  43. // 查找最后一个.
  44. int index = this.srcFile.lastIndexOf(".");
  45. String ext = this.srcFile.substring(index);
  46. this.destFile = this.srcFile.substring(0, index) + "_s" + ext;
  47. img = javax.imageio.ImageIO.read(_file); // 构造Image对象
  48. width = img.getWidth(null); // 得到源图宽
  49. height = img.getHeight(null); // 得到源图长
  50. }
  51.  
  52. /**
  53. * 强制压缩/放大图片到固定的大小
  54. *
  55. * @param w
  56. * int 新宽度
  57. * @param h
  58. * int 新高度
  59. * @throws IOException
  60. */
  61. public void resize(int w, int h) throws IOException {
  62. BufferedImage _image = new BufferedImage(w, h,
  63. BufferedImage.TYPE_INT_RGB);
  64. _image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
  65. FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
  66. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  67. encoder.encode(_image); // 近JPEG编码
  68. out.close();
  69. }
  70.  
  71. /**
  72. * 按照固定的比例缩放图片
  73. *
  74. * @param t
  75. * double 比例
  76. * @throws IOException
  77. */
  78. public void resize(double t) throws IOException {
  79. int w = (int) (width * t);
  80. int h = (int) (height * t);
  81. resize(w, h);
  82. }
  83.  
  84. /**
  85. * 以宽度为基准,等比例放缩图片
  86. *
  87. * @param w
  88. * int 新宽度
  89. * @throws IOException
  90. */
  91. public void resizeByWidth(int w) throws IOException {
  92. int h = (int) (height * w / width);
  93. resize(w, h);
  94. }
  95.  
  96. /**
  97. * 以高度为基准,等比例缩放图片
  98. *
  99. * @param h
  100. * int 新高度
  101. * @throws IOException
  102. */
  103. public void resizeByHeight(int h) throws IOException {
  104. int w = (int) (width * h / height);
  105. resize(w, h);
  106. }
  107.  
  108. /**
  109. * 按照最大高度限制,生成最大的等比例缩略图
  110. *
  111. * @param w
  112. * int 最大宽度
  113. * @param h
  114. * int 最大高度
  115. * @throws IOException
  116. */
  117. public void resizeFix(int w, int h) throws IOException {
  118. if (width / height > w / h) {
  119. resizeByWidth(w);
  120. } else {
  121. resizeByHeight(h);
  122. }
  123. }
  124.  
  125. /**
  126. * 设置目标文件名 setDestFile
  127. *
  128. * @param fileName
  129. * String 文件名字符串
  130. */
  131. public void setDestFile(String fileName) throws Exception {
  132. if (!fileName.endsWith(".jpg")) {
  133. throw new Exception("Dest File Must end with \".jpg\".");
  134. }
  135. destFile = fileName;
  136. }
  137.  
  138. /**
  139. * 获取目标文件名 getDestFile
  140. */
  141. public String getDestFile() {
  142. return destFile;
  143. }
  144.  
  145. /**
  146. * 获取图片原始宽度 getSrcWidth
  147. */
  148. public int getSrcWidth() {
  149. return width;
  150. }
  151.  
  152. /**
  153. * 获取图片原始高度 getSrcHeight
  154. */
  155. public int getSrcHeight() {
  156. return height;
  157. }
  158. }

使用案例:

// 添加自动生成小图 代码

PicUtils picUtils = new PicUtils(getServletContext().getRealPath("/upload" + randomDir)+ "/" + uuidname);

picUtils.resize(100, 100);

制作缩略图java工具类的更多相关文章

  1. java工具类系列 (四.SerializationUtils)

    java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...

  2. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  3. 排名前 16 的 Java 工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  4. 排名前16的Java工具类

    原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...

  5. 第一章 Java工具类目录

    在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...

  6. java工具类之按对象中某属性排序

    import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...

  7. 干货:排名前16的Java工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  8. Java工具类:给程序增加版权信息

       我们九天鸟的p2p网贷系统,基本算是开发完成了.   现在,想给后端的Java代码,增加版权信息.   手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...

  9. 常用高效 Java 工具类总结

    一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...

随机推荐

  1. jQuery使用(三):DOM操作之val()方法操作表单元素value值

    操作表单元素属性value的值 <form action="./" method='GET'> <h3 >选择你喜欢的明星</h3> <s ...

  2. 五.HashTable原理及实现学习总结

    有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相对应的valu ...

  3. JDK8新特性02 Lambda表达式02_Lambda语法规则

    //函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...

  4. Python 生成requirement 使用requirements.txt安装类库

    快速生成requirement.txt的安装文件 (CenterDesigner) xinghe@xinghe:~/PycharmProjects/CenterDesigner$ pip freeze ...

  5. MySql数据库学习笔记(1)

    MySql数据库 下载地址 https://dev.mysql.com/downloads/mysql/5.1.html#downloads 连接到本机上的MYSQL mysql -u root -p ...

  6. 通过命令窗口控制mysql服务的启动与停止

    mysql服务的启动: 以管理员的身份运行cmd命令窗口,输入命名 net start mysql 如果不是以管理员的身份运行cmd,会提示如下错误 mysql服务的停止: 以管理员的身份运行cmd命 ...

  7. Protobuf学习

    https://www.jianshu.com/p/2265f56805fa https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.ht ...

  8. Thymeleaf在前台下拉列表获取后台传的值

    Thymeleaf在前台下拉列表获取后台传的值 后台添加代码: /** * 新增机构 */ @GetMapping("/add") public String add(ModelM ...

  9. 20165221 JAVA第一周学习心得及体会

    JAVA入门的理论学习 在JAVA2使用教程的网课学中,分为以下几个模块讲解的 JAVA的地位 JAVA的特点 安装JDK(Java Develepement Kit) Java程序的开发步骤 简单的 ...

  10. centos安装fish shell

    对于 CentOS 7,请以根用户 root 运行下面命令: cd /etc/yum.repos.d/ wget https://download.opensuse.org/repositories/ ...