/src/main/java/com/eastrobot/doc/watermark/FontImage.java

https://github.com/ekoz/kbase-doc · Java · 82 lines · 48 code · 7 blank · 27 comment · 0 complexity · 65cabab48ddc64791d913cec2bd4014f MD5 · raw file

  1. /*
  2. * Power by www.xiaoi.com
  3. */
  4. package com.eastrobot.doc.watermark;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.Graphics2D;
  8. import java.awt.Transparency;
  9. import java.awt.font.FontRenderContext;
  10. import java.awt.geom.AffineTransform;
  11. import java.awt.geom.Rectangle2D;
  12. import java.awt.image.BufferedImage;
  13. import java.io.File;
  14. import java.io.IOException;
  15. import java.util.Date;
  16. import javax.imageio.ImageIO;
  17. import org.apache.commons.lang3.time.DateFormatUtils;
  18. /**
  19. * copy from https://github.com/puyulin/learn_java
  20. * @author <a href="mailto:eko.z@outlook.com">eko.zhan</a>
  21. * @date 2018年9月18日 上午10:11:03
  22. * @version 1.0
  23. */
  24. public class FontImage {
  25. /**
  26. * 根据指定的文本创建图片
  27. * @author eko.zhan at 2018年9月18日 上午10:14:44
  28. * @param text
  29. * @throws WatermarkException
  30. */
  31. public static File createImage(String text) throws WatermarkException {
  32. Font font = new Font("宋体", Font.PLAIN, 100);
  33. int[] arr = getWidthAndHeight(text, font);
  34. int width = arr[0];
  35. int height = arr[1];
  36. // 创建图片
  37. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//创建图片画布
  38. // Graphics gs = image.getGraphics();
  39. // Graphics2D g = (Graphics2D)gs;
  40. // g.setColor(Color.WHITE); // 先用白色填充整张图片,也就是背景
  41. Graphics2D g = image.createGraphics();
  42. // 增加下面代码使得背景透明
  43. image = g.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  44. g.dispose();
  45. g = image.createGraphics();
  46. // 背景透明代码结束
  47. // g.fillRect(0, 0, width, height);//画出矩形区域,以便于在矩形区域内写入文字
  48. g.setColor(new Color(242, 242, 242));// 再换成黑色,以便于写入文字
  49. g.setFont(font);// 设置画笔字体
  50. g.translate(10, 10);
  51. // g.rotate(0.1*Math.PI);//旋转
  52. g.rotate(0.16);
  53. g.drawString(text, 0, font.getSize());// 画出一行字符串
  54. g.dispose();
  55. String property = System.getProperty("java.io.tmpdir");
  56. File imageFile = new File(property + "/kbs-watermark-" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss") + ".png");
  57. try {
  58. // 输出png图片
  59. ImageIO.write(image, "png", imageFile);
  60. } catch (IOException e) {
  61. throw new WatermarkException("IOException", e);
  62. }
  63. return imageFile;
  64. }
  65. private static int[] getWidthAndHeight(String text, Font font) {
  66. Rectangle2D r = font.getStringBounds(text, new FontRenderContext(AffineTransform.getScaleInstance(1, 1), false, false));
  67. int unitHeight = (int) Math.floor(r.getHeight());//
  68. // 获取整个str用了font样式的宽度这里用四舍五入后+1保证宽度绝对能容纳这个字符串作为图片的宽度
  69. int width = (int) Math.round(r.getWidth()) + 20;
  70. // 把单个字符的高度+3保证高度绝对能容纳字符串作为图片的高度
  71. int height = unitHeight + 20;
  72. // System.out.println("width:" + width + ", height:" + height);
  73. return new int[]{width, height*2};
  74. }
  75. }