package com.test.image;
import static org.junit.Assert.*;
import java.io.File;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.junit.Test;
public class ImageTest {
/**
* 按照指定大小缩放 按照比例
* @throws Exception
*/
@Test
public void testOne() throws Exception {
//File file = new File("image/1.jpg");
//System.out.println(file.exists());
Thumbnails.of("image/1.jpg").size(200, 300).toFile("image/1_200x300.jpg");
}
/***
* 按照指定比例进行缩放
* @throws Exception
*/
@Test
public void testTwo() throws Exception {
Thumbnails.of("image/1.jpg").scale(0.25f).toFile("image/a_0.25%.jpg");
}
/***
* 根据指定大小进行缩放 不按照比例
* @throws Exception
*/
@Test
public void testThree() throws Exception {
Thumbnails.of("image/1.jpg").size(200, 300)
//设置是否按比例 false 图片可能会走形 默认true
.keepAspectRatio(false)
.toFile("image/1_200x300_no.jpg");
}
/**
* 添加水印 (好像只能添加图片水印?)
* @throws Exception
*/
@Test
public void testFour() throws Exception {
Thumbnails.of("image/1.jpg")
.size(1920, 1080) //必须要设置大小 不然会抛异常
.watermark(Positions.BOTTOM_RIGHT,ImageIO.read(new File("image/a_0.25%.jpg")), 0.5f)
//输出品质 越高 图片越大
.outputQuality(1.0f)
.toFile("image/wt.jpg");
}
/***
* 裁剪
* @throws Exception
*/
@Test
public void testFive() throws Exception {
Thumbnails.of("image/1.jpg")
//从原图哪里开始裁剪 裁减多少
.sourceRegion(Positions.CENTER, 400, 400)
//新图的大小
.size(200, 200).toFile("image/crop.jpg");
}
/**
* 改变图片格式
* @throws Exception
*/
@Test
public void testSix() throws Exception {
Thumbnails.of("image/1.jpg")
.size(1920, 1080)
.outputFormat("png")
.toFile("image/1_png.png");
}
@Test
public void testName() throws Exception {
System.out.println(System.getProperty("user.dir"));
}
}
Java通过thumbnailator对图片进行缩放,裁剪,添加水印等..
最新推荐文章于 2024-07-24 14:47:20 发布