- 浏览: 3361824 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
http://my.oschina.net/backtract/blog/403828
支持将Image的宽度、高度缩放到指定width、height,并保存在指定目录
通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
可以设置图片缩放质量,并且可以根据指定的宽高缩放图片
支持将Image的宽度、高度缩放到指定width、height,并保存在指定目录
通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
可以设置图片缩放质量,并且可以根据指定的宽高缩放图片
package com.hoo.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.ImageFormatException; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; /** * <b>function:</b> 缩放图片工具类,创建缩略图、伸缩图片比例 * @author hoojo * @createDate 2012-2-3 上午10:08:47 * @file ScaleImageUtils.java * @package com.hoo.util * @blog http://blog.csdn.net/IBM_hoojo * http://hoojo.cnblogs.com * @email hoojo_@126.com * @version 1.0 */ public abstract class ScaleImageUtils { private static final float DEFAULT_SCALE_QUALITY = 1f; private static final String DEFAULT_IMAGE_FORMAT = ".jpg"; // 图像文件的格式 private static final String DEFAULT_FILE_PATH = "C:/temp-"; /** * <b>function:</b> 设置图片压缩质量枚举类; * Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality * @author hoojo * @createDate 2012-2-7 上午11:31:45 * @file ScaleImageUtils.java * @package com.hoo.util * @project JQueryMobile * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public enum ImageQuality { max(1.0f), high(0.75f), medium(0.5f), low(0.25f); private Float quality; public Float getQuality() { return this.quality; } ImageQuality(Float quality) { this.quality = quality; } } private static Image image; /** * <b>function:</b> 通过目标对象的大小和标准(指定)大小计算出图片缩小的比例 * @author hoojo * @createDate 2012-2-6 下午04:41:48 * @param targetWidth 目标的宽度 * @param targetHeight 目标的高度 * @param standardWidth 标准(指定)宽度 * @param standardHeight 标准(指定)高度 * @return 最小的合适比例 */ public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) { double widthScaling = 0d; double heightScaling = 0d; if (targetWidth > standardWidth) { widthScaling = standardWidth / (targetWidth * 1.00d); } else { widthScaling = 1d; } if (targetHeight > standardHeight) { heightScaling = standardHeight / (targetHeight * 1.00d); } else { heightScaling = 1d; } return Math.min(widthScaling, heightScaling); } /** * <b>function:</b> 将Image的宽度、高度缩放到指定width、height,并保存在savePath目录 * @author hoojo * @createDate 2012-2-6 下午04:54:35 * @param width 缩放的宽度 * @param height 缩放的高度 * @param savePath 保存目录 * @param targetImage 即将缩放的目标图片 * @return 图片保存路径、名称 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, int height, String savePath, Image targetImage) throws ImageFormatException, IOException { width = Math.max(width, 1); height = Math.max(height, 1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(targetImage, 0, 0, width, height, null); if (savePath == null || "".equals(savePath)) { savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT; } FileOutputStream fos = new FileOutputStream(new File(savePath)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); encoder.encode(image); image.flush(); fos.flush(); fos.close(); return savePath; } /** * <b>function:</b> 可以设置图片缩放质量,并且可以根据指定的宽高缩放图片 * @author hoojo * @createDate 2012-2-7 上午11:01:27 * @param width 缩放的宽度 * @param height 缩放的高度 * @param quality 图片压缩质量,最大值是1; 使用枚举值:{@link ImageQuality} * Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality * @param savePath 保存目录 * @param targetImage 即将缩放的目标图片 * @return 图片保存路径、名称 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws ImageFormatException, IOException { width = Math.max(width, 1); height = Math.max(height, 1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(targetImage, 0, 0, width, height, null); if (savePath == null || "".equals(savePath)) { savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT; } FileOutputStream fos = new FileOutputStream(new File(savePath)); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos); JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam(image); if (quality == null || quality <= 0) { quality = DEFAULT_SCALE_QUALITY; } /** 设置图片压缩质量 */ encodeParam.setQuality(quality, true); encoder.encode(image, encodeParam); image.flush(); fos.flush(); fos.close(); return savePath; } /** * <b>function:</b> 通过指定大小和图片的大小,计算出图片缩小的合适大小 * @author hoojo * @createDate 2012-2-6 下午05:53:10 * @param width 指定的宽度 * @param height 指定的高度 * @param image 图片文件 * @return 返回宽度、高度的int数组 */ public static int[] getSize(int width, int height, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); double scaling = getScaling(targetWidth, targetHeight, width, height); long standardWidth = Math.round(targetWidth * scaling); long standardHeight = Math.round(targetHeight * scaling); return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) }; } /** * <b>function:</b> 通过指定的比例和图片对象,返回一个放大或缩小的宽度、高度 * @author hoojo * @createDate 2012-2-7 上午10:27:59 * @param scale 缩放比例 * @param image 图片对象 * @return 返回宽度、高度 */ public static int[] getSize(float scale, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); long standardWidth = Math.round(targetWidth * scale); long standardHeight = Math.round(targetHeight * scale); return new int[] { Integer.parseInt(Long.toString(standardWidth)), Integer.parseInt(String.valueOf(standardHeight)) }; } public static int[] getSize(int width, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); long height = Math.round((targetHeight * width) / (targetWidth * 1.00f)); return new int[] { width, Integer.parseInt(String.valueOf(height)) }; } public static int[] getSizeByHeight(int height, Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); long width = Math.round((targetWidth * height) / (targetHeight * 1.00f)); return new int[] { Integer.parseInt(String.valueOf(width)), height }; } /** * * <b>function:</b> 将指定的targetFile图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savePath目录 * @author hoojo * @createDate 2012-2-6 下午04:57:02 * @param width 缩小的宽度 * @param height 缩小的高度 * @param savePath 保存目录 * @param targetImage 改变的目标图片 * @return 图片保存路径、名称 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, int height, String savePath, File targetFile) throws ImageFormatException, IOException { image = ImageIO.read(targetFile); int[] size = getSize(width, height, image); return resize(size[0], size[1], savePath, image); } /** * * <b>function:</b> 将指定的targetURL网络图片文件的宽度、高度大于指定width、height的图片缩小,并保存在savePath目录 * @author hoojo * @createDate 2012-2-6 下午04:57:07 * @param width 缩小的宽度 * @param height 缩小的高度 * @param savePath 保存目录 * @param targetImage 改变的目标图片 * @return 图片保存路径、名称 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, int height, String savePath, URL targetURL) throws ImageFormatException, IOException { image = ImageIO.read(targetURL); int[] size = getSize(width, height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 将一个本地的图片文件按照指定的比例进行缩放 * @author hoojo * @createDate 2012-2-7 上午10:29:18 * @param scale 缩放比例 * @param savePath 保存文件路径、名称 * @param targetFile 本地图片文件 * @return 新的文件名称 * @throws ImageFormatException * @throws IOException */ public static String resize(float scale, String savePath, File targetFile) throws ImageFormatException, IOException { image = ImageIO.read(targetFile); int[] size = getSize(scale, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 将一个网络图片文件按照指定的比例进行缩放 * @author hoojo * @createDate 2012-2-7 上午10:30:56 * @param scale 缩放比例 * @param savePath 保存文件路径、名称 * @param targetFile 本地图片文件 * @return 新的文件名称 * @throws ImageFormatException * @throws IOException */ public static String resize(float scale, String savePath, URL targetURL) throws ImageFormatException, IOException { image = ImageIO.read(targetURL); int[] size = getSize(scale, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定宽度进行等比缩放本地图片 * @author hoojo * @createDate 2012-2-7 上午10:49:56 * @param width 固定宽度 * @param savePath 保存路径、名称 * @param targetFile 本地目标文件 * @return 返回保存路径 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, String savePath, File targetFile) throws ImageFormatException, IOException { image = ImageIO.read(targetFile); int[] size = getSize(width, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定宽度进行等比缩放网络图片 * @author hoojo * @createDate 2012-2-7 上午10:50:52 * @param width 固定宽度 * @param savePath 保存路径、名称 * @param targetFile 本地目标文件 * @return 返回保存路径 * @throws ImageFormatException * @throws IOException */ public static String resize(int width, String savePath, URL targetURL) throws ImageFormatException, IOException { image = ImageIO.read(targetURL); int[] size = getSize(width, image); return resize(size[0], size[1], savePath, image); } /** * * <b>function:</b> 按照固定高度进行等比缩放本地图片 * @author hoojo * @createDate 2012-2-7 上午10:51:17 * @param height 固定高度 * @param savePath 保存路径、名称 * @param targetFile 本地目标文件 * @return 返回保存路径 * @throws ImageFormatException * @throws IOException */ public static String resizeByHeight(int height, String savePath, File targetFile) throws ImageFormatException, IOException { image = ImageIO.read(targetFile); int[] size = getSizeByHeight(height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定高度进行等比缩放网络图片 * @author hoojo * @createDate 2012-2-7 上午10:52:23 * @param height 固定高度 * @param savePath 保存路径、名称 * @param targetFile 本地目标文件 * @return 返回保存路径 * @throws ImageFormatException * @throws IOException */ public static String resizeByHeight(int height, String savePath, URL targetURL) throws ImageFormatException, IOException { image = ImageIO.read(targetURL); int[] size = getSizeByHeight(height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> * @author hoojo * @createDate 2012-2-3 上午10:08:47 * @param args * @throws IOException * @throws MalformedURLException * @throws ImageFormatException */ public static void main(String[] args) throws ImageFormatException, MalformedURLException, IOException { System.out.println(ScaleImageUtils.resize(140, 140, null, new URL("http://www.open-open.com/lib/images/logo.jpg"))); ScaleImageUtils.resize(100, 100, ImageQuality.high.getQuality(), null, ImageIO.read(new URL("http://www.open-open.com/lib/images/logo.jpg"))); } }
发表评论
-
Java Comparable和Comparator
2016-06-26 08:52 654http://my.oschina.net/android52 ... -
Java集合框架之fastutil & koloboke
2016-06-23 14:04 2409Java集合框架之fastutil http://rensan ... -
ehcache 分布式支持
2016-06-05 22:26 1055原文 http://my.oschina.net/glenxu ... -
Intellij IDEA插件开发入门
2016-05-26 11:42 2839原文: http://blog.csdn.net/dc_726 ... -
阿里巴巴Druid数据源的配置与使用
2016-05-24 17:42 1494http://my.oschina.net/wjme/blog ... -
mysql中间件研究(Atlas,cobar,TDDL), 分库分表插件
2016-05-09 14:15 3387http://www.guokr.com/blog/47576 ... -
Java集合: Queue和Deque
2016-05-09 09:49 1825Queue http://my.oschina.net/kev ... -
使用gzip优化web应用(filter实现)
2016-05-07 01:45 997使用gzip优化web应用(filter实现) http:// ... -
Byteman 3.0.5 发布,Java 字节码注入工具
2016-04-23 10:29 1723Byteman 3.0.5 发布,Java 字 ... -
RandomStringUtils的说明和生成随机汉字
2016-04-20 15:21 1315更多参考: http://my.oschina.net/wil ... -
通过IP地址获取地理位置
2016-04-20 15:19 854http://my.oschina.net/githubhty ... -
Java编程中使用正则表达式过滤非数字字符串
2016-04-14 13:51 1673/** * * @param str ... -
非对称加密DH算法,DH代码实现
2016-04-13 11:33 1315RSA算法原理(一)http:// ... -
企业支付宝账号开发接口教程
2016-03-31 14:52 1192企业支付宝账号开发接口教程--JAVA-UTF-8(实际操作- ... -
java double类型数据操作工具类
2016-03-28 17:36 1176http://my.oschina.net/yxwblog/b ... -
double转换到BigDecimal
2016-03-28 17:11 1502BigDecimal b = new BigDecimal(d ... -
Java 生成好看的验证码
2016-03-23 10:52 3303http://www.oschina.net/code/sni ... -
Linux环境安装配置Swftools
2016-03-22 21:01 1048http://tetop.blog.51cto.com/188 ... -
java压缩与解压缩文件
2016-03-20 22:03 1428http://www.oschina.net/code/sni ... -
java图像压缩
2016-03-19 23:20 917http://my.oschina.net/686991/bl ...
相关推荐
java裁剪、缩放图片工具类。含工具类和一个测试类。
将图片生成缩略图和转换为二进制的工具类,缩略图大小可调
C# 图片工具类 上传 缩放 裁剪 水印 生成缩略图
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
java上传图片,处理图片,word转pdf图片缩略图生成,替换所有图片的ip地址工具类
图片处理工具类,能缩放图片,给图片打水印等。
java 图片处理工具类,图片的切割、水印、坐标、分辨率。
实现功能: 叠加图片;图片压缩(支持MultipartFile),添加文本–可根据textWidth参数自动换行,支持行间距、字间距设置;...图片高质量缩放;图片png格式缩放;图片克隆副本;读取图片对象;图片输出 ……
一个不错的工具类,上传的图片根据长宽的判断,按比例缩放生成另一张小图,可以减少浏览的浪费
使用java工具类可有效的提高开发效率! 没有CSDN积分的朋友到这里源头下载:http://www.javacs.cn/bbs/thread-382-1-1.html 感谢支持 [工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中...
[工具类] CookieCounter .java.txt [工具类] 验证码img .jsp.txt [工具类] Java中计算任意两个日期之间的工作天数 .java.txt [工具类] java抓取网页 .java.txt [工具类] MD5 .java.txt [工具类] MD5强化版 .java.txt...
jQuery网格缩略图片点击大小缩放图片切换代码 jQuery网格缩略图片点击大小缩放图片切换代码 jQuery网格缩略图片点击大小缩放图片切换代码
NULL 博文链接:https://wenkaixuan.iteye.com/blog/785823
完整的java文件读写工具类 包括读取、创建、删除文件, 检查、创建、删除目录
DDS图片缩略图显示工具DDS_viewer