首页 > 代码库 > Java通过thumbnailator对图片进行缩放,裁剪,添加水印等..
Java通过thumbnailator对图片进行缩放,裁剪,添加水印等..
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对图片进行缩放,裁剪,添加水印等..
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。