package default;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
/**
* 圖片縮略、裁剪、添加水印工具類
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [相關類/方法]
* @since [產品/模塊版本]
*/
public class ImageThumbUtils {
/** 壓縮質量爲100% */
public static double PERCENT_100 = 1.0;
/**
* 圖片尺寸不變,圖片內存壓縮
* 壓縮圖片文件大小outputQuality實現,參數1爲最高質量,此處0.1f表示壓縮爲原內存的1/10
*
* @param originalImgPath 源圖片路徑
* @param targetImgPath 壓縮後圖片路徑
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void thumbImage(String originalImgPath, String targetImgPath) throws Exception {
Thumbnails.of(originalImgPath).scale(1f).outputQuality(0.1f).toFile(targetImgPath);
}
/**
* 縮略圖片,圖片質量爲源圖的100%
* @param originalImgPath 源圖片存儲路徑
* @param w 圖片壓縮後的寬度
* @param h 圖片壓縮後的高度
* @param targetImgPath 縮略圖的存放路徑
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void thumbImage(String originalImgPath, int w, int h, String targetImgPath) throws Exception {
thumbImage(new FileInputStream(originalImgPath), w, h, targetImgPath, PERCENT_100);
}
/**
* 縮略圖片,圖片質量爲源圖100%
* @param originalImgData 源圖片字節數
* @param w 圖片壓縮後的寬度
* @param h 圖片壓縮後的高度
* @param targetImgPath 縮略圖的存放路徑
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void thumbImage(byte[] originalImgData, int w, int h, String targetImgPath) throws Exception {
thumbImage(new ByteArrayInputStream(originalImgData), w, h, targetImgPath, PERCENT_100);
}
/**
* 按尺寸比例縮略
* @param originalInputSteam 源圖輸入流
* @param w 縮略寬
* @param h 縮略高
* @param targetImgPath 縮略圖存儲路徑
* @param compressQalitiy 縮略質量比例,0~1之間的數
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void thumbImage(InputStream originalInputSteam, int w, int h, String targetImgPath, double compressQalitiy) throws Exception {
thumbImage(originalInputSteam, w, h, targetImgPath, compressQalitiy, true);
}
/**
*
* @param originalImgInputSteam 源圖片輸入流
* @param w 圖片壓縮後的寬度
* @param h 圖片壓縮後的高度
* @param targetImgPath 縮略圖的存放路徑
* @param compressQalitiy 壓縮比例,0~1之間的double數字
* @param keepAspectRatio 是否保持等比例壓縮,是true,不是false
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void thumbImage(InputStream originalImgInputSteam, int w, int h, String targetImgPath, double compressQalitiy, boolean keepAspectRatio) throws Exception {
Thumbnails.of(originalImgInputSteam).size(w, h).outputQuality(Double.valueOf(compressQalitiy)).keepAspectRatio(true).toFile(targetImgPath);
}
/**
* 圖片裁剪
* @param originalImgPath 源圖片路徑
* @param position 位置 0正中間,1中間左邊,2中間右邊,3底部中間,4底部左邊,5底部右邊,6頂部中間,7頂部左邊,8頂部右邊,其餘爲默認正中間
* @param w 裁剪寬度
* @param h 裁剪高度
* @param targetImgPath 新圖片路徑
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void crop(String originalImgPath, int position, int w, int h, String targetImgPath) throws Exception {
Thumbnails.of(originalImgPath).sourceRegion(getPositions(position), w, h).size(w, h).outputQuality(1).toFile(targetImgPath);
}
/**
* 圖片添加水印
* @param originalImgPath 將被添加水印圖片路徑
* @param watermarkImgPath 水印圖片
* @param position 位置 0正中間,1中間左邊,2中間右邊,3底部中間,4底部左邊,5底部右邊,6頂部中間,7頂部左邊,8頂部右邊,其餘爲默認正中間
* @param opacity 不透明度,取0~1之間的float數字,0徹底透明,1徹底不透明
* @param targetImgPath 含有水印的新圖片路徑
* @throws Exception
* @author Henry
* @version [版本號, Jun 20, 2017]
* @see [類、類#方法、類#成員]
*/
public static void watermark(String originalImgPath, String watermarkImgPath, int position, float opacity, String targetImgPath) throws Exception {
Thumbnails.of(originalImgPath).watermark(getPositions(position), ImageIO.read(new File(watermarkImgPath)), opacity).scale(1.0).outputQuality(1).toFile(targetImgPath);
}
private static Positions getPositions(int position) {
Positions p = Positions.CENTER;
switch (position) {
case 0: {
p = Positions.CENTER;
break;
}
case 1: {
p = Positions.CENTER_LEFT;
break;
}
case 2: {
p = Positions.CENTER_RIGHT;
break;
}
case 3: {
p = Positions.BOTTOM_CENTER;
break;
}
case 4: {
p = Positions.BOTTOM_LEFT;
break;
}
case 5: {
p = Positions.BOTTOM_RIGHT;
break;
}
case 6: {
p = Positions.TOP_CENTER;
break;
}
case 7: {
p = Positions.TOP_LEFT;
break;
}
case 8: {
p = Positions.TOP_RIGHT;
break;
}
default: {
p = Positions.CENTER;
break;
}
}
return p;
}
}java