图片压缩工具Thumbnailator的使用

转载:http://blog.csdn.net/cyjch/article/details/51658889


Thumbnailator 是一个为Java界面更流畅的缩略图生成库。从API提供现有的图像文件和图像对象的缩略图中简化了缩略过程,两三行代码就能够从现有图片生成缩略图,且允许微调缩略图生成,同时保持了需要写入到最低限度的代码量。同时还支持根据一个目录批量生成缩略图。

http://code.google.com/p/thumbnailator/

版本:thumbnailator-0.4.8.jar

package com.zspr.utils.img;

import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 图片工具
 * @author jichao
 *
 */
public class Pic {

	/**
	 * 指定大小进行缩放
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param width 宽
	 * @param height 高
	 * @throws IOException
	 */
	public static void resize(String srcUrl,String targetUrl,int width,int height) throws IOException {
		/*
		 * size(width,height) 若图片横比200小,高比300小,不变
		 * 若图片横比200小,高比300大,高缩小到300,图片比例不变 若图片横比200大,高比300小,横缩小到200,图片比例不变
		 * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300
		 */
		Thumbnails.of(srcUrl).size(width, height).toFile(targetUrl);
	}
	
	
	/**
	 * 按照比例进行缩放
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param num 质量比例如 0.8
	 * @throws IOException
	 */
	public static void scale(String srcUrl,String targetUrl,double num) throws IOException {
		Thumbnails.of(srcUrl).scale(num).toFile(targetUrl );
	}
	
	/**
	 * 水印
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param width 宽
	 * @param height 高
	 * @param num 质量比例如 0.8
	 * @param pos 显示位置:  Positions.BOTTOM_RIGHT  
	 * @throws IOException
	 */
	public static void watermark(String srcUrl,String targetUrl,int width,int height,float num,Positions pos) throws IOException {
		Thumbnails.of(srcUrl).size(width,height).watermark(pos,ImageIO.read(new File(targetUrl)), num).outputQuality(num).toFile(targetUrl);
	}
	
	/**
	 * 裁剪
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param width 宽
	 * @param height 高
	 * @param pos 显示位置:  Positions.BOTTOM_RIGHT  
	 * @param x 区域宽度 
	 * @param y 区域高度
	 * @throws IOException
	 */
	public static void cut(String srcUrl,String targetUrl,int width,int height,Positions pos,int x,int y)throws IOException {
		Thumbnails.of(srcUrl).sourceRegion(pos,x,y).size(width, height).keepAspectRatio(false).toFile(targetUrl);
	}
	
	
	/**
	 * 裁剪--指定坐标/大小
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param width 宽
	 * @param height 高
	 * @param pointA_1 坐标A1 
	 * @param pointA_2坐标A2 
	 * @param pointB_1坐标B1
	 * @param pointB_2坐标B2
	 * @throws IOException
	 */
	public static void cut(String srcUrl,String targetUrl,int width,int height,int pointA_1,int pointA_2,int pointB_1,int pointB_2) throws IOException {
		Thumbnails.of(srcUrl).sourceRegion(pointA_1, pointA_2, pointB_1, pointB_2).size(width, height).keepAspectRatio(false).toFile(targetUrl);
	}
	
	/**
	 * 转化图像格式
	 * @param srcUrl 源图片地址
	 * @param targetUrl 目标图片地址
	 * @param width 宽
	 * @param height 高
	 * @param format 格式 如png/gif/jpg
	 * @throws IOException
	 */
	public static void format(String srcUrl,String targetUrl,int width,int height,String format) throws IOException {
		Thumbnails.of(srcUrl).size(width, height).outputFormat(format).toFile(targetUrl);
	}
}
<dependency>
		    <groupId>net.coobird</groupId>
			<artifactId>thumbnailator</artifactId>
			<version>0.4.8</version>
	    </dependency>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值