制作缩略图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万个开源项目源码 ...
随机推荐
- 正则表达式基于JavaScript的入门详解
关于正则表达式,和很多前辈聊起这个知识点时,他们的反馈都比聊其他技术谦逊,而和很多刚入门的程序员讨论时甚至会有觉得你看不起他. 的确,正则表达式从通常的应用来看,的确不难,比如电话,邮箱等验证.语法, ...
- 【转载】C++ getline函数用法
https://www.cnblogs.com/xiaofeiIDO/p/8574042.html 摘要: 通过getline()函数一个小小的实例,那么把getline()函数作为while的判断语 ...
- vertica系列:时间相关函数
-- * 注意: 本文的SQL是在 2017-09-14 测试的. 所以如果取当前日期, 结果为 2017-09-14* ------------------------------ 相关数据类型 - ...
- SQL Server进阶 SQL优化
找到消耗内存最多的SQL SELECT mg.granted_memory_kb, mg.session_id, t.text, qp.query_plan FROM sys.dm_exec_quer ...
- 新SQL temp
select a.createtime, -- 日期 dept.name as deptName, -- 科室 (select t.docname from ( SELECT u.clinic_id ...
- luogu 2878 贪心
其实这题不难,只是想告诉自己:贪心不全是真的脑残拿最大就AC 此题实际上就是比较x,y优先级利用时间计算得到a[i]t/a[i].d(没错时间在上,并非惯性思维的d在上) t*a[x].d+(t+a[ ...
- java实现数据缓存
摘抄自java并发实战 有时候需要对数据缓存.用Map缓存数据比较合适.但是由于对吞吐量,一致性,计算性能的要求,对数据进行缓存的设计还是需要慎重考虑的. 一.利用HashMap加同步 (1)说明 把 ...
- AspectJ开发
aspectJ 是基于java语言的aop框架,提供了强大的aop功能. aspectJ的实现主要有两种,一种是基于xml的声明式aspectJ,另一种是基于注解的aspectJ. 1.基于xml的声 ...
- TensorFlow从入门到理解(二):你的第一个神经网络
运行代码: from __future__ import print_function import tensorflow as tf import numpy as np import matplo ...
- cookie——登录注册极简版
本实例旨在最直观地说明如何利用cookie完成登录注册功能,忽略正则验证. index.html <!doctype html> <html lang="en"& ...