前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Thumbnailator用法-SpringBoot整合谷歌开源图片处理框架

Thumbnailator用法-SpringBoot整合谷歌开源图片处理框架

作者头像
CaesarChang张旭
发布2021-03-07 10:43:34
6660
发布2021-03-07 10:43:34
举报
文章被收录于专栏:悟道悟道悟道

1新建一个springboot项目 并且在pom里面 引入依赖 thumbnailator

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

2 在test下面的目录下的测试类写

import net.coobird.thumbnailator.Thumbnailator;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Position;
import net.coobird.thumbnailator.geometry.Positions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;

@SpringBootTest
class PictestApplicationTests {


    private String originFile="/Users/zhangxu/Desktop/origin.jpeg";

    private String toFile = "/Users/zhangxu/Desktop/new.jpg";

    private String waterImg="/Users/zhangxu/Desktop/可爱熊二.png";
    /**
     * 改变尺寸大小
     * @throws IOException
     */
    @Test
    public void changeSizeTest() throws IOException {
        Thumbnails.of(originFile).size(200,100).toFile(toFile);
    }

    /**
     * 缩放
     * @throws IOException
     */
    @Test
    public void scaleTest() throws IOException {
        Thumbnails.of(originFile).scale(0.2).toFile(toFile);
    }


    /**
     * 旋转
     * @throws IOException
     */
    @Test
    public void rotateTest() throws IOException {
        Thumbnails.of(originFile).scale(1).rotate(200).toFile(toFile);
    }


    /**
     * 裁剪   居中裁剪 400*200
     * @throws IOException
     */
    @Test
    public void regionTest() throws IOException {
        Thumbnails.of(originFile).scale(1).sourceRegion(Positions.CENTER,400,200).toFile(toFile);
    }


        /**
         * 加水印
         */

    @Test
    public void wasterMaskTest() throws IOException {
        Thumbnails.of(originFile).scale(1).watermark(Positions.TOP_RIGHT, ImageIO.read(new File(waterImg)),0.6f).toFile(toFile);
    }




}

上面的originFile为源文件 位置, toFile为保存后的位置, watermask为水印, 可以自己更改你自己的路径

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2021-03-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云服务器利旧
云服务器(Cloud Virtual Machine,CVM)提供安全可靠的弹性计算服务。 您可以实时扩展或缩减计算资源,适应变化的业务需求,并只需按实际使用的资源计费。使用 CVM 可以极大降低您的软硬件采购成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档