一个超级简洁的图片编辑工具类Thumbnails

图片编辑工具类Thumbnails

1.导入依赖

        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>

2.创建测试类

package com.atwu.miao.test;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.io.File;

public class ThumbnailatorTest {

 //定义读取和输出图片的路径
 private String file = "D:\\desk\\temp\\img\\maotouying.jpg";
 private String toFile = "D:\\desk\\temp\\img\\copy.png";

 /**
  * 修改长和宽
  * @throws Exception
  */
 @Test
 public void test() throws Exception{
     Thumbnails.of(file).size(1000,600).toFile(toFile);
 }

 /**
  * 等比例缩放
  * @throws Exception
  */
 @Test
 public void scale() throws Exception{
     Thumbnails.of(file).scale(0.8).toFile(toFile);
 }

 /**
  * 图片旋转
  * @throws Exception
  */
 @Test
 public void rotate() throws Exception{
     //rotate 之前必须调scale方法 不然会报错 java.lang.IllegalStateException: size is not set.
     Thumbnails.of(file).scale(1).rotate(180).toFile(toFile);
 }

 /**
  * 图片裁剪
  * @throws Exception
  */
 @Test
 public void region() throws Exception{
     //必须调scale方法
     Thumbnails.of(file).scale(1).sourceRegion(Positions.CENTER,40,40).toFile(toFile);
 }

 public static final String watermark = "D:\\desk\\temp\\img\\haha.png";

 /**
  * 添加水印
  * @throws Exception
  */
 @Test
 public void waterMark() throws Exception{
     //必须调scale方法
     Thumbnails.of(file).scale(1).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)),0.5f).toFile(toFile);
 }
}

简洁到不行呢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值