CodeUtil.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.iamberry.wechat.utils;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.EncodeHintType;
  4. import com.google.zxing.MultiFormatWriter;
  5. import com.google.zxing.WriterException;
  6. import com.google.zxing.common.BitMatrix;
  7. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  8. import org.apache.commons.lang3.StringUtils;
  9. import javax.imageio.ImageIO;
  10. import java.awt.*;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.util.Date;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * 画制定logo和制定描述的二维码
  18. */
  19. public class CodeUtil {
  20. private static final int QRCOLOR = 0xFF000000; // 默认是黑色
  21. private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色
  22. private static final int WIDTH = 400; // 二维码宽
  23. private static final int HEIGHT = 400; // 二维码高
  24. // 用于设置QR二维码参数
  25. private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
  26. private static final long serialVersionUID = 1L;
  27. {
  28. put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息
  29. put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
  30. put(EncodeHintType.MARGIN, 0);
  31. }
  32. };
  33. public static void main(String[] args) throws WriterException {
  34. File logoFile = new File("C://Users/Administrator/Desktop/code/05.png");
  35. logoFile = null;
  36. File QrCodeFile = new File("C://Users/Administrator/Desktop/code/06.png");
  37. String url = "https://www.baidu.com/";
  38. String note = "访问百度连接";
  39. note = null;
  40. long x = new Date().getTime();
  41. drawLogoQRCode(logoFile, QrCodeFile, url, note);
  42. long y = new Date().getTime();
  43. System.out.println("生成二维码所需要的时间" + (y-x));
  44. }
  45. /**
  46. * 生成带logo的二维码图片
  47. * @param logoFile logo 如果不需要就为null
  48. * @param codeFile 存储位置
  49. * @param qrUrl 二维码内容
  50. * @param note 文字,如果不需要就为null
  51. */
  52. public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {
  53. try {
  54. MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
  55. // 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
  56. BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
  57. BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  58. // 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色
  59. for (int x = 0; x < WIDTH; x++) {
  60. for (int y = 0; y < HEIGHT; y++) {
  61. image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
  62. }
  63. }
  64. int width = image.getWidth();
  65. int height = image.getHeight();
  66. if (logoFile != null && logoFile.exists()) {
  67. // 构建绘图对象
  68. Graphics2D g = image.createGraphics();
  69. // 读取Logo图片
  70. BufferedImage logo = ImageIO.read(logoFile);
  71. // 开始绘制logo图片
  72. g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
  73. g.dispose();
  74. logo.flush();
  75. }
  76. // 自定义文本描述
  77. if (StringUtils.isNotEmpty(note)) {
  78. // 新的图片,把带logo的二维码下面加上文字
  79. BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);
  80. Graphics2D outg = outImage.createGraphics();
  81. // 画二维码到新的面板
  82. outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
  83. // 画文字到新的面板
  84. outg.setColor(Color.BLACK);
  85. outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号
  86. int strWidth = outg.getFontMetrics().stringWidth(note);
  87. if (strWidth > 399) {
  88. // //长度过长就截取前面部分
  89. // 长度过长就换行
  90. String note1 = note.substring(0, note.length() / 2);
  91. String note2 = note.substring(note.length() / 2, note.length());
  92. int strWidth1 = outg.getFontMetrics().stringWidth(note1);
  93. int strWidth2 = outg.getFontMetrics().stringWidth(note2);
  94. outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);
  95. BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);
  96. Graphics2D outg2 = outImage2.createGraphics();
  97. outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);
  98. outg2.setColor(Color.BLACK);
  99. outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号
  100. outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);
  101. outg2.dispose();
  102. outImage2.flush();
  103. outImage = outImage2;
  104. } else {
  105. outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字
  106. }
  107. outg.dispose();
  108. outImage.flush();
  109. image = outImage;
  110. }
  111. image.flush();
  112. ImageIO.write(image, "png", codeFile);
  113. } catch (Exception e) {
  114. e.printStackTrace();
  115. }
  116. }
  117. }