|
|
@@ -0,0 +1,122 @@
|
|
|
+package com.zhixinghe1.ots.core;
|
|
|
+
|
|
|
+import net.coobird.thumbnailator.filters.ImageFilter;
|
|
|
+import net.coobird.thumbnailator.util.BufferedImages;
|
|
|
+
|
|
|
+import java.awt.*;
|
|
|
+import java.awt.geom.AffineTransform;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 水印平铺
|
|
|
+ */
|
|
|
+public class TileImageFilter implements ImageFilter {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印字体
|
|
|
+ */
|
|
|
+ private Font font;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印字体颜色
|
|
|
+ */
|
|
|
+ private Color c;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印字体透明度
|
|
|
+ */
|
|
|
+ private Float alpha;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印内容
|
|
|
+ */
|
|
|
+ private String caption;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 水印文字旋转角度
|
|
|
+ */
|
|
|
+ private Integer degree;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平铺时X轴间隔
|
|
|
+ */
|
|
|
+ private Integer widthInterval;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 平铺时Y轴间隔
|
|
|
+ */
|
|
|
+ private Integer heightInterval;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否错开
|
|
|
+ */
|
|
|
+ private Boolean criscross;
|
|
|
+
|
|
|
+ public TileImageFilter(Font font, Color c, Float alpha, String caption, Integer degree, Integer widthInterval, Integer heightInterval, Boolean criscross) {
|
|
|
+ this.font = font;
|
|
|
+ this.c = c;
|
|
|
+ this.alpha = alpha;
|
|
|
+ this.caption = caption;
|
|
|
+ this.degree = degree;
|
|
|
+ this.widthInterval = widthInterval;
|
|
|
+ this.heightInterval = heightInterval;
|
|
|
+ this.criscross = criscross;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BufferedImage apply(BufferedImage img) {
|
|
|
+ BufferedImage newImage = BufferedImages.copy(img);
|
|
|
+
|
|
|
+ Graphics2D g = newImage.createGraphics();
|
|
|
+ g.setFont(font);
|
|
|
+ g.setColor(c);
|
|
|
+ g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
|
|
|
+
|
|
|
+ // 图片长度、图片宽度
|
|
|
+ int imageWidth = img.getWidth();
|
|
|
+ int imageHeight = img.getHeight();
|
|
|
+
|
|
|
+ // 图片中心坐标
|
|
|
+ int centerX = imageWidth / 2;
|
|
|
+ int centerY = imageHeight / 2;
|
|
|
+
|
|
|
+ // 内容长度、内容高度
|
|
|
+ int captionWidth = g.getFontMetrics().stringWidth(caption);
|
|
|
+ int captionHeight = g.getFontMetrics().getHeight();
|
|
|
+
|
|
|
+ // 内容间隔
|
|
|
+ int tmpWidthInterval = captionWidth + widthInterval;
|
|
|
+ int tmpHeightInterval = captionHeight + heightInterval;
|
|
|
+
|
|
|
+ // 错开时偏移量
|
|
|
+ int offsetX = criscross ? tmpWidthInterval / 2 : 0;
|
|
|
+
|
|
|
+ // 文字旋转
|
|
|
+ g.transform(AffineTransform.getRotateInstance(Math.toRadians(degree), 0, 0));
|
|
|
+
|
|
|
+ // 计算字体坐标
|
|
|
+ for (int i = centerX; i < 2 * imageWidth; i = i + tmpWidthInterval) {
|
|
|
+ for (int j = centerY, idx = 0; j < 2 * imageHeight; j = j + tmpHeightInterval, idx++) {
|
|
|
+ g.drawString(caption, idx % 2 == 0 ? offsetX + i : i, j);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = centerY - tmpHeightInterval, idx = 1; j > 2 * (-imageHeight); j = j - tmpHeightInterval, idx++) {
|
|
|
+ g.drawString(caption, idx % 2 == 0 ? offsetX + i : i, j);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = centerX - tmpWidthInterval; i > 2 * (-imageWidth); i = i - tmpWidthInterval) {
|
|
|
+ for (int j = centerY, idx = 0; j < 2 * imageHeight; j = j + tmpHeightInterval, idx++) {
|
|
|
+ g.drawString(caption, idx % 2 == 0 ? offsetX + i : i, j);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int j = centerY - tmpHeightInterval, idx = 1; j > 2 * (-imageHeight); j = j - tmpHeightInterval, idx++) {
|
|
|
+ g.drawString(caption, idx % 2 == 0 ? offsetX + i : i, j);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ g.dispose();
|
|
|
+
|
|
|
+ return newImage;
|
|
|
+ }
|
|
|
+}
|