zoukankan      html  css  js  c++  java
  • Java生成缩略图,我觉得Thumbnailator挺香!

    简介

    Thumbnailator是一个用于Java的缩略图生成库。

    通过Thumbnailator提供的流畅接口(fluent interface)的方式可以完成复杂的缩略图处理任务,无需访问Image I/O API并通过Graphics2D对象手动操作BufferedImages。

    Maven依赖:

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

    Thumbnailator的使用

    原图original.jpg:

    大小:49.1 KB
    尺寸:640 x 426

    生成缩略图(按等比缩放):

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of(new File("f:\original.jpg"))
                    //设置缩略图大小,按等比缩放
                    .size(200, 200)
                    //将生成的缩略图写入文件
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:4.7 KB
    尺寸:200 x 133

    生成缩略图(不按等比缩放):

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of(new File("f:\original.jpg"))
                    //设置缩略图大小,不按等比缩放
                    .forceSize(200, 200)
                    //将生成的缩略图写入文件
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:6.3 KB
    尺寸:200 x 200

    按比例缩放图片:

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of(new File("f:\original.jpg"))
                    //缩小50%
                    .scale(0.5)
                    //将生成的缩略图写入文件
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:9.8 KB
    尺寸:320 x 213

    缩放并旋转图片:

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of(new File("f:\original.jpg"))
                    .size(300, 300)
                    //旋转180度
                    .rotate(180)
                    //将生成的缩略图写入文件
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:8.6 KB
    尺寸:300 x 200

    缩放图片并添加水印:

    public class Demo {
        public static void main(String[] args) throws IOException {
            //水印图片
            BufferedImage watermarkImage = ImageIO.read(new File("f:\watermark.jpg"));
    
            Thumbnails.of(new File("f:\original.jpg"))
                    .size(500, 500)
                    //添加水印
                    //watermark参数1:表示水印位置,Positions枚举类中预定义了一些常用的位置
                    //watermark参数2:水印图片
                    //watermark参数3:水印的不透明度
                    .watermark(Positions.BOTTOM_RIGHT, watermarkImage, 0.8f)
                    //将生成的缩略图写入文件
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:23.1 KB
    尺寸:500 x 333

    图片裁剪:

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of(new File("f:\original.jpg"))
                    //裁剪大小
                    .size(200, 200)
                    //裁剪位置
                    .crop(Positions.CENTER)
                    .toFile(new File("f:\thumbnail.jpg"));
        }
    }
    

    大小:6.1 KB
    尺寸:200 x 200

    批量处理缩略图:

    public class Demo {
        public static void main(String[] args) throws IOException {
            Thumbnails.of("f:\original.jpg", "f:\original2.jpg", "f:\original3.jpg")
                    .size(200, 200)
                    //处理后的缩略图输出到指定文件夹,使用原来的名称
                    .toFiles(new File("f:\images"), Rename.NO_CHANGE);
        }
    }
    

    除了将缩略图输出到指定文件之外,也可以将他输出到输出流或直接作为BufferedImage返回。

  • 相关阅读:
    bzoj 4012: [HNOI2015]开店
    POJ 1054 The Troublesome Frog
    POJ 3171 Cleaning Shifts
    POJ 3411 Paid Roads
    POJ 3045 Cow Acrobats
    POJ 1742 Coins
    POJ 3181 Dollar Dayz
    POJ 3040 Allowance
    POJ 3666 Making the Grade
    洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
  • 原文地址:https://www.cnblogs.com/seve/p/14559250.html
Copyright © 2011-2022 走看看