Image processing is a very important part in today's software development. However, there are not many open-source frameworks for processing images. Although the Java code circulating on the cash network can be used to process images in a simple way, the results are not satisfactory. Although there are some other solutions, they can't get rid of the cumbersome and inconvenient to use.
In order to solve this problem, I also searched for it online for a long time. I read a lot of materials and found an open-source framework with great image processing skills. I would like to share it with you.
Thumbnailator is an excellent open-source Google Java class library for image processing. The processing effect is far better than that of Java APIs. The processing process is simplified from the APIs that provide existing image files and image objects. Two or three lines of code can be used to generate processed images from existing images and fine-tune the image generation method, at the same time, the minimum amount of code to be written is maintained. It also supports batch processing of all images in a directory.
Supported processing operations: image scaling, area cropping, watermark, rotation, and aspect ratio.
Source image:
1. Zoom in or out the specified size
// Size (width, height)/** if the image horizontal ratio is smaller than 200, the height ratio is smaller than 300. * If the image horizontal ratio is smaller than 200, the height ratio is greater than 300, and the height is reduced to 300, the image proportion remains unchanged * If the image horizontal ratio is greater than 200, the aspect ratio is smaller than 300, and the aspect ratio is reduced to 200. * If the image horizontal ratio is greater than 200, the aspect ratio is greater than 300, and the image proportion is reduced, the horizontal value is 200 or the height is 300 */Thumbnails. of (images/a380_1280x1024.jpg ). size (200,300 ). toFile (c:/a380_200x300.jpg); Thumbnails. of (images/a380_1280x1024.jpg ). size (2560,204 8 ). toFile (c:/a380_2560x2048.jpg );
2. Scale by proportion
// Scale (proportion) Thumbnails. of (images/a380_1280x1024.jpg ). scale (0.25f ). toFile (c:/a380_252.16.jpg); Thumbnails. of (images/a380_1280x1024.jpg ). scale (1.10f ). toFile (c:/a380_1102.16.jpg );
3. scale according to the specified size instead of the ratio.
// KeepAspectRatio (false) The default value is proportional scaling of Thumbnails. of (images/a380_1280x1024.jpg). size (200,200). keepAspectRatio (false). toFile (c:/a380_200x200.jpg );
4. Rotate
// Rotate (angle), positive value: Negative clockwise number: Thumbnails counterclockwise. of (images/a380_1280x1024.jpg ). size ). rotate (90 ). toFile (c:/a380_rotate000090.jpg); Thumbnails. of (images/a380_1280x1024.jpg ). size ). rotate (-90 ). toFile (c:/a380_rotate-90.jpg );
5. Watermarks
// Watermark (Position, watermark image, transparency) Thumbnails. of (images/a380_1280x1024.jpg ). size ). watermark (Positions. BOTTOM_RIGHT, ImageIO. read (newFile (images/watermark.png), 0.5f ). outputQuality (0.8f ). toFile (c:/a380_watermark_bottom_right.jpg); Thumbnails. of (images/a380_1280x1024.jpg ). size ). watermark (Positions. CENTER, ImageIO. read (newFile (images/watermark.png), 0.5f ). outputQuality (0.8f ). toFile (c:/a380_watermark_center.jpg );
6. Cropping
// SourceRegion () // Thumbnails in the image center 400*400 region. of (images/a380_1280x1024.jpg ). sourceRegion (Positions. CENTER, 400,400 ). size (200,200 ). keepAspectRatio (false ). toFile (c:/a380_region_center.jpg); // Thumbnails in the lower right corner of the image 400*400. of (images/a380_1280x1024.jpg ). sourceRegion (Positions. BOTTOM_RIGHT, 400,400 ). size (200,200 ). keepAspectRatio (false ). toFile (c:/a380_region_bootom_right.jpg); // specify the coordinates of Thumbnails. of (images/a380_1280x1024.jpg ). sourceRegion (1, 600,500,400,400 ). size (200,200 ). keepAspectRatio (false ). toFile (c:/a380_region_coord.jpg );
7. Convert the image format
// OutputFormat (image format) Thumbnails. of (images/a380_1280x1024.jpg ). size ). outputFormat (png ). toFile (c:/a380_1280x1024.png); Thumbnails. of (images/a380_1280x1024.jpg ). size ). outputFormat (gif ). toFile (c:/a380_1280x1024.gif );
8. output to OutputStream
// ToOutputStream (Stream object) OutputStreamos = newFileOutputStream (c:/example); Thumbnails. of (images/a380_1280x1024.jpg). size (,). toOutputStream (OS );
9. output to BufferedImage
// AsBufferedImage () returns BufferedImageBufferedImagethumbnail = Thumbnails. of (images/a380_1280x1024.jpg ). size ). asBufferedImage (); ImageIO. write (thumbnail, jpg, newFile (c:/a380_1280x1024_BufferedImage.jpg ));