Thumbnailator java圖片壓縮,加水印,批量生成縮略圖

地址:http://code.google.com/p/thumbnailator/google

一、指定大小進行縮放spa

    //size(寬度, 高度)  
      
    /*   
     * 若圖片橫比200小,高比300小,不變   
     * 若圖片橫比200小,高比300大,高縮小到300,圖片比例不變   
     * 若圖片橫比200大,高比300小,橫縮小到200,圖片比例不變   
     * 若圖片橫比200大,高比300大,圖片按比例縮小,橫爲200或高爲300   
     */   
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .size(200, 300)  
            .toFile("c:/a380_200x300.jpg");  
      
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .size(2560, 2048)   
            .toFile("c:/a380_2560x2048.jpg");  

二、按照比例進行縮放code

Java代碼  收藏代碼

    //scale(比例)  
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .scale(0.25f)  
            .toFile("c:/a380_25%.jpg");  
      
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .scale(1.10f)  
            .toFile("c:/a380_110%.jpg"); 

三、不按照比例,指定大小進行縮放orm

//keepAspectRatio(false) 默認是按照比例縮放的  
Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(200, 200)   
        .keepAspectRatio(false)   
        .toFile("c:/a380_200x200.jpg")

四、旋轉對象

//rotate(角度),正數:順時針 負數:逆時針  
Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(1280, 1024)  
        .rotate(90)   
        .toFile("c:/a380_rotate+90.jpg");   
  
Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(1280, 1024)  
        .rotate(-90)   
        .toFile("c:/a380_rotate-90.jpg");

五、水印blog

    //watermark(位置,水印圖,透明度)  
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .size(1280, 1024)  
            .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)   
            .outputQuality(0.8f)   
            .toFile("c:/a380_watermark_bottom_right.jpg");  
      
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .size(1280, 1024)  
            .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)   
            .outputQuality(0.8f)   
            .toFile("c:/a380_watermark_center.jpg");  

六、裁剪圖片

    //sourceRegion()  
      
    //圖片中心400*400的區域  
    Thumbnails.of("images/a380_1280x1024.jpg")  
            .sourceRegion(Positions.CENTER, 400,400)  
            .size(200, 200)  
            .keepAspectRatio(false)   
            .toFile("c:/a380_region_center.jpg");  
      
    //圖片右下400*400的區域  
    Thumbnails.of("images/a380_1280x1024.jpg")  
            .sourceRegion(Positions.BOTTOM_RIGHT, 400,400)  
            .size(200, 200)  
            .keepAspectRatio(false)   
            .toFile("c:/a380_region_bootom_right.jpg");  
      
    //指定座標  
    Thumbnails.of("images/a380_1280x1024.jpg")  
            .sourceRegion(600, 500, 400, 400)  
            .size(200, 200)  
            .keepAspectRatio(false)   
            .toFile("c:/a380_region_coord.jpg");  

七、轉化圖像格式get

//outputFormat(圖像格式)  
Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(1280, 1024)  
        .outputFormat("png")   
        .toFile("c:/a380_1280x1024.png");   
  
Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(1280, 1024)  
        .outputFormat("gif")   
        .toFile("c:/a380_1280x1024.gif"); 

八、輸出到OutputStreamit

    //toOutputStream(流對象)  
    OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");  
    Thumbnails.of("images/a380_1280x1024.jpg")   
            .size(1280, 1024)  
            .toOutputStream(os);  

九、輸出到BufferedImageio

//asBufferedImage() 返回BufferedImage  
BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")   
        .size(1280, 1024)  
        .asBufferedImage();  
ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg")); 
相關文章
相關標籤/搜索