首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Thumbnailator用法-SpringBoot整合谷歌开源图片处理框架

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

作者头像
编程张无忌
发布于 2021-03-07 02:43:34
发布于 2021-03-07 02:43:34
94100
代码可运行
举报
文章被收录于专栏:悟道悟道
运行总次数:0
代码可运行

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
 <groupId>net.coobird</groupId>
 <artifactId>thumbnailator</artifactId>
 <version>0.4.8</version>
</dependency>

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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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 条评论
热度
最新
推荐阅读
领券
一站式MCP教程库,解锁AI应用新玩法
涵盖代码开发、场景应用、自动测试全流程,助你从零构建专属AI助手
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验