package com.hand.emp.test;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.builders.BufferedImageBuilder;
import net.coobird.thumbnailator.geometry.Positions;
import net.coobird.thumbnailator.Thumbnails.Builder;
import java.math.BigDecimal;
public class TestThumbnailator {
String outputBasePth = "D:\\pictures\\thumbnail\\";
String resourceFilePath = "D:\\pictures\\yu1.jpg";
// @Test
public void TestChangeFormat() {
try {
Thumbnails.of(resourceFilePath)
.scale(0.5f)//there must be size or scale
.outputFormat("png")
.toFile(new File(outputBasePth + "PicFormat1.png"));// .toFile(new File(dicString + "t1.png"));
//Allow no suffix
//if the name of the file is same,the new one will cover the old one
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSize() {
try {
Thumbnails.of(resourceFilePath)
.forceSize(800, 300)
.toFile(new File(outputBasePth + "PicSize1.png"));
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
// @Test
public void TestChangeSizeNotStrexched() {
String outputPath = outputBasePth + "Notstretched1";
int target_width = 1000;
int target_height = 2000;
String target_suffix = "jpg";
cover(resourceFilePath, outputPath, target_width, target_height, target_suffix);
}
// @Test
public void TestWaterMark() {
try {
//給圖檔加水印,watermark(position,waterPictureFile,透明度)
Thumbnails.of(resourceFilePath).scale(2f)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("D:\\pictures\\timg.jpg")), 0.6f)
.outputQuality(0.8f).toFile(outputBasePth+"Picwatermark.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Test
public void TestRotate(){
try {
//rotate picture
Thumbnails.of(resourceFilePath).scale(2f)
.rotate(90.0)
.outputQuality(0.8f).toFile(outputBasePth+"PicRotate.jpg");
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void cover(String sourceFile, String targetFile, int targetWidth, int targetHeight, String target_suffix) {
File fromFile = new File(sourceFile);
BufferedImage srcImage = null;
int s_height = 0;
int s_width = 0;
try {
srcImage = ImageIO.read(fromFile);
// 擷取源檔案的寬高
s_height = srcImage.getHeight();
s_width = srcImage.getWidth();
System.out.println(s_height + "");
System.out.println(s_width + "");
} catch (IOException e1) {
e1.printStackTrace();
}
// 判斷是否需要壓縮
if (targetWidth != s_width || targetHeight != s_height) {// 需要壓縮
BigDecimal n = new BigDecimal(s_height).divide(new BigDecimal(s_width), 10, BigDecimal.ROUND_UP);// 源圖檔檔案高/寬 的比例
BigDecimal m = new BigDecimal(targetHeight).divide(new BigDecimal(targetWidth), 10, BigDecimal.ROUND_UP);// 目标圖檔檔案高/寬 的比例
System.out.println(n + "");
System.out.println(m + "");
int flag = n.compareTo(m); //比較源圖檔檔案和目标圖檔 的高/寬 比
try {
if (flag == 0) {// n == m,高/寬 比一緻, 無需裁減, 直接壓縮
System.out.println("flag: " + flag);
Thumbnails.of(sourceFile).forceSize(targetWidth, targetHeight).outputFormat(target_suffix).toFile(new File(targetFile));
} else { // 高/寬 比不一緻,需要裁減
if (flag > 0) {// 高的比例 > 寬的比例,以寬的比例進行縮放, 需要裁減高
srcImage = Thumbnails.of(sourceFile).width(targetWidth).asBufferedImage();//以寬的比例進行縮放
} else if (flag < 0) {// n < m 高的比例 < 寬的比例,以高的比例進行縮放, 需要裁減寬
srcImage = Thumbnails.of(sourceFile).height(targetHeight).asBufferedImage();//以高的比例進行縮放
}
// 居中裁減
Builder<BufferedImage> builder = Thumbnails.of(srcImage)
.sourceRegion(Positions.CENTER, targetWidth, targetHeight).size(targetWidth, targetHeight);
builder.outputFormat(target_suffix).toFile(new File(targetFile));
}
} catch (IOException ex) {
Logger.getLogger(TestThumbnailator.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}