Thumbnailator is a thumbnail build library that is smoother for the Java interface. By simplifying the thumbnail process from the APIs that provide existing image files and image objects, two or three lines of code can generate thumbnails from existing pictures and allow fine-tuning of thumbnail builds while maintaining the minimum amount of code that needs to be written. It also supports batch generation of thumbnails based on a directory.
Version: Thumbnailator-0.4.2.jar
The original artwork is as follows:
1, the specified size to scale
Copy Code code as follows:
Size (width, height)
/*
* If the picture shape factor 200 small, taller than 300 small, unchanged
* If the picture shape factor 200 small, high than 300 large, high to 300, the picture proportion unchanged
* If the picture shape factor 200, the height is smaller than 300, the horizontal shrinks to 200, the picture proportion does not change
* If the picture shape factor 200 large, high than 300, the picture is scaled down, horizontal 200 or high 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");
2. Scale by scale
Copy Code code as follows:
Scale (proportion)
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, does not follow the scale, specifies the size to scale
Copy Code code as follows:
Keepaspectratio (false) is scaled proportionally by default
Thumbnails.of ("Images/a380_1280x1024.jpg")
. Size (200, 200)
. Keepaspectratio (False)
. ToFile ("c:/a380_200x200.jpg");
4. Rotate
Copy Code code as follows:
Rotate (angle), positive number: 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");
5, Watermark
Copy Code code as follows:
Watermark (location, 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");
6, cutting
Copy Code code as follows:
Sourceregion ()
400*400 area of the picture center
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 the lower right 400*400 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");
Specify coordinates
Thumbnails.of ("Images/a380_1280x1024.jpg")
. Sourceregion (600, 500, 400, 400)
. Size (200, 200)
. Keepaspectratio (False)
. ToFile ("c:/a380_region_coord.jpg");
7. Transform image format
Copy Code code as follows:
OutputFormat (image format)
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 OutputStream
Copy Code code as follows:
Tooutputstream (Stream object)
OutputStream OS = new FileOutputStream ("C:/a380_1280x1024_outputstream.png");
Thumbnails.of ("Images/a380_1280x1024.jpg")
. Size (1280, 1024)
. Tooutputstream (OS);
9, output to BufferedImage
Copy Code code as follows:
Asbufferedimage () return to BufferedImage
BufferedImage thumbnail = thumbnails.of ("images/a380_1280x1024.jpg")
. Size (1280, 1024)
. Asbufferedimage ();
Imageio.write (thumbnail, "jpg", New File ("c:/a380_1280x1024_bufferedimage.jpg"));
Size: 20.7 KB
Size: 1.1 KB
Size: 2.3 KB
Size: 2.2 KB
Size: 23.2 KB
Size: 23.3 KB
Size: 27.9 KB
Size: MB