支持将Image的宽度、高度缩放到指定width、height,并保存在指定目录
通过目标对象的大小和标准(指定)大小计算出图片缩小的比例

可以设置图片缩放质量,并且可以根据指定的宽高缩放图片

package com.iflashbuy.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* @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")));}
}

缩放图片工具类,创建缩略图、伸缩图片比例相关推荐

  1. 常用工具类五 Excel转图片工具类

    市面上大多数excel转图片为收费工具,借鉴他人用awt的Graphics2D自己实现的工具类,只涉及poi依赖. /** * 版权: taylor * 描述: 将excel转为图片工具类 * 创建时 ...

  2. java图片缩放工具类,一个JAVA图形缩放处置工具类

    一个JAVA图形缩放处理工具类 调用的例子 import java.io.FileOutputStream; import java.io.IOException; import javax.imag ...

  3. Java之png图片工具类

    import java.awt.Graphics2D; import java.awt.Image; import java.awt.Transparency; import java.awt.ima ...

  4. 打印多张分页图片工具类

    工具类创建,可以直接复制粘贴 /// <summary>/// 获取图片打印图片/// </summary>public class PrintImage{PrintDocum ...

  5. 上传PDF文件转换图片工具类

    一,pdf图片转换工具 import org.apache.http.entity.ContentType; import org.apache.pdfbox.pdmodel.PDDocument; ...

  6. 如何使用zip工具类打包下载压缩图片?

    使用zip工具类打包下载压缩图片? 最近工作遇到一需求,让我把用户想要的图片下载下来,并打包成压缩包.当用户没选择时,就下载所有的图片.由于感觉很有意思,便做一下学习总计. 首先说一下,制作的思路.前 ...

  7. Java 旋转、翻转图片工具类(附代码) | Java工具类

    目录 前言 Maven依赖 代码 总结 前言 本文提供java操作图片生成旋转.翻转后的图片工具类,拿来即用. Maven依赖 <dependency><groupId>com ...

  8. Java旋转图片工具类

    前言: 本文提供可以任意角度(最小精度1度),旋转图片的Java工具类,旋转后的图片不会失真或丢失边角,根据角度参数,重新绘制图片,达到无损旋转的目的. 一.图片工具类 import java.awt ...

  9. excel转图片工具类

    添加依赖 <repository><id>AsposeJavaAPI</id><name>Aspose Java API</name>< ...

最新文章

  1. python中国余数定理_Python实现的中国剩余定理算法示例
  2. java $.getjson_JQuery 获取json数据$.getJSON方法的实例代码
  3. 数独高阶技巧入门之四:简单异数链
  4. 文字打印机 效果实现
  5. P6348 [PA2011]Journeys 线段树优化建图 区间连区间
  6. Python数据结构常见的八大排序算法(详细整理)
  7. 查询端口被什么程序占用及停止的方法及netstat的妙用
  8. Android6.0动态权限
  9. Keras学习---RNN模型建立篇
  10. 《mysql必知必会》学习_第八章_20180730_欢
  11. python处理卫星云图获取亮温值
  12. E盾网络验证企业版个人版离线版易语言源码加密对接好的自绘界面1
  13. mysql封机器码,lol机器码解除(同理支持市面上任意一款游戏)解机器码
  14. DiskPart介绍
  15. 仪表研发工程师所需要了解的蓝牙和WiFi知识
  16. 剑指Offer_46 把数字翻译成字符串
  17. 修改植物大战僵尸游戏存档(Java实现版)
  18. 真的羡慕玉自寒和烈如歌的爱情
  19. 快速搞懂htpp原理
  20. uniaccess安全助手卸载

热门文章

  1. javascript 倒计时工具
  2. 追本溯源 - 勿忘质量之本
  3. ffmpeg教程-手把手教你如何简单快捷处理音视屏
  4. 第一个简单Python爬虫:抓取古诗文网中李白的诗歌
  5. 常见的使用水平集函数的零水平集表示的曲面汇总(3D)(MATLAB 画三维图)
  6. ccf 201703-3 Markdown ( java)
  7. 南京大学计算机系校庆,南京大学建校100周年庆典邀请函
  8. 计算机主机箱上是什么问题,电脑机箱常见问题有哪些?电脑机箱常见问题的解决方法...
  9. Java是值传递还是引用传递
  10. C++编译错误 :error: explicit qualification in declaration of xxx