1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| public class ThumbnailsTest {
@Test public void handle() throws Exception {
String inputPath = "src/main/resources/WechatIMG2.jpeg"; String outputPathPrefix = "src/main/resources/img/WechatIMG_"; String watermark = "src/main/resources/watermark.png";
Thumbnails.of(inputPath).size(200, 300).toFile(outputPathPrefix + "200x300.jpg"); Thumbnails.of(inputPath).size(2560, 2048).toFile(outputPathPrefix + "2560x2048.jpg");
Thumbnails.of(inputPath).scale(0.25f).toFile(outputPathPrefix + "25%.jpg"); Thumbnails.of(inputPath).scale(1.10f).toFile(outputPathPrefix + "110%.jpg");
Thumbnails.of(inputPath).size(120, 120).keepAspectRatio(false).toFile(outputPathPrefix + "120x120.jpg");
Thumbnails.of(inputPath).size(1280, 1024).rotate(90).toFile(outputPathPrefix+"+90.jpg"); Thumbnails.of(inputPath).size(1280, 1024).rotate(-90).toFile(outputPathPrefix+"-90.jpg");
Thumbnails.of(inputPath).size(1280, 1024) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(watermark)), 0.5f) .outputQuality(0.8f).toFile(outputPathPrefix + "watermark_bottom_right.jpg"); Thumbnails.of(inputPath).size(1280, 1024) .watermark(Positions.CENTER, ImageIO.read(new File(watermark)), 0.5f) .outputQuality(0.8f).toFile(outputPathPrefix + "watermark_center.jpg");
Thumbnails.of(inputPath).sourceRegion(Positions.CENTER, 400, 400).size(200, 200).keepAspectRatio(false) .toFile(outputPathPrefix + "region_center.jpg"); Thumbnails.of(inputPath).sourceRegion(Positions.BOTTOM_RIGHT, 400, 400).size(200, 200).keepAspectRatio(false) .toFile(outputPathPrefix + "region_bootom_right.jpg"); Thumbnails.of(inputPath).sourceRegion(600, 500, 400, 400).size(200, 200).keepAspectRatio(false) .toFile(outputPathPrefix + "region_coord.jpg");
Thumbnails.of(inputPath).size(1280, 1024).outputFormat("png").toFile(outputPathPrefix + "1280x1024.png"); Thumbnails.of(inputPath).size(1280, 1024).outputFormat("gif").toFile(outputPathPrefix + "1280x1024.gif");
OutputStream os = new FileOutputStream(outputPathPrefix + "1280x1024_OutputStream.png"); Thumbnails.of(inputPath).size(1280, 1024).toOutputStream(os);
BufferedImage thumbnail = Thumbnails.of(inputPath).size(1280, 1024).asBufferedImage(); ImageIO.write(thumbnail, "jpg", new File(outputPathPrefix + "1280x1024_BufferedImage.jpg")); }
}
|