You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Java 8(Spring Boot)中添加带背景的90度旋转文字水印?

实现90度旋转且带黑色背景的文字水印

我正在使用Java 8(Spring Boot),希望为图片添加如下样式的文字水印:文字「HELLO WORLD!」呈90度旋转状态,且带有黑色背景(灰色为原图背景)。以下是我当前实现的添加居中无背景、未旋转文字水印的Java代码:

public BufferedImage AddTextWatermarkInCenter(BufferedImage targetImg, String watermarkText) {
    Graphics2D targetImgGraphics = (Graphics2D)targetImg.getGraphics();
    
    // Init graphic properties
    AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
    targetImgGraphics.setComposite(alphaComposite);
    targetImgGraphics.setColor(Color.BLACK);
    targetImgGraphics.setFont(WatermarkImageHelper.AcquireWatermarkFont());
    
    FontMetrics targetImgFontMetrics = targetImgGraphics.getFontMetrics();
    Rectangle2D targetImgRectangle = targetImgFontMetrics.getStringBounds(watermarkText, targetImgGraphics);
    
    // Calculates the coordinate where the text is painted
    int centerX = ((targetImg.getWidth() - (int)targetImgRectangle.getWidth()) / 2);
    int centerY = (targetImg.getHeight() / 2);
    
    // Paints the textual watermark
    targetImgGraphics.drawString(watermarkText, centerX, centerY);
    
    targetImgGraphics.dispose();
    
    return targetImg;
}

请问如何修改代码以实现示例中的水印效果?


修改后的代码实现

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

public BufferedImage AddTextWatermarkInCenter(BufferedImage targetImg, String watermarkText) {
    Graphics2D targetImgGraphics = (Graphics2D)targetImg.getGraphics();
    
    // 开启抗锯齿,优化文字与图形的平滑度
    targetImgGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    // 初始化绘图属性
    AlphaComposite alphaComposite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f);
    targetImgGraphics.setComposite(alphaComposite);
    targetImgGraphics.setFont(WatermarkImageHelper.AcquireWatermarkFont());
    
    FontMetrics targetImgFontMetrics = targetImgGraphics.getFontMetrics();
    Rectangle2D targetImgRectangle = targetImgFontMetrics.getStringBounds(watermarkText, targetImgGraphics);
    
    int textWidth = (int)targetImgRectangle.getWidth();
    int textHeight = (int)targetImgRectangle.getHeight();
    
    // 以图片中心为原点,旋转90度(弧度转换)
    targetImgGraphics.rotate(Math.toRadians(90), targetImg.getWidth() / 2.0, targetImg.getHeight() / 2.0);
    
    // 计算旋转后文本的居中起始坐标
    int centerX = (targetImg.getWidth() - textWidth) / 2;
    int centerY = (targetImg.getHeight() + textHeight) / 2;
    
    // 绘制带边距的黑色背景框
    int padding = 5;
    targetImgGraphics.setColor(Color.BLACK);
    targetImgGraphics.fillRect(centerX - padding, centerY - textHeight - padding, 
                               textWidth + 2 * padding, textHeight + 2 * padding);
    
    // 设置文字颜色(白色与黑色背景形成对比,可按需调整)
    targetImgGraphics.setColor(Color.WHITE);
    // 绘制旋转后的文字
    targetImgGraphics.drawString(watermarkText, centerX, centerY);
    
    targetImgGraphics.dispose();
    
    return targetImg;
}

关键修改点说明

  • 开启抗锯齿:添加渲染提示,让旋转后的文字和背景边缘更平滑
  • 实现90度旋转:通过rotate()方法将坐标系以图片中心为原点旋转90度,后续所有绘制操作都基于旋转后的坐标系执行
  • 添加黑色背景框:使用fillRect()绘制带内边距的矩形背景,确保文字与背景有足够间距,避免贴边
  • 调整文字颜色:将文字设为白色,保证在黑色背景上的可读性,可根据需求修改颜色
  • 修正坐标计算:旋转后文本宽高互换,重新计算居中坐标,确保旋转后的水印依然处于图片中心

内容的提问来源于stack exchange,提问作者young-ceo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 07:15:33