Thumbnail library for java image processing (watermark, thumbnail)

Posted by Loran on Mon, 04 Nov 2019 17:11:10 +0100

Thumbnail is a library for generating thumbnails for a smoother Java interface. It simplifies the thumbnail process from the existing image files and thumbnails of image objects provided by API. Two or three lines of code can generate thumbnails from existing images, and allows fine-tuning thumbnail generation, while maintaining the minimum amount of code to be written. At the same time, it also supports generating thumbnails in batches according to a directory.

It is much more stable and high-quality than any existing one based on jdk library. I don't know why so many people have to rebuild their own wheels. The key is not to build others.  

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

Version: thumbnail-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 to zoom

  1. //Size (width, height)
  2.   
  3. /*   
  4.  * If the horizontal ratio of the picture is smaller than 200, the height ratio is smaller than 300, it will not change
  5.  * If the horizontal ratio of the picture is 200, the height ratio is 300, and the height is reduced to 300, the proportion of the picture remains the same
  6.  * If the horizontal ratio of the picture is 200, the height ratio is 300, and the horizontal ratio is reduced to 200, the proportion of the picture remains the same
  7.  * If the horizontal ratio of the picture is 200 and the height ratio is 300, the picture will be scaled down to 200 or 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. Scale in proportion

  1. //Scale
  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. Do not scale according to the specified size

  1. //Keepsaspectratio (false) is scaled by default.
  2. Thumbnails.of("images/a380_1280x1024.jpg")   
  3.         .size(200, 200)   
  4.         .keepAspectRatio(false)   
  5.         .toFile("c:/a380_200x200.jpg");  



4, rotation

  1. //Rotate, 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 

  1. //Watermark (location, 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,Tailoring 

  1. //sourceRegion()  
  2.   
  3. //Image center 400 * 400 area
  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. //400 * 400 area at the bottom right of the picture
  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. //Specify 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,Convert image format 

  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. Output to OutputStream

  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. Output to BufferedImage

  1. //asBufferedImage() returns 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"));   

Topics: PHP Java JDK Google