WordToPic.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.jeeplus.common.utils;
  2. import java.awt.*;
  3. import java.awt.image.BufferedImage;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.OutputStream;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import javax.imageio.ImageIO;
  11. import sun.swing.SwingUtilities2;
  12. public class WordToPic {
  13. //
  14. public static int getLength(String text) {
  15. int length = 0;
  16. for (int i = 0; i < text.length(); i++) {
  17. if (new String(text.charAt(i) + "").getBytes().length > 1) {
  18. length += 2;
  19. } else {
  20. length += 1;
  21. }
  22. }
  23. return length / 2;
  24. }
  25. public static void TextToPic(String text, int width, int height,int fontSize,String filepath) {
  26. try {
  27. File file = new File(filepath);
  28. Font font = new Font("微软雅黑", 0, fontSize);//sans
  29. BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  30. Graphics2D g2 = bi.createGraphics();
  31. g2.setBackground(new Color(0,188,212));
  32. g2.clearRect(0, 0, width, height);
  33. //btn2文本
  34. g2.setColor(Color.white);
  35. g2.setFont(font);
  36. g2.drawString(text, (width - (text.length() * fontSize)) / 2 + 0,(height - fontSize) / 2 +45);
  37. g2.dispose();
  38. /*Graphics2D g2 = bi.createGraphics();
  39. g2.setColor(new Color(255, 120, 89));
  40. //g2.setBackground(new Color(56,110,162));
  41. g2.clearRect(0, 0, width, height);
  42. g2.setFont(font);
  43. g2.setColor(Color.white);
  44. g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.3f));
  45. g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  46. printString(g2, text, (width - (text.length() * fontSize)) / 2 + 0,(height - fontSize) / 2 +45, fontSize);
  47. g2.dispose();*/
  48. ImageIO.write(bi, "png", file);
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public static void TextToAliyunPic(String text, int width, int height, int fontHeight,Color bground, ByteArrayOutputStream outputStream) {
  54. try {
  55. Font font = new Font("微软雅黑", 0, fontHeight);//sans
  56. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  57. Graphics2D gd = buffImg.createGraphics();
  58. //设置透明 start
  59. buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  60. gd=buffImg.createGraphics();
  61. //设置透明 end
  62. gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体
  63. gd.setColor(Color.black); //设置颜色
  64. gd.drawString(text, width/2-fontHeight*text.length()/2,fontHeight);
  65. ImageIO.write(buffImg, "png", outputStream);
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }
  70. private static void printString(Graphics2D g2d, String str, int x, int y,int fontSize) {
  71. FontMetrics metrics = SwingUtilities2.getFontMetrics(null,g2d.getFont());
  72. for (char ca : str.toCharArray()) {
  73. int px = metrics.stringWidth("" + ca);
  74. g2d.drawString("" + ca, x + (fontSize - px) / 2, y);
  75. x += fontSize;
  76. }
  77. }
  78. public static String getDate() {
  79. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
  80. return formatter.format(new Date());
  81. }
  82. public static void main(String[] args)
  83. {
  84. BufferedImage imgMap = drawTranslucentStringPic(400, 80, 36,"欢迎访问我的博客");
  85. File imgFile=new File("D://www.cxyapi.com.png");
  86. try
  87. {
  88. ImageIO.write(imgMap, "PNG", imgFile);
  89. } catch (IOException e)
  90. {
  91. e.printStackTrace();
  92. }
  93. System.out.println("生成完成");
  94. }
  95. public static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight,String drawStr)
  96. {
  97. try
  98. {
  99. BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  100. Graphics2D gd = buffImg.createGraphics();
  101. //设置透明 start
  102. buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  103. gd=buffImg.createGraphics();
  104. //设置透明 end
  105. gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体
  106. gd.setColor(Color.ORANGE); //设置颜色
  107. // gd.drawRect(0, 0, width - 1, height - 1); //画边框
  108. gd.drawString(drawStr, width/2-fontHeight*drawStr.length()/2,fontHeight); //输出文字(中文横向居中)
  109. return buffImg;
  110. } catch (Exception e) {
  111. return null;
  112. }
  113. }
  114. }