|
1 | 1 | package cn.com.ttblog.ssmbootstrap_table; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.IOException; |
5 | | - |
6 | | -import javax.imageio.ImageIO; |
7 | | - |
| 3 | +import net.coobird.thumbnailator.Thumbnails; |
| 4 | +import net.coobird.thumbnailator.geometry.Coordinate; |
8 | 5 | import org.apache.commons.lang3.RandomStringUtils; |
9 | 6 | import org.junit.Test; |
10 | 7 | import org.slf4j.Logger; |
11 | 8 | import org.slf4j.LoggerFactory; |
12 | 9 |
|
13 | | -import net.coobird.thumbnailator.Thumbnails; |
14 | | -import net.coobird.thumbnailator.geometry.Positions; |
| 10 | +import javax.imageio.ImageIO; |
| 11 | +import java.awt.image.BufferedImage; |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.net.URL; |
15 | 15 |
|
16 | 16 | /** |
17 | 17 | * http://rensanning.iteye.com/blog/1545708 |
18 | 18 | * Thumbnailator 图片裁切、缩放、水印等 |
19 | 19 | */ |
20 | 20 | public class TestThumbnailator { |
21 | | - |
22 | | - private static final String FILENAME="thumbnailator.jpg"; |
23 | | - private static final Logger LOG=LoggerFactory.getLogger(TestThumbnailator.class); |
24 | | - @Test |
25 | | - public void testThumbnailator() throws IOException{ |
| 21 | + |
| 22 | + private static final String FILENAME = "thumbnailator.jpg"; |
| 23 | + private static final Logger LOG = LoggerFactory.getLogger(TestThumbnailator.class); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testThumbnailator() throws IOException { |
26 | 27 | // 指定大小进行缩放 |
27 | | - /* |
28 | | - * 若图片横比200小,高比300小,不变 |
29 | | - * 若图片横比200小,高比300大,高缩小到300,图片比例不变 |
30 | | - * 若图片横比200大,高比300小,横缩小到200,图片比例不变 |
31 | | - * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 |
32 | | - */ |
33 | | - String file=this.getClass().getResource(FILENAME).getFile(); |
34 | | - File f=new File(file); |
35 | | - LOG.debug("file-path:{},parent:{}",f.getAbsolutePath(),f.getParent()); |
36 | | - Thumbnails.of(f.getAbsolutePath()) |
37 | | - .size(200, 300) |
38 | | - .toFile("thumbnailator"+RandomStringUtils.randomAlphabetic(5)+".jpg"); |
39 | | - |
40 | | - Thumbnails.of(f.getAbsolutePath()) |
41 | | - .size(2560, 2048) |
42 | | - .toFile("thumbnailator"+RandomStringUtils.randomAlphabetic(5)+".jpg"); |
43 | | - |
44 | | - Thumbnails.of(f.getAbsolutePath()) |
45 | | - .scale(0.25f) |
46 | | - .toFile("thumbnailator"+RandomStringUtils.randomAlphabetic(5)+".jpg"); |
47 | | - |
48 | | - Thumbnails.of(f.getAbsolutePath()) |
49 | | - .scale(1.10f) |
50 | | - .toFile("thumbnailator"+RandomStringUtils.randomAlphabetic(5)+".jpg"); |
51 | | - } |
52 | | - |
53 | | - @Test |
54 | | - public void testWaterMark() throws IOException{ |
55 | | - String file=this.getClass().getResource("qr.jpg").getFile(),icon=this.getClass().getResource("java.png").getFile(); |
56 | | - File f=new File(file); |
57 | | - Thumbnails.of(f.getAbsolutePath()) |
58 | | - .size(200, 200) |
59 | | - .watermark(Positions.CENTER, ImageIO.read(new File(icon)), 0.8f) |
60 | | - .outputQuality(0.8f) |
61 | | - .toFile("thumbnailator"+RandomStringUtils.randomAlphabetic(5)+".jpg"); |
62 | | - } |
| 28 | + /* |
| 29 | + * 若图片横比200小,高比300小,不变 |
| 30 | + * 若图片横比200小,高比300大,高缩小到300,图片比例不变 |
| 31 | + * 若图片横比200大,高比300小,横缩小到200,图片比例不变 |
| 32 | + * 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 |
| 33 | + */ |
| 34 | + String file = this.getClass().getResource(FILENAME).getFile(); |
| 35 | + File f = new File(file); |
| 36 | + LOG.debug("file-path:{},parent:{}", f.getAbsolutePath(), f.getParent()); |
| 37 | + Thumbnails.of(f.getAbsolutePath()) |
| 38 | + .size(200, 300) |
| 39 | + .toFile("thumbnailator" + RandomStringUtils.randomAlphabetic(5) + ".jpg"); |
| 40 | + |
| 41 | + Thumbnails.of(f.getAbsolutePath()) |
| 42 | + .size(2560, 2048) |
| 43 | + .toFile("thumbnailator" + RandomStringUtils.randomAlphabetic(5) + ".jpg"); |
| 44 | + |
| 45 | + Thumbnails.of(f.getAbsolutePath()) |
| 46 | + .scale(0.25f) |
| 47 | + .toFile("thumbnailator" + RandomStringUtils.randomAlphabetic(5) + ".jpg"); |
| 48 | + |
| 49 | + Thumbnails.of(f.getAbsolutePath()) |
| 50 | + .scale(1.10f) |
| 51 | + .toFile("thumbnailator" + RandomStringUtils.randomAlphabetic(5) + ".jpg"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testWaterMark() throws IOException { |
| 56 | + String file = "F:\\image\\bg.png", |
| 57 | + icon = "F:\\image\\showqrcode.jpg"; |
| 58 | + BufferedImage image = ImageIO.read(new File(file)); |
| 59 | + Thumbnails.of(image) |
| 60 | + .size(image.getWidth(), image.getHeight()) |
| 61 | + .watermark(new Coordinate(229, 178), Thumbnails.of(icon).size(240, 240).asBufferedImage(), 1.0f) |
| 62 | + .outputQuality(1.0f) |
| 63 | + .toFile("f:/image/thumbnailator" + RandomStringUtils.randomAlphabetic(5) + ".jpg"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testImageIO() throws IOException { |
| 68 | + URL url = new URL("https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQEC8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyYXpCdElNd3dlYm0xSzgxbzFxY0oAAgRcwJhaAwQsAQAA"); |
| 69 | + BufferedImage bufferedImage = ImageIO.read(url); |
| 70 | + LOG.info("bufferedImage:{}", bufferedImage); |
| 71 | + ImageIO.write(bufferedImage, "jpg", new File("f:/image/qr.jpg")); |
| 72 | + } |
63 | 73 | } |
0 commit comments