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();
}
}
}