Thumbnailator生成缩略图--封装笔记

Thumbnailator--封装笔记


package com.bobonew.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 
 * @author czz
 * @data 2017年3月22日 上午10:11:57
 */
public class ThumbnailsUtil {
	
	public static void main(String[] args) {
		//C:\Users\weixj\Desktop\aaa1.jpg
		String img = "C:/Users/weixj/Desktop/aaa1.jpg";
		String mark = "C:/Users/weixj/Desktop/icon.png";
		
		
		SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMddHHmmssSSS" );
   		Date d=new Date();
   		String str=sdf.format(d);
   		String outFilepath = "D:/"+str+".jpg";
   		
   		shrinkToSize(80,80,img,outFilepath);
   		//shrinkToScale(0.5,img, outFilepath);
		//addWatermark(img, mark, outFilepath);
	}
	
	/** 
	 * 指定大小进行缩放   
	 * @param width
	 * @param height
	 * @param img
	 * @param outFilepath
	 * @param keepAspectRatio    
	 */
	public static void shrinkToSize(int width,int height,String img,String outFilepath){
		try {
			Thumbnails.of(img)
			.size(width, height)
			.keepAspectRatio(false)
			.toFile(outFilepath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 按照比例进行缩放 
	 * @param scale
	 * @param img
	 * @param outFilepath
	 */
	public static void shrinkToScale(double scale,String img,String outFilepath){
		try {
			Thumbnails.of(img)
			.scale(scale)
			.toFile(outFilepath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 水印 
	 * @param width
	 * @param height
	 * @param img
	 * @param watermarkIMG
	 * @param outFilepath
	 */
	public static void addWatermark(int width,int height,String img,String watermarkIMG,String outFilepath){
		//watermark(位置,水印图,透明度)  
		try {
			Thumbnails.of(img)   
			.size(width, height)  
			.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermarkIMG)), 0.5f)   
			.outputQuality(0.8f)   
			.toFile(outFilepath);
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 水印 
	 * @param img
	 * @param watermarkIMG
	 * @param outFilepath
	 */
	public static void addWatermark(String img,String watermarkIMG,String outFilepath){
		//watermark(位置,水印图,透明度)  
		try {
			Thumbnails.of(img)
			.scale(1f)
			.watermark(Positions.TOP_RIGHT, ImageIO.read(new File(watermarkIMG)), 0.5f)   
			.outputQuality(0.8f)   
			.toFile(outFilepath);
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 旋转
	 * //rotate(角度),正数:顺时针 负数:逆时针  
	 * @param scale
	 * @param img
	 * @param outFilepath
	 */
	public static void retateImg(double scale,double angle,String img,String outFilepath){
		try {
			Thumbnails.of(img)
			.scale(scale)
			.rotate(angle)
			.toFile(outFilepath);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值