Java实现生成二维码

Java实现生成二维码

1.引入谷歌提供jar包
在这里插入图片描述
2.后台service层代码:

public BufferedImage coachQRCode(Long coachId) throws WriterException {
        Map hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        //二维码中返回数据
        CoachQRDto qr =  new CoachQRDto();
        qr.setCoachId(coachId);
        //3分钟过期时间
        qr.setExpireTime(Dates.addMinute(new Date(),3).getTime());
        //根据实际业务编写
//      BitMatrix qrcode = new QRCodeWriter().encode(Jsons.toJSONString(qr), BarcodeFormat.QR_CODE, 300, 300);
        //可直接写入网址扫码即可跳转页面
        BitMatrix qrcode = new QRCodeWriter().encode("https://www.baidu.com", BarcodeFormat.QR_CODE, 300, 300);
        return MatrixToImageWriter.toBufferedImage(qrcode);
    }

3.controller层代码

 @GetMapping(value = "/coachQRCode", produces = MediaType.IMAGE_JPEG_VALUE)
    public BufferedImage coachQRCode(Long coachId) throws WriterException {
        return bizCoachService.coachQRCode(coachId);
    }

4.postman调用结果:
在这里插入图片描述
前端获取
在这里插入图片描述写入地址即可。注意请加上协议及完整地址

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值