java Picture (add watermark, generate thumbnails), etc. Thumbnailator library

Thumbnailator is a smoother interface for Java thumbnail generation libraries. Providing existing image files and thumbnail image of the object from the API simplifies the abbreviated process, two or three lines of code to generate thumbnails from existing images, and allows fine-tuning thumbnail generation, while keeping to a minimum the need to write It limits the amount of code. It also supports batch generate thumbnails based on a directory.

Based on their own than any existing jdk library to write stable, high-quality and more, I do not know why so many people have to their own re-create the wheel, the key made, but people. 

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

version: thumbnailator-0.4.2.jar 

<!-- https://mvnrepository.com/artifact/net.coobird/thumbnailator -->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>

1, specify the size of the zoom 

Java code   Collection Code
  1. // size (width, height)  
  2.   
  3. /*   
  4.  * If your photo is smaller cross than 200 higher than 300 small, unchanged   
  5.  * If your photo is horizontal, it is greater than 300 higher than 200 small, high reduced to 300, the same proportion Pictures   
  6.  * If the picture horizontal than 200 large and 300 smaller than the cross down to 200, the same proportion Pictures   
  7.  * If the image is larger than the cross-200, higher than 300 large, scaled down image, or a high transverse 200 300   
  8.  */   
  9. Thumbnails.of("images/a380_1280x1024.jpg")   
  10.         .size(200, 300)  
  11.         .toFile("c:/a380_200x300.jpg");  
  12.   
  13. Thumbnails.of("images/a380_1280x1024.jpg")   
  14.         .size(2560, 2048)   
  15.         .toFile("c:/a380_2560x2048.jpg");  



2. The zoom ratio 

Java code   Collection Code
  1. // scale (ratio)  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .scale(0.25f)  
  4.         .toFile("c:/a380_25%.jpg");  
  5.   
  6. Thumbnails.of("images/a380_1280x1024.jpg")   
  7.         .scale(1.10f)  
  8.         .toFile("c:/a380_110%.jpg");  



3, is not scaled according to the ratio specified size 

Java code   Collection Code
  1. // keepAspectRatio (false) default is scaled in accordance with  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(200, 200)   
  4.         .keepAspectRatio(false)   
  5.         .toFile("c:/a380_200x200.jpg");  



4. Rotate 

Java code   Collection Code
    1. // rotate (angle), positive: clockwise negative: counterclockwise  
    2. Thumbnails.of("images/a380_1280x1024.jpg")   
    3.         .size(1280, 1024)  
    4.         .rotate(90)   
    5.         .toFile("c:/a380_rotate+90.jpg");   
    6.   
    7. Thumbnails.of("images/a380_1280x1024.jpg")   
    8.         .size(1280, 1024)  
    9.         .rotate(-90)   
    10.         .toFile("c:/a380_rotate-90.jpg");   

 

5, watermark 

Java code   Collection Code
    1. // watermark (position, the watermark, transparency)  
    2. Thumbnails.of("images/a380_1280x1024.jpg")   
    3.         .size(1280, 1024)  
    4.         .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f)   
    5.         .outputQuality(0.8f)   
    6.         .toFile("c:/a380_watermark_bottom_right.jpg");  
    7.   
    8. Thumbnails.of("images/a380_1280x1024.jpg")   
    9.         .size(1280, 1024)  
    10.         .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f)   
    11.         .outputQuality(0.8f)   
    12.         .toFile("c:/a380_watermark_center.jpg");  

 

6, cutting 

Java code   Collection Code
    1. //sourceRegion()  
    2.   
    3. Regional // Photo Center 400 * 400  
    4. Thumbnails.of("images/a380_1280x1024.jpg")  
    5.         .sourceRegion(Positions.CENTER, 400,400)  
    6.         .size(200, 200)  
    7.         .keepAspectRatio(false)   
    8.         .toFile("c:/a380_region_center.jpg");  
    9.   
    10. Picture lower right area // 400 * 400  
    11. Thumbnails.of("images/a380_1280x1024.jpg")  
    12.         .sourceRegion(Positions.BOTTOM_RIGHT, 400,400)  
    13.         .size(200, 200)  
    14.         .keepAspectRatio(false)   
    15.         .toFile("c:/a380_region_bootom_right.jpg");  
    16.   
    17. // specified coordinates  
    18. Thumbnails.of("images/a380_1280x1024.jpg")  
    19.         .sourceRegion(600, 500, 400, 400)  
    20.         .size(200, 200)  
    21.         .keepAspectRatio(false)   
    22.         .toFile("c:/a380_region_coord.jpg");  

 

7, image format conversion 

Java code   Collection Code
  1. // outputFormat (image format)  
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(1280, 1024)  
  4.         .outputFormat("png")   
  5.         .toFile("c:/a380_1280x1024.png");   
  6.   
  7. Thumbnails.of("images/a380_1280x1024.jpg")   
  8.         .size(1280, 1024)  
  9.         .outputFormat("gif")   
  10.         .toFile("c:/a380_1280x1024.gif");   



8, the output to the OutputStream 

Java code   Collection Code
  1. // toOutputStream (stream object)  
  2. OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");  
  3. Thumbnails.of("images/a380_1280x1024.jpg")   
  4.         .size(1280, 1024)  
  5.         .toOutputStream (OS);  



9, the output BufferedImage 

Java code   Collection Code
    1. //asBufferedImage() 返回BufferedImage  
    2. BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")   
    3.         .size(1280, 1024)  
    4.         .asBufferedImage();  
    5. ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg"));   

Guess you like

Origin www.cnblogs.com/zhjh256/p/10927687.html