制作缩略图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万个开源项目源码 ...
随机推荐
- [leetcode-108,109] 将有序数组转换为二叉搜索树
109. 有序链表转换二叉搜索树 Given a singly linked list where elements are sorted in ascending order, convert it ...
- Nginx 之六: Nginx服务器的正向及反向代理功能
一:Nginx作为正向代理服务器: 1.正向代理:代理(proxy)服务也可以称为是正向代理,指的是将服务器部署在公司的网关,代理公司内部员工上外网的请求,可以起到一定的安全作用和管理限制作用,正向代 ...
- vue-cli3 第三版安装搭建项目
Vue CLI是一个用于快速Vue.js开发的完整系统 3.X较2.X结构变了很多,更优雅,开发体验更好 官方:https://cli.vuejs.org/guide/ 安装:https://cli. ...
- js强制将页面放到最大
<!DOCTYPE html> <html> <head> <title></title> <script language=&quo ...
- Javaweb学习笔记——(十四)—————— 服务器端验证注册登入表单项目
项目:https://download.csdn.net/download/qq_40223688/10463436 项目 功能: *注册 *登录--------------------------- ...
- 测试四则运算2:Right-BICEP
n6个值得测试的具体部位,他们能够提高你的测试技巧 nRight-结果是否正确? nB-是否所有的边界条件都是正确的? nI-能查一下反向关联吗 nC-能用其他手段交叉检查一下结果吗? nE-你是否可 ...
- PHP+MySql+Bootstrap实现用户界面数据的删除、修改与批量选择删除——实例操作
第一步:在数据库中建立要操作的信息表 如下图: 第二步:实现对该信息表中数据的删除功能 代码如下:main(主页面) <!DOCTYPE html><html> < ...
- luogu P4916 魔力环
传送门 表示这种\(Burnside\)定理之类的东西一用就忘qwq 题目要求不同染色方案数,因为变换方式只有旋转,所以只有\(n\)个置换,然后可能会出现某种方案有循环节,这个循环节长度显然要是\( ...
- Java基础_0205: 程序逻辑结构
使用if语句进行判断 public class TestDemo { public static void main(String args[]) { double score = 90.0; // ...
- gitbash使用git 命令的准备工作
1.git下载 2.git clone http://git.missfresh.cn/... 配置用户名密码 3.git 常用命令简写配置 git config --global alias.st ...