制作缩略图java工具类
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- /**
- * 制作图片缩略图
- *
- * @author seawind
- *
- */
- public class PicUtils {
- private String srcFile;
- private String destFile;
- private int width;
- private int height;
- private Image img;
- public static void main(String[] args) throws IOException {
- PicUtils picUtils1 = new PicUtils(
- "WebRoot/upload/5/15/7e46c2f9-534c-4a8d-9e3b-4058f18c1429.jpg");
- PicUtils picUtils2 = new PicUtils(
- "WebRoot/upload/7/12/571a5dfb-237e-4539-840f-5c59be0c5d93.jpg");
- picUtils1.resize(100, 100);
- picUtils2.resize(100, 100);
- }
- /**
- * 构造函数
- *
- * @param fileName
- * String
- * @throws IOException
- */
- public PicUtils(String fileName) throws IOException {
- File _file = new File(fileName); // 读入文件
- this.srcFile = fileName;
- // 查找最后一个.
- int index = this.srcFile.lastIndexOf(".");
- String ext = this.srcFile.substring(index);
- this.destFile = this.srcFile.substring(0, index) + "_s" + ext;
- img = javax.imageio.ImageIO.read(_file); // 构造Image对象
- width = img.getWidth(null); // 得到源图宽
- height = img.getHeight(null); // 得到源图长
- }
- /**
- * 强制压缩/放大图片到固定的大小
- *
- * @param w
- * int 新宽度
- * @param h
- * int 新高度
- * @throws IOException
- */
- public void resize(int w, int h) throws IOException {
- BufferedImage _image = new BufferedImage(w, h,
- BufferedImage.TYPE_INT_RGB);
- _image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
- FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(_image); // 近JPEG编码
- out.close();
- }
- /**
- * 按照固定的比例缩放图片
- *
- * @param t
- * double 比例
- * @throws IOException
- */
- public void resize(double t) throws IOException {
- int w = (int) (width * t);
- int h = (int) (height * t);
- resize(w, h);
- }
- /**
- * 以宽度为基准,等比例放缩图片
- *
- * @param w
- * int 新宽度
- * @throws IOException
- */
- public void resizeByWidth(int w) throws IOException {
- int h = (int) (height * w / width);
- resize(w, h);
- }
- /**
- * 以高度为基准,等比例缩放图片
- *
- * @param h
- * int 新高度
- * @throws IOException
- */
- public void resizeByHeight(int h) throws IOException {
- int w = (int) (width * h / height);
- resize(w, h);
- }
- /**
- * 按照最大高度限制,生成最大的等比例缩略图
- *
- * @param w
- * int 最大宽度
- * @param h
- * int 最大高度
- * @throws IOException
- */
- public void resizeFix(int w, int h) throws IOException {
- if (width / height > w / h) {
- resizeByWidth(w);
- } else {
- resizeByHeight(h);
- }
- }
- /**
- * 设置目标文件名 setDestFile
- *
- * @param fileName
- * String 文件名字符串
- */
- public void setDestFile(String fileName) throws Exception {
- if (!fileName.endsWith(".jpg")) {
- throw new Exception("Dest File Must end with \".jpg\".");
- }
- destFile = fileName;
- }
- /**
- * 获取目标文件名 getDestFile
- */
- public String getDestFile() {
- return destFile;
- }
- /**
- * 获取图片原始宽度 getSrcWidth
- */
- public int getSrcWidth() {
- return width;
- }
- /**
- * 获取图片原始高度 getSrcHeight
- */
- public int getSrcHeight() {
- return height;
- }
- }
使用案例:
// 添加自动生成小图 代码
PicUtils picUtils = new PicUtils(getServletContext().getRealPath("/upload" + randomDir)+ "/" + uuidname);
picUtils.resize(100, 100);
制作缩略图java工具类的更多相关文章
- java工具类系列 (四.SerializationUtils)
java工具类系列 (四.SerializationUtils) SerializationUtils该类为序列化工具类,也是lang包下的工具,主要用于序列化操作 import java.io.Se ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
- 排名前 16 的 Java 工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- 排名前16的Java工具类
原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...
- 第一章 Java工具类目录
在这一系列博客中,主要是记录在实际开发中会常用的一些Java工具类,方便后续开发中使用. 以下的目录会随着后边具体工具类的添加而改变. 浮点数精确计算 第二章 Java浮点数精确计算 crc32将任意 ...
- java工具类之按对象中某属性排序
import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang ...
- 干货:排名前16的Java工具类
在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...
- Java工具类:给程序增加版权信息
我们九天鸟的p2p网贷系统,基本算是开发完成了. 现在,想给后端的Java代码,增加版权信息. 手动去copy-paste,太没有技术含量. 于是,写了个Java工具类,给Java源文件 ...
- 常用高效 Java 工具类总结
一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...
随机推荐
- jQuery使用(三):DOM操作之val()方法操作表单元素value值
操作表单元素属性value的值 <form action="./" method='GET'> <h3 >选择你喜欢的明星</h3> <s ...
- 五.HashTable原理及实现学习总结
有两个类都提供了一个多种用途的hashTable机制,他们都可以将可以key和value结合起来构成键值对通过put(key,value)方法保存起来,然后通过get(key)方法获取相对应的valu ...
- JDK8新特性02 Lambda表达式02_Lambda语法规则
//函数式接口:只有一个抽象方法的接口称为函数式接口. 可以使用注解 @FunctionalInterface 修饰 @FunctionalInterface public interface MyF ...
- Python 生成requirement 使用requirements.txt安装类库
快速生成requirement.txt的安装文件 (CenterDesigner) xinghe@xinghe:~/PycharmProjects/CenterDesigner$ pip freeze ...
- MySql数据库学习笔记(1)
MySql数据库 下载地址 https://dev.mysql.com/downloads/mysql/5.1.html#downloads 连接到本机上的MYSQL mysql -u root -p ...
- 通过命令窗口控制mysql服务的启动与停止
mysql服务的启动: 以管理员的身份运行cmd命令窗口,输入命名 net start mysql 如果不是以管理员的身份运行cmd,会提示如下错误 mysql服务的停止: 以管理员的身份运行cmd命 ...
- Protobuf学习
https://www.jianshu.com/p/2265f56805fa https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.ht ...
- Thymeleaf在前台下拉列表获取后台传的值
Thymeleaf在前台下拉列表获取后台传的值 后台添加代码: /** * 新增机构 */ @GetMapping("/add") public String add(ModelM ...
- 20165221 JAVA第一周学习心得及体会
JAVA入门的理论学习 在JAVA2使用教程的网课学中,分为以下几个模块讲解的 JAVA的地位 JAVA的特点 安装JDK(Java Develepement Kit) Java程序的开发步骤 简单的 ...
- centos安装fish shell
对于 CentOS 7,请以根用户 root 运行下面命令: cd /etc/yum.repos.d/ wget https://download.opensuse.org/repositories/ ...