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
- // size (width, height)
- /*
- * If your photo is smaller cross than 200 higher than 300 small, unchanged
- * If your photo is horizontal, it is greater than 300 higher than 200 small, high reduced to 300, the same proportion Pictures
- * If the picture horizontal than 200 large and 300 smaller than the cross down to 200, the same proportion Pictures
- * If the image is larger than the cross-200, higher than 300 large, scaled down image, or a high transverse 200 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. The zoom ratio
- // 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, is not scaled according to the ratio specified size
- // keepAspectRatio (false) default is scaled in accordance with
- Thumbnails.of("images/a380_1280x1024.jpg")
- .size(200, 200)
- .keepAspectRatio(false)
- .toFile("c:/a380_200x200.jpg");
4. Rotate
- // rotate (angle), 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");
5, watermark
- // watermark (position, the 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
- //sourceRegion()
- Regional // Photo Center 400 * 400
- Thumbnails.of("images/a380_1280x1024.jpg")
- .sourceRegion(Positions.CENTER, 400,400)
- .size(200, 200)
- .keepAspectRatio(false)
- .toFile("c:/a380_region_center.jpg");
- Picture lower right area // 400 * 400
- Thumbnails.of("images/a380_1280x1024.jpg")
- .sourceRegion(Positions.BOTTOM_RIGHT, 400,400)
- .size(200, 200)
- .keepAspectRatio(false)
- .toFile("c:/a380_region_bootom_right.jpg");
- // specified coordinates
- Thumbnails.of("images/a380_1280x1024.jpg")
- .sourceRegion(600, 500, 400, 400)
- .size(200, 200)
- .keepAspectRatio(false)
- .toFile("c:/a380_region_coord.jpg");
7, image format conversion
- // 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, the output to the OutputStream
- // toOutputStream (stream object)
- OutputStream os = new FileOutputStream("c:/a380_1280x1024_OutputStream.png");
- Thumbnails.of("images/a380_1280x1024.jpg")
- .size(1280, 1024)
- .toOutputStream (OS);
9, the output BufferedImage
- //asBufferedImage() 返回BufferedImage
- BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg")
- .size(1280, 1024)
- .asBufferedImage();
- ImageIO.write(thumbnail, "jpg", new File("c:/a380_1280x1024_BufferedImage.jpg"));