1,添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. <version>2.3.2.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-autoconfigure</artifactId>
  10. <version>2.3.2.RELEASE</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.google.zxing</groupId>
  14. <artifactId>core</artifactId>
  15. <version>2.3.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.google.zxing</groupId>
  19. <artifactId>javase</artifactId>
  20. <version>2.3.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>com.github.liuyueyi.media</groupId>
  24. <artifactId>qrcode-plugin</artifactId>
  25. <version>2.5.2</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>commons-lang</groupId>
  29. <artifactId>commons-lang</artifactId>
  30. <version>2.6</version>
  31. </dependency>
  32. </dependencies>

2,application.yml配置上传文件大小

  1. spring:
  2. servlet:
  3. multipart:
  4. max-file-size: 50MB
  5. max-request-size: 100MB

3,创建QrCodeUtil类,生成base64编码,前面拼接”data:image/png;base64,”

  1. package com.huan.util;
  2. import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeGenWrapper;
  3. import com.github.hui.quick.plugin.qrcode.wrapper.QrCodeOptions;
  4. import com.google.zxing.WriterException;
  5. import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
  6. import java.awt.*;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. /**
  10. * @author zyhstart
  11. * @create 2021-10-04-14:57
  12. */
  13. public class QrCodeUtil {
  14. //普通二维码
  15. public static String normal(String text) throws IOException, WriterException {
  16. return QrCodeGenWrapper.of(text)
  17. .asString();
  18. }
  19. //logo二维码
  20. public static String logo(String text, InputStream logoFile) throws IOException, WriterException {
  21. return QrCodeGenWrapper.of(text)
  22. .setLogo(logoFile) // logo地址 支持网络图片,本地相对路劲图片,本地绝对路径图片
  23. .setLogoRate(7)
  24. .setLogoStyle(QrCodeOptions.LogoStyle.ROUND)
  25. .asString();
  26. }
  27. //logo二维码
  28. public static String logo2(String text, String logoPath) throws IOException, WriterException {
  29. return QrCodeGenWrapper.of(text)
  30. .setLogo(logoPath) //logo地址 支持网络图片,本地相对路劲图片,本地绝对路径图片
  31. .setBgH(400).setBgW(400)
  32. .setLogoStyle(QrCodeOptions.LogoStyle.ROUND) //圆角
  33. .setLogoBgColor(0xfffefefe)
  34. .setLogoBorderBgColor(0xffc7c7c7)
  35. .setLogoBorder(true)
  36. .asString();
  37. }
  38. //彩色二维码
  39. public static String color(String text) throws IOException, WriterException {
  40. return QrCodeGenWrapper.of(text)
  41. .setDrawPreColor(Color.RED)
  42. .asString();
  43. }
  44. //背景色二维码
  45. public static String bg(String text,InputStream bgFile) throws IOException, WriterException {
  46. return QrCodeGenWrapper.of(text)
  47. .setBgImg(bgFile)
  48. .setBgStyle(QrCodeOptions.BgImgStyle.PENETRATE)
  49. .setBgH(500)
  50. .setBgW(500)
  51. .setW(500)
  52. .setH(500)
  53. .asString();
  54. }
  55. //特殊二维码
  56. public static String style(String text) throws IOException, WriterException {
  57. return QrCodeGenWrapper.of(text)
  58. .setBgH(500)
  59. .setBgW(500)
  60. .setW(500)
  61. .setH(500)
  62. .setDrawEnableScale(true)
  63. .setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE)
  64. .asString();
  65. }
  66. //图片填充二维码
  67. public static String fill(String text,InputStream bgFile) throws IOException, WriterException {
  68. return QrCodeGenWrapper.of(text)
  69. .setW(500)
  70. .setH(500)
  71. .setDrawEnableScale(true)
  72. .setErrorCorrection(ErrorCorrectionLevel.H)
  73. .setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)
  74. .addImg(1,1,bgFile)
  75. .asString();
  76. }
  77. //gif二维码
  78. public static String gif(String text,InputStream bgFile) throws IOException, WriterException {
  79. return QrCodeGenWrapper.of(text)
  80. .setW(500)
  81. .setH(500)
  82. .setBgImg(bgFile)
  83. .setBgOpacity(0.5f)
  84. .setPicType("gif")
  85. .asString();
  86. }
  87. }