全屏浏览
缩小浏览
回到页首

Java三方---->Thumbnailator框架的使用

  Thumbnailator是一个用来生成图像缩略图的 Java类库,通过很简单的代码即可生成图片缩略图,也可直接对一整个目录的图片生成缩略图。有了它我们就不用在费心思使用Image I/O API,Java 2D API等等来生成缩略图了,它支持图片缩放,区域裁剪,水印,旋转,保持比例等等。今天,我们就开始Thumbnailator的学习。

 

Thumbnailator项目的引入

一、 Thumbnailator的下载地址:https://github.com/coobird/thumbnailator。下载完成之后进入src/main/java/。将net文件夹复制到自己新建的项目的src下。

二、 选中复制的部分,右键选择expot ---> JAR file。在JAR Export中选择jar的输出位置。

三、 将jar包导入到项目中,删除上述的thumbnailator源文件。

 

Thumbnailator的简单使用

项目中引用的huhx.jpg如下:

一、 生成一张固定大小的huhx1.jpg

@Test
public void thumbnailator1() {
    try {
        Thumbnails.of("image/huhx.jpg").size(80, 80).toFile("photo/huhx1.jpg");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 生成的huhx1.jpg如下:

二、 文件夹所有图片都生成缩略图

@Test
public void thumbnailator2() {
    try {
        Thumbnails.of(new File("image").listFiles()).size(640, 480).outputFormat("jpg").toFiles(Rename.PREFIX_DOT_THUMBNAIL);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 生成的thumbnail.huhx.jpg如下:

 三、 旋转角度的缩略图

@Test
public void thumbnailator3() {
    try {
        Thumbnails.of(new File("image/huhx.jpg")).scale(0.25).rotate(90).outputFormat("jpg").toFile("photo/huhx2.jpg");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

  生成的huhx2.jpg如下:

 

 四、 生成一个带有旋转和水印的缩略图

@Test
public void thumbnailator4() {
    try {
        Thumbnails.of(new File("image/huhx.jpg")).size(160, 160).rotate(90)
                .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("image/water.jpg")), 0.5f)
                .outputQuality(0.8f).toFile(new File("photo/huhx3.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

生成的huhx3.jpg如下:

五、 把生成的图片输出到输出流

@Test
public void thumbnailator5() {
    try {
        OutputStream os = new FileOutputStream("photo/huhx4.jpg");
        Thumbnails.of("image/huhx.jpg").size(200, 200).outputFormat("jpg").toOutputStream(os);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

 生成的huhx4.jpg:

 六、 按一定的比例生成缩略图,生成缩略图的大小是原来的25%

@Test
public void thumbnailator6() {
    try {
        BufferedImage originalImage = ImageIO.read(new File("image/huhx.jpg"));
        BufferedImage thumbnail = Thumbnails.of(originalImage).scale(0.25f).asBufferedImage();
        ImageIO.write(thumbnail, "JPG", new File("photo/huhx5.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

七、 对图片进行裁剪

@Test
    public void thumbnailator7() {
        try {
            /**
             * 中心400*400的区域
             */
            Thumbnails.of("image/huhx.jpg").sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxCenter.jpg");
            /**
             * 右下400*400的区域
             */
            Thumbnails.of("image/huhx.jpg").sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxRight.jpg");
            /**
             * 指定坐标(0, 0)和(400, 400)区域,再缩放为200*200
             */
            Thumbnails.of("image/huhx.jpg").sourceRegion(0, 0, 400, 400).size(200, 200).keepAspectRatio(false).toFile("photo/huhxRegion.jpg");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 生成的huhxCenter.jpg, huhxRight.jpg, huhxRegion.jpg分别如下:

                 

 

友情链接

 

posted @ 2016-05-13 08:05  huhx  阅读(11487)  评论(5编辑  收藏  举报