Thumbnails图片处理工具类

Thumbnailator 是Google 开源的一个优秀图片处理 Java 库

仓库地址:https://github.com/coobird/thumbnailator

  1.  引入Maven依赖
<dependency>
    <groupId>net.coobird</groupId>
    <artifactId>thumbnailator</artifactId>
    <version>0.4.8</version>
</dependency>

  1. 封装 ThumbnailsUtil 工具类
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

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


public class ThumbnailsUtil {
    /**
     * 指定大小缩放 若图片横比width小,高比height小,放大
     * 若图片横比width小,高比height大,高缩小到height,图片比例不变
     * 若图片横比width大,高比height小,横缩小到width,图片比例不变
     * 若图片横比width大,高比height大,图片按比例缩小,横为width或高为height
     *
     * @param resource  源文件路径
     * @param width     宽
     * @param height    高
     * @param toFilePath 生成文件路径
     */
    public static void changeSize(String resource, int width, int height, String toFilePath) {
        try {
            Thumbnails.of(resource).size(width, height).toFile(toFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 指定比例缩放 scale(),参数小于1,缩小;大于1,放大
     *
     * @param resource   源文件路径
     * @param scale      指定比例
     * @param toFilePath     生成文件路径
     */
    public static void changeScale(String resource, double scale, String toFilePath) {
        try {
            Thumbnails.of(resource).scale(scale).toFile(toFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加水印 watermark(位置,水印,透明度)
     *
     * @param resource  源文件路径
     * @param p         水印位置
     * @param shuiyin   水印文件路径
     * @param opacity   水印透明度
     * @param toFilePath 生成文件路径
     */
    public static void watermark(String resource, Positions p, String shuiyin, float opacity, String toFilePath) {
        try {
            Thumbnails.of(resource).scale(1).watermark(p, ImageIO.read(new File(shuiyin)), opacity).toFile(toFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片旋转 rotate(度数),顺时针旋转
     *
     * @param resource  源文件路径
     * @param rotate    旋转度数
     * @param toFilePath    生成文件路径
     */
    public static void rotate(String resource, double rotate, String toFilePath) {
        try {
            Thumbnails.of(resource).scale(1).rotate(rotate).toFile(toFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 图片裁剪 sourceRegion()有多种构造方法,示例使用的是sourceRegion(裁剪位置,宽,高)
     *
     * @param resource  源文件路径
     * @param p         裁剪位置
     * @param width     裁剪区域宽
     * @param height    裁剪区域高
     * @param toFilePath    生成文件路径
     */
    public static void region(String resource, Positions p, int width, int height, String toFilePath) {
        try {
            Thumbnails.of(resource).scale(1).sourceRegion(p, width, height).toFile(toFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        changeSize("G:\\test.jpg", 15, 15, "G:\\thumbnails\\1.jpg");
        changeScale("G:\\test.jpg", 0.2, "G:\\thumbnails\\changeScale.jpg");
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值