二维码生成

悠悠 2021-12-10 15:39 638阅读 0赞

maven依赖

  1. <!-- 二维码 -->
  2. <dependency>
  3. <groupId>com.google.zxing</groupId>
  4. <artifactId>core</artifactId>
  5. <version>3.3.0</version>
  6. </dependency>

代码

  1. package com.qt.exercise;
  2. import com.google.zxing.BarcodeFormat;
  3. import com.google.zxing.EncodeHintType;
  4. import com.google.zxing.MultiFormatWriter;
  5. import com.google.zxing.common.BitMatrix;
  6. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  7. import javax.imageio.ImageIO;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.geom.RoundRectangle2D;
  11. import java.awt.image.BufferedImage;
  12. import java.io.File;
  13. import java.net.URL;
  14. import java.util.Hashtable;
  15. import static org.apache.catalina.manager.Constants.CHARSET;
  16. /**
  17. * @author: lsq
  18. * @date: 2019/7/11 11:45
  19. */
  20. public class QRCodeUtil {
  21. private static final String FORMAT_NAME = "JPG";
  22. // 二维码尺寸
  23. private static final int QRCODE_SIZE = 275;
  24. /**
  25. * 生成二维码插入到背景图上
  26. * @param content
  27. * @param bgPath
  28. * @param QRCodePath
  29. * @param needCompress
  30. * @return
  31. * @throws Exception
  32. */
  33. public static String encodeBgImage(String content, String bgPath, String QRCodePath,boolean needCompress) throws Exception {
  34. BufferedImage image = QRCodeUtil.createImage(content, bgPath, needCompress);
  35. mkdirs(QRCodePath);
  36. ImageIO.write(image, FORMAT_NAME, new File(QRCodePath));
  37. return QRCodePath;
  38. }
  39. /**
  40. * 生成二维码
  41. * @param content
  42. * @param QRCodePath
  43. * @param needCompress
  44. * @return
  45. * @throws Exception
  46. */
  47. public static String encode(String content, String QRCodePath,boolean needCompress) throws Exception {
  48. BufferedImage image = QRCodeUtil.createImage(content, needCompress);
  49. mkdirs(QRCodePath);
  50. ImageIO.write(image, FORMAT_NAME, new File(QRCodePath));
  51. return QRCodePath;
  52. }
  53. private static BufferedImage createImage(String content, String bgPath, boolean needCompress) throws Exception {
  54. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);
  55. int width = bitMatrix.getWidth();
  56. int height = bitMatrix.getHeight();
  57. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  58. for (int x = 0; x < width; x++) {
  59. for (int y = 0; y < height; y++) {
  60. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
  61. }
  62. }
  63. if (bgPath == null || "".equals(bgPath)) {
  64. return image;
  65. }
  66. // 插入图片
  67. image = QRCodeUtil.insertImage(bgPath, image, needCompress);
  68. return image;
  69. }
  70. private static BufferedImage createImage(String content, boolean needCompress) throws Exception {
  71. BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);
  72. int width = bitMatrix.getWidth();
  73. int height = bitMatrix.getHeight();
  74. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  75. for (int x = 0; x < width; x++) {
  76. for (int y = 0; y < height; y++) {
  77. image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
  78. }
  79. }
  80. return image;
  81. }
  82. //二维码插到背景图上
  83. private static BufferedImage insertImage(String imgPath, BufferedImage source,boolean needCompress) throws Exception {
  84. Image src = ImageIO.read(new File(imgPath)); //本地图片获取
  85. // Image src = ImageIO.read(new URL(imgPath)); //线上图片获取
  86. BufferedImage background = toBufferedImage(src);
  87. int width = source.getWidth(null);
  88. int height = source.getHeight(null);
  89. int backgroundWidth = background.getWidth(null);
  90. // 插入LOGO
  91. Graphics2D graph = background.createGraphics();
  92. int x = (backgroundWidth - width) / 2 - 8;
  93. int y = 500;
  94. graph.drawImage(source, x, y, width, height, null);
  95. Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
  96. graph.setStroke(new BasicStroke(3f));
  97. graph.draw(shape);
  98. graph.dispose();
  99. return background;
  100. }
  101. public static BufferedImage toBufferedImage(Image image) {
  102. if (image instanceof BufferedImage) {
  103. return (BufferedImage) image;
  104. }
  105. // This code ensures that all the pixels in the image are loaded
  106. image = new ImageIcon(image).getImage();
  107. // Create a buffered image using the default color model
  108. int type = BufferedImage.TYPE_INT_RGB;
  109. BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
  110. // Copy image to buffered image
  111. Graphics g = bimage.createGraphics();
  112. // Paint the image onto the buffered image
  113. g.drawImage(image, 0, 0, null);
  114. g.dispose();
  115. return bimage;
  116. }
  117. /**
  118. * 创建文件
  119. * @param destPath
  120. */
  121. public static void mkdirs(String destPath) {
  122. File file = new File(destPath);
  123. // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
  124. if (!file.exists() && !file.isDirectory()) {
  125. file.mkdirs();
  126. }
  127. }
  128. public static void main(String[] args) throws Exception {
  129. System.out.println("begin");
  130. QRCodeUtil qrCodeUtil = new QRCodeUtil();
  131. String content = "http://www.baidu.com";
  132. // String imgPath = "D:/qrCode/123.jpg";
  133. String QRCodePath = "E:/qrcode/" + System.currentTimeMillis() + ".jpg";
  134. // System.out.println(qrCodeUtil.encodeBgImage(content,imgPath,QRCodePath,true)); //生成二维码插入到背景图上
  135. System.out.println(qrCodeUtil.encode(content,QRCodePath,true)); //直接生成二维码
  136. System.out.println("end");
  137. }
  138. }

发表评论

表情:
评论列表 (有 0 条评论,638人围观)

还没有评论,来说两句吧...

相关阅读

    相关 生成

    二维码生成源码 开发前准备第三方包:QRCode.jar 下载链接:[QRCode.jar百度云下载链接][QRCode.jar] 提取码:1pvs ![在这里插入图

    相关 swift生成,扫描

    ~~~写在前面的话~~~ 我之前打算做一个APP,然后把电话号码生成二维码 或者条形码, 用手机扫描,这样,就不用担心会输入错误电话号码了。 在下面是实现的扫描二维码的功能