diff --git a/src/test/java/com/freewayso/AppTest.java b/src/test/java/com/freewayso/AppTest.java index 8c46782b6699946127332a9d1bf79d7259860316..7688df00e3b4c50a30a385a21a4b6abe9e1654fd 100644 --- a/src/test/java/com/freewayso/AppTest.java +++ b/src/test/java/com/freewayso/AppTest.java @@ -732,4 +732,96 @@ public class AppTest { System.out.println(fontName[i]); } } + + /** + * Png 文字水印九宫格位置水印测试 + * + * @throws Exception + */ + @Test + public void pngTextWatermark() throws Exception { + //合成器和背景图(整个图片的宽高和相关计算依赖于背景图,所以背景图的大小是个基准) + ImageCombiner combiner = new ImageCombiner(ImgUtil.read(URLUtil.url("https://img.thebeastshop.com/combine_image/funny_topic/resource/bg_3x4.png")), OutputFormat.JPG); + + // 设置字体和大小 + Font font = new Font("阿里巴巴普惠体", Font.PLAIN, 20); + + // 水印与图片边缘的间距 + int padding = 10; + + // 左上 + TextElement topLeft = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + topLeft.setAutoBreakLine("/n", 30).setX(padding).setAlpha(.8f); + combiner.addElement(topLeft); + + // 上中 + TextElement topCenter = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + topCenter.setAutoBreakLine("/n", 30, LineAlign.Center) + .setCenter(true) + .setAlpha(.8f); + combiner.addElement(topCenter); + + // 右上 + TextElement topRight = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + topRight.setAutoBreakLine("/n", 30, LineAlign.Right) + .setX(combiner.getCanvasWidth() - padding) + .setAlpha(.8f) + .setDirection(Direction.RightLeft); + combiner.addElement(topRight); + + // 左中 + TextElement centerLeft = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + centerLeft.setAutoBreakLine("/n", 30) + .setX(padding) + .setY((combiner.getCanvasHeight() - centerLeft.getHeight()) / 2) + .setAlpha(.8f); + combiner.addElement(centerLeft); + + // 居中 + TextElement center = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + center.setAutoBreakLine("/n", 30, LineAlign.Center) + .setCenter(true) + .setY((combiner.getCanvasHeight() - centerLeft.getHeight()) / 2) + .setAlpha(.8f); + combiner.addElement(center); + + // 右中 + TextElement centerRight = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + centerRight.setAutoBreakLine("/n", 30, LineAlign.Right) + .setX(combiner.getCanvasWidth() - padding) + .setY((combiner.getCanvasHeight() - centerLeft.getHeight()) / 2) + .setAlpha(.8f) + .setDirection(Direction.RightLeft); + combiner.addElement(centerRight); + + // 左下 + TextElement bottomLeft = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + bottomLeft.setAutoBreakLine("/n", 30) + .setX(padding) + .setY(combiner.getCanvasHeight() - bottomLeft.getHeight()) + .setAlpha(.8f); + combiner.addElement(bottomLeft); + + // 下中 + TextElement bottomCenter = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + bottomCenter.setAutoBreakLine("/n", 30, LineAlign.Center) + .setCenter(true) + .setY(combiner.getCanvasHeight() - bottomCenter.getHeight()) + .setAlpha(.8f); + combiner.addElement(bottomCenter); + + // 右下 + TextElement bottomRight = new TextElement("第一行文字/n第二行文字/n第三行文字/n" + DateUtil.now(), font, 0, 0); + bottomRight.setAutoBreakLine("/n", 30, LineAlign.Right) + .setX(combiner.getCanvasWidth() - padding) + .setY(combiner.getCanvasHeight() - bottomRight.getHeight()) + .setAlpha(.8f) + .setDirection(Direction.RightLeft); + combiner.addElement(bottomRight); + + //合成图片 + combiner.combine(); + //上传oss + combiner.save("d://pngTextWatermark.png"); + } }