springboot生成二维码

时间:2021-09-22
本文章向大家介绍springboot生成二维码,主要包括springboot生成二维码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、引入依赖

		<!--生成二维码-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.10</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.4.0</version>
        </dependency>

2、编写service类及方法

@Service
@Slf4j
public class QRCodeService {
    // 自定义参数,这部分是Hutool工具封装的
    private static QrConfig initQrConfig(int width, int height) {
        QrConfig config = new QrConfig(width, height);
        // 设置边距,既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色,既二维码颜色(青色)
        config.setForeColor(Color.black.getRGB());
        // 设置背景色(灰色)
        config.setBackColor(Color.white.getRGB());
        return config;
    }

    /**
     * 生成到文件
     *
     * @param content
     * @param filepath
     */
    public void createQRCode2File(String content, int width, int height, String filepath) {
        try {
            QrCodeUtil.generate(content, initQrConfig(width, height), FileUtil.file(filepath));
            log.info("生成二维码成功, 位置在:{}!", filepath);
        } catch (QrCodeException e) {
            log.error("发生错误! {}!", e.getMessage());
        }
    }

    /**
     * 生成到流
     *
     * @param content
     * @param response
     */
    public void createQRCode2Stream(String content, int width, int height, HttpServletResponse response) {
        try {
            QrCodeUtil.generate(content, initQrConfig(width,height), "png", response.getOutputStream());
            log.info("生成二维码成功!");
        } catch (QrCodeException | IOException e) {
            log.error("发生错误! {}!", e.getMessage());
        }
    }

3、编写模拟controller

@RestController
@Slf4j
public class QRCodeController {
    @Autowired
    private QRCodeService qrCodeService;

    /**
     * 生成流的形式的二维码
     * @param codeContent
     * @param response
     */
    @GetMapping("qrCode")
    public void getQRCode(String codeContent, int width, int height, HttpServletResponse response) {
        try {
            qrCodeService.createQRCode2Stream(codeContent, width, height, response);

            log.info("成功生成二维码!");
        } catch (Exception e) {
            log.error("发生错误, 错误信息是:{}!", e.getMessage());
        }
    }

    /**
     * 生成图片并存放到本地
     * @param codeContent
     */
    @GetMapping("qrCodeByFile")
    public void getQRCodeByFile(String codeContent, int width, int height) {
        String filePath="G:/RPC/erweima/first.png";
        try {
            qrCodeService.createQRCode2File(codeContent,width,height,filePath);
            log.info("成功生成二维码!");
        } catch (Exception e) {
            log.error("发生错误, 错误信息是:{}!", e.getMessage());
        }
    }

原文地址:https://www.cnblogs.com/nlbz/p/15320956.html