制作水印图片
Graphics2D设置旋转时 是将整个画布旋转,所以写每个水印文字时 重新设置原点,以原点旋转
代码:
private static BufferedImage createWaterMark(String content) {
Integer width = 1000;
Integer height = 1360;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// 获取bufferedImage对象
String fontType = "宋体";
Integer fontStyle = Font.PLAIN;
Integer fontSize = 30;
Font font = new Font(fontType, fontStyle, fontSize);
Graphics2D g2d = image.createGraphics(); // 获取Graphics2d对象
image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g2d.dispose();
for (int i = 1; i <= 20; i += 2) {
for (int j = 1; j <= 10; j += 2) {
int px = j * 100;
int py = i * 100;
g2d = image.createGraphics();
// g2d.setColor(new Color(0, 0, 0, 50 )); // 设置字体颜色和透明度
g2d.setColor(Color.GREEN);
g2d.setStroke(new BasicStroke(1)); // 设置字体
g2d.setFont(font); // 设置字体类型 加粗 大小
g2d.translate(px, py);//设置原点
g2d.rotate(Math.toRadians(-30));// 设置倾斜度
FontRenderContext context = g2d.getFontRenderContext();
Rectangle2D bounds = font.getStringBounds(content, context);
g2d.drawString(content, 0, 0);
g2d.dispose();
}
}
return image;
}
效果图: 背景透明