sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Java 生成水印图片
原文链接:https://blog.csdn.net/qq_42151956/article/details/121976565

工具类返回 BufferedImage, 写入文件生成水印图片,可见代码

一、核心代码

 /**
 * 生成背景透明的 文字水印
 *
 * @param width     生成图片宽度
 * @param height    生成图片高度
 * @param text      水印文字
 * @param color     颜色对象
 * @param font      awt字体
 * @param degree    水印文字旋转角度
 * @param alpha     水印不透明度0f-1.0f
 * @param imagePath 图片地址
 */
public static BufferedImage waterMarkByText(int width, int height, String text, Color color, Font font, Double degree, float alpha, String imagePath) {
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    // 得到画笔对象
    Graphics2D g2d = buffImg.createGraphics();
    // 增加下面的代码使得背景透明
    buffImg = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    g2d.dispose();
    g2d = buffImg.createGraphics();
 
    // 设置对线段的锯齿状边缘处理
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
 
    //把源图片写入
    if (imagePath != null && !imagePath.equals("")) {
        try {
            Image srcImg = ImageIO.read(new File(imagePath));
            Image image = srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH);
            g2d.drawImage(image, 0, 0, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    // 设置水印旋转
    if (null != degree) {
        //注意rotate函数参数theta,为弧度制,故需用Math.toRadians转换一下
        //以矩形区域中央为圆心旋转
        g2d.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
    }
 
    // 设置颜色
    g2d.setColor(color);
 
    // 设置 Font
    g2d.setFont(font);
 
    // 设置透明度:1.0f为透明度 ,值从0-1.0,依次变得不透明
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
 
    // 获取真实宽度
    float realWidth = getRealFontWidth(text);
    float fontSize = font.getSize();
 
    // 计算绘图偏移x、y,使得使得水印文字在图片中居中,x、y坐标是基于Graphics2D.rotate过后的坐标系
    float x = 0.5f * width - 0.5f * fontSize * realWidth;
    float y = 0.5f * height + 0.5f * fontSize;
 
    // 取绘制的字串宽度、高度中间点进行偏移,使得文字在图片坐标中居中
    g2d.drawString(text, x, y);
    // 释放资源
    g2d.dispose();
    return buffImg;
}
 
 
/**
 * 获取真实字符串宽度,ascii字符占用0.5,中文字符占用1.0
 *
 * @param text 文字
 * @return 宽度
 */
private static float getRealFontWidth(String text) {
    int len = text.length();
    float width = 0f;
    for (int i = 0; i < len; i++) {
        if (text.charAt(i) < 256) {
            width += 0.5f;
        } else {
            width += 1.0f;
        }
    }
    return width;
}

二、测试

/**
 * 测试
 *
 * @param markImagePath 水印图片目标地址
 */
public static void test(String markImagePath) {
    try {
        BufferedImage bi = waterMarkByText("测试水印~,");
        ImageIO.write(bi, "png", new File(markImagePath));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

三、结果

水印图片如下:由于背景白色,后附一张win 11 预览图

原生水印图片

win11 预览截图

posted on   sunny123456  阅读(102)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-04-11 Idea使用lombok时warn:Generating equals/hashCode implementation but without a call 子类继承父类时候
2022-04-11 配置Maven环境变量
2022-04-11 idea 如何将项目改成maven项目 在pom.xml 文件上右键 Add as Maven Project
2022-04-11 idea 缺失右侧maven窗口
点击右上角即可分享
微信分享提示