Thumbnailator 学习

  • 简介

Thumbnailator是针对java语言生成缩略图的类库
此类库的github地址

  • 特点及相关示例

  • 从现有图片创建高质量缩略图
  • 在缩略图中嵌入水印(如徽标)
  • 调整水印的透明度,从透明到不透明(0%--100%)
  • 缩略图的反转
  • 示例代码

官方示例地址
所需maven依赖


    net.coobird
    thumbnailator
    0.4.8

我的示例代码

@Test
public void test1() throws IOException {
    //生成缩略图
    Thumbnails.of(new File("e:/img/test.jpg")).size(160, 160).toFile(new File("e:/img/test1.jpg"));
}
@Test
public void test2() throws IOException {
    Thumbnails.of("e:/img/test.jpg")
            .size(200,200)
            .outputFormat("png")
            .toFile("e:/img/test2.png");
}
@Test
public void test3() throws IOException {
    //创建带水印和反转的缩略图
    Thumbnails.of(new File("e:/img/test.jpg"))//原图片
            .size(160,160)//缩略的尺寸(宽高)
            .rotate(90)//旋转90度
            .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("e:/img/water.png")),0.5f)//放置水印图片的位置以及透明度
            .outputQuality(0.8f)//输出图片的质量
            .toFile(new  File("e:/img/test3.jpg"));//图片的输出位置
}
@Test
public void test4() throws IOException {
    Thumbnails.of("e:/img/test.jpg")
            .scale(0.5)//按比例缩放
            .toFile("e:/img/test4.jpg");
}
@Test
public void test5() throws IOException {
    //把缩略图写到指定目录
    File destinationDir = new File("e:/img/output");
    destinationDir.mkdirs();
    Thumbnails.of("e:/img/test.jpg","e:/img/test_2.jpg")
            .size(200,200)
            .toFiles(destinationDir, Rename.PREFIX_DOT_THUMBNAIL);
}

我的示例中需要用的图片

Thumbnailator 学习_第1张图片
test.jpg
Thumbnailator 学习_第2张图片
test_2.jpg

你可能感兴趣的:(Thumbnailator 学习)