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分別如下:

                 

 

友情鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM