Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

148 строки
5.2 KiB

  1. package com.iformall.utils;
  2. import javax.imageio.ImageIO;
  3. import java.awt.*;
  4. import java.awt.font.FontRenderContext;
  5. import java.awt.geom.Rectangle2D;
  6. import java.awt.image.BufferedImage;
  7. import java.io.File;
  8. /**
  9. * Created by Stormeye on 2017/8/7.
  10. */
  11. public class QRCodeUtils {
  12. public static String PAGE_URL = "pages/index/index";
  13. public static final int QR_WIDTH = 430;
  14. public static final int QR_HIGHT = 430;
  15. public static final int QR_FONT_SIZE = 19;
  16. public static final int QR_H_TIP = 90;
  17. public static final int QR_H_TIP2 = 120;
  18. /**
  19. * 二维码 + 文字1
  20. *
  21. * @param oldimg
  22. * @param newimg
  23. * @param pressText
  24. */
  25. public static void graphicsGeneration(File oldimg, File newimg, String pressText) {
  26. int H_tip = QR_H_TIP; // 文字的高度
  27. int imageWidth = QR_WIDTH; // 图片的宽度
  28. int imageHeight = QR_HIGHT + QR_H_TIP; // 图片的高度
  29. BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
  30. // 设置图片的背景色
  31. Graphics2D main = image.createGraphics();
  32. main.setColor(Color.white);
  33. main.fillRect(0, 0, imageWidth, imageHeight);
  34. Graphics mainPic = image.getGraphics();
  35. BufferedImage bimg = null;
  36. try {
  37. bimg = ImageIO.read(oldimg);
  38. if (bimg != null) {
  39. mainPic.drawImage(bimg, 0, 0, QR_WIDTH, QR_HIGHT, null);
  40. mainPic.dispose();
  41. }
  42. Graphics2D tip = image.createGraphics();
  43. // 设置区域颜色
  44. tip.setColor(Color.white);
  45. // 填充区域并确定区域大小位置
  46. tip.fillRect(0, QR_HIGHT, QR_WIDTH, H_tip);
  47. // 设置字体颜色,先设置颜色,再填充内容
  48. tip.setColor(Color.black);
  49. // 设置字体
  50. Font tipFont = new Font("宋体", Font.PLAIN, QR_FONT_SIZE);
  51. tip.setFont(tipFont);
  52. FontRenderContext context = tip.getFontRenderContext();
  53. Rectangle2D bounds = tipFont.getStringBounds(pressText, context);
  54. double x = (QR_WIDTH - bounds.getWidth()) / 2;
  55. tip.drawString(pressText, (int) x, QR_HIGHT + 50);
  56. // 分割线
  57. tip.drawLine(0, QR_HIGHT, QR_WIDTH, QR_HIGHT);
  58. //FileOutputStream out = new FileOutputStream(newimg);
  59. ImageIO.write(image, "png", newimg);
  60. //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  61. //encoder.encode(image);
  62. //out.close();
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. System.out.println(e);
  66. }
  67. }
  68. /**
  69. * 二维码 + 文字1 + 文字2
  70. *
  71. * @param oldimg
  72. * @param newimg
  73. * @param pressText1
  74. * @param pressText2
  75. */
  76. public static void graphicsGeneration2(File oldimg, File newimg, String pressText1, String pressText2) {
  77. int H_tip = QR_H_TIP2; // 文字的高度
  78. int imageWidth = QR_WIDTH; // 图片的宽度
  79. int imageHeight = QR_HIGHT + QR_H_TIP2; // 图片的高度
  80. BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
  81. // 设置图片的背景色
  82. Graphics2D main = image.createGraphics();
  83. main.setColor(Color.white);
  84. main.fillRect(0, 0, imageWidth, imageHeight);
  85. Graphics mainPic = image.getGraphics();
  86. BufferedImage bimg = null;
  87. try {
  88. bimg = ImageIO.read(oldimg);
  89. if (bimg != null) {
  90. mainPic.drawImage(bimg, 0, 0, QR_WIDTH, QR_HIGHT, null);
  91. mainPic.dispose();
  92. }
  93. Graphics2D tip = image.createGraphics();
  94. // 设置区域颜色
  95. tip.setColor(Color.white);
  96. // 填充区域并确定区域大小位置
  97. tip.fillRect(0, QR_HIGHT, QR_WIDTH, H_tip);
  98. // 设置字体颜色,先设置颜色,再填充内容
  99. tip.setColor(Color.black);
  100. // 设置字体
  101. Font tipFont = new Font("粗体", Font.BOLD, QR_FONT_SIZE);
  102. tip.setFont(tipFont);
  103. FontRenderContext context = tip.getFontRenderContext();
  104. Rectangle2D bounds1 = tipFont.getStringBounds(pressText1, context);
  105. double x = (QR_WIDTH - bounds1.getWidth()) / 2;
  106. tip.drawString(pressText1, (int) x, QR_HIGHT + 50);
  107. Rectangle2D bounds2 = tipFont.getStringBounds(pressText2, context);
  108. x = (QR_WIDTH - bounds2.getWidth()) / 2;
  109. tip.drawString(pressText2, (int) x, QR_HIGHT + 90);
  110. //FileOutputStream out = new FileOutputStream(newimg);
  111. ImageIO.write(image, "png", newimg);
  112. //JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  113. //encoder.encode(image);
  114. //out.close();
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. System.out.println(e);
  118. }
  119. }
  120. public static void main(String[] args) {
  121. File souce = new File("./uploads/null.png");
  122. File dest = new File("./uploads/new.png");
  123. graphicsGeneration(souce, dest, "长融世贸广场停车专用");
  124. }
  125. }