天天看点

缩略图开源库 Thumbnailator 的使用

缩略图开源库 Thumbnailator 可根据已有图片,截取指定范围并缩放,还支持添加水印。本文总结了它的用法。

作者:王克锋

出处:https://kefeng.wang/2017/04/09/thumbnailator/

版权:自由转载-非商用-非衍生-保持署名,转载请标明作者和出处。

1 概述

功能需求:对于特定的原始图片,要求输出为 480*360 的图片。算法为:

  • 宽高等比缩放至 width=480(当纵向内容多余时) 或 height=360(当横向内容多余时);
  • 从图片中心点向四周截取 480*360 大小的图片,四边多余内容丢掉;
  • 最好能在右下角打上水印。

2 开源库 thumbnailator(最佳)

特色: 不依赖外部库,轻便高效,任何平台适用,支持缩放、旋转、截取,特别还支持【水印】。

示例: https://github.com/coobird/thumbnailator/wiki/Examples

源码: https://github.com/coobird/thumbnailator

文档: https://coobird.github.io/thumbnailator/javadoc/0.4.8/

2.1 Maven 依赖

Maven: https://mvnrepository.com/artifact/net.coobird/thumbnailator

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

2.2 代码

public boolean createImage(String input, String output) {
    try {
        BufferedImage image = ImageIO.read(new File(input));
        // BufferedImage image = ImageIO.read(imageFile.getInputStream());

        // 确保原图够大
        if (image.getWidth() < DEST_WIDTH || image.getHeight() < DEST_HEIGHT) {
            logger.warn("ImageFile.createImage({}, {}) too small.", image.getWidth(), image.getHeight());
            return false;
        }

        // 按比例缩小至目标尺寸
        Thumbnails.Builder<BufferedImage> builder = Thumbnails.of(image);
        builder.crop(Positions.CENTER).size(DEST_WIDTH, DEST_HEIGHT);

        // 添加水印
        BufferedImage watermarkImage = ImageIO.read(new File(watermarkfile));
        builder.watermark(Positions.BOTTOM_RIGHT, watermarkImage, f);

        // 保存
        String fileExt = FilenameUtils.getExtension(output);
        builder.outputFormat(fileExt).toFile(output);
        logger.info("ImageFile.createImage(\"{}\") OK.", output);
        return true;
    } catch (IOException e) {
        logger.warn(e.getMessage());
    }

    return false;
}
           

3 开源库 imgscalr

特色: 全部基于 Java 2D,不依赖外部库,轻便高效,任何平台适用,支持缩放、旋转、截取,不支持水印。

源码: https://github.com/rkalla/imgscalr

3.1 Maven 依赖

Maven: http://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib

<dependency>
    <groupId>org.imgscalr</groupId>
    <artifactId>imgscalr-lib</artifactId>
    <version>4.2</version>
</dependency>
           

3.2 代码

public boolean createImage(String input, String output) {
    try {
        BufferedImage image = ImageIO.read(new File(input));
        // BufferedImage image = ImageIO.read(imageFile.getInputStream());

        // 确保原图够大
        if (image.getWidth() < DEST_WIDTH || image.getHeight() < DEST_HEIGHT) {
            logger.warn("ImageFile.createImage({}, {}) too small.", image.getWidth(), image.getHeight());
            return false;
        }

        // 按比例缩小至目标尺寸
        int x = , y = ;
        BufferedImage resized = null;
        double widthRate = (double) image.getWidth() / DEST_WIDTH;
        double heightRate = (double) image.getHeight() / DEST_HEIGHT;
        if (widthRate > heightRate) { // 宽度过剩
            resized = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_HEIGHT, DEST_WIDTH, DEST_HEIGHT, Scalr.OP_ANTIALIAS);
            x = (resized.getWidth() - DEST_WIDTH) / ;
        } else { // 高度过剩
            resized = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.FIT_TO_WIDTH, DEST_WIDTH, DEST_HEIGHT, Scalr.OP_ANTIALIAS);
            y = (resized.getHeight() - DEST_HEIGHT) / ;
        }

        // 截取中间部分
        BufferedImage thumbnail = Scalr.crop(resized, x, y, DEST_WIDTH, DEST_HEIGHT);

        // 保存
        String fileExt = FilenameUtils.getExtension(output);
        ImageIO.write(thumbnail, fileExt, new File(output));
        logger.info("ImageFile.createImage(\"{}\") OK.", output);
        return true;
    } catch (IOException e) {
        logger.warn(e.getMessage());
    }

    return false;
}
           

继续阅读