sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

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

一、核心代码

  1. /**
  2. * 生成背景透明的 文字水印
  3. *
  4. * @param width 生成图片宽度
  5. * @param height 生成图片高度
  6. * @param text 水印文字
  7. * @param color 颜色对象
  8. * @param font awt字体
  9. * @param degree 水印文字旋转角度
  10. * @param alpha 水印不透明度0f-1.0f
  11. * @param imagePath 图片地址
  12. */
  13. public static BufferedImage waterMarkByText(int width, int height, String text, Color color, Font font, Double degree, float alpha, String imagePath) {
  14. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  15. // 得到画笔对象
  16. Graphics2D g2d = buffImg.createGraphics();
  17. // 增加下面的代码使得背景透明
  18. buffImg = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  19. g2d.dispose();
  20. g2d = buffImg.createGraphics();
  21. // 设置对线段的锯齿状边缘处理
  22. g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
  23. //把源图片写入
  24. if (imagePath != null && !imagePath.equals("")) {
  25. try {
  26. Image srcImg = ImageIO.read(new File(imagePath));
  27. Image image = srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH);
  28. g2d.drawImage(image, 0, 0, null);
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. // 设置水印旋转
  34. if (null != degree) {
  35. //注意rotate函数参数theta,为弧度制,故需用Math.toRadians转换一下
  36. //以矩形区域中央为圆心旋转
  37. g2d.rotate(Math.toRadians(degree), (double) buffImg.getWidth() / 2, (double) buffImg.getHeight() / 2);
  38. }
  39. // 设置颜色
  40. g2d.setColor(color);
  41. // 设置 Font
  42. g2d.setFont(font);
  43. // 设置透明度:1.0f为透明度 ,值从0-1.0,依次变得不透明
  44. g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
  45. // 获取真实宽度
  46. float realWidth = getRealFontWidth(text);
  47. float fontSize = font.getSize();
  48. // 计算绘图偏移x、y,使得使得水印文字在图片中居中,x、y坐标是基于Graphics2D.rotate过后的坐标系
  49. float x = 0.5f * width - 0.5f * fontSize * realWidth;
  50. float y = 0.5f * height + 0.5f * fontSize;
  51. // 取绘制的字串宽度、高度中间点进行偏移,使得文字在图片坐标中居中
  52. g2d.drawString(text, x, y);
  53. // 释放资源
  54. g2d.dispose();
  55. return buffImg;
  56. }
  57. /**
  58. * 获取真实字符串宽度,ascii字符占用0.5,中文字符占用1.0
  59. *
  60. * @param text 文字
  61. * @return 宽度
  62. */
  63. private static float getRealFontWidth(String text) {
  64. int len = text.length();
  65. float width = 0f;
  66. for (int i = 0; i < len; i++) {
  67. if (text.charAt(i) < 256) {
  68. width += 0.5f;
  69. } else {
  70. width += 1.0f;
  71. }
  72. }
  73. return width;
  74. }

二、测试

  1. /**
  2. * 测试
  3. *
  4. * @param markImagePath 水印图片目标地址
  5. */
  6. public static void test(String markImagePath) {
  7. try {
  8. BufferedImage bi = waterMarkByText("测试水印~,");
  9. ImageIO.write(bi, "png", new File(markImagePath));
  10. } catch (IOException e) {
  11. e.printStackTrace();
  12. }
  13. }

三、结果

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

原生水印图片

win11 预览截图

posted on 2023-04-11 18:56  sunny123456  阅读(86)  评论(0编辑  收藏  举报