Java thumbnail generation library Thumbnailator application description

  • 2020-04-01 01:23:39
  • OfStack

Thumbnailator is a library for generating thumbnails for a smoother Java interface. Providing thumbnails of existing image files and image objects from the API simplifies the thumbnail process, allowing two or three lines of code to generate thumbnails from existing images and to fine-tune the thumbnail generation while keeping the amount of code written to a minimum. It also supports batch generation of thumbnails from a directory.

Version: thumbnailator - 0.4.2. Jar

The original picture is as follows:
< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022535.jpg" >

1. Specify the size to scale


//Size (width, height)

 
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");


Scale to scale

//Scale (ratio)
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");


3, not according to the scale, the specified size for scaling

//KeepAspectRatio (false) is scaled by default
Thumbnails.of("images/a380_1280x1024.jpg") 
.size(200, 200) 
.keepAspectRatio(false) 
.toFile("c:/a380_200x200.jpg");


4, rotation,

//Rotate, positive: clockwise negative: counterclockwise
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"); 

< img SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022536.jpg" border = 0 > < img border = 0 SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022537.jpg" >

5, the watermark

//Watermark (position, watermark, transparency)
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");

< img SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022538.jpg" border = 0 > < img border = 0 SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022539.jpg" >

6, cutting

//sourceRegion() 

//An area of 400 by 400 in the center of the image
Thumbnails.of("images/a380_1280x1024.jpg") 
.sourceRegion(Positions.CENTER, 400,400) 
.size(200, 200) 
.keepAspectRatio(false) 
.toFile("c:/a380_region_center.jpg"); 

//The area of 400*400 at the bottom right of the picture
Thumbnails.of("images/a380_1280x1024.jpg") 
.sourceRegion(Positions.BOTTOM_RIGHT, 400,400) 
.size(200, 200) 
.keepAspectRatio(false) 
.toFile("c:/a380_region_bootom_right.jpg"); 

//The specified coordinates
Thumbnails.of("images/a380_1280x1024.jpg") 
.sourceRegion(600, 500, 400, 400) 
.size(200, 200) 
.keepAspectRatio(false) 
.toFile("c:/a380_region_coord.jpg");

< img SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022540.jpg" border = 0 > < img SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022541.jpg" border = 0 > < img border = 0 SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022542.jpg" >

7. Convert image format

//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"); 


8. Output to an OutputStream

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


Output to BufferedImage

//AsBufferedImage () returns the BufferedImage
BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg") 
.size(1280, 1024) 
.asBufferedImage(); 
ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg")); 


< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022543.jpg" > size: 20.7 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022544.jpg" > size: 1.1 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022545.jpg" > size: 2.3 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022546.jpg" > size: 2.2 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022547.jpg" > size: 23.2 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022548.jpg" > size: 23.3 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022549.jpg" > size: 27.9 KB

< img border = 0 class = magplus title = click view the original size image SRC = "/ / files.jb51.net/file_images/article/201212/2012121516022550.jpg" > size: 27 KB


Related articles: