Thumbnailator图像处理

来源:互联网 发布:淘宝改差评的链接 编辑:程序博客网 时间:2024/06/01 15:17



/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
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;

/**
*
* @author JP
*/
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"));
/**
* the picture will be stretched if you want get a new one which can
* be the size you need and wouldn`t be stretched, you should
* computing it`s own scale -->can call TestChangeSizeNotStrexched
*/
} 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);
}
}
}

}

0 0
原创粉丝点击