记事本:Java 通过hutool工具生成二维码

maven依赖

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.4</version>
</dependency>

代码
 

 public static String createdQrCodeBase64(String text,int width,int height,String urlstr) throws MalformedURLException {
        String base64 = "";
        QrConfig qrConfig = new QrConfig(width,height);
        //设置边距
        qrConfig.setMargin(0);
        //设置二维码颜色
        qrConfig.setForeColor(Color.BLACK.getRGB());
        //设置背景色
        qrConfig.setBackColor(Color.WHITE.getRGB());
        //二维码中间图片
        if (com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotBlank(urlstr)) {
            URL url = new URL(urlstr);
            Image image = ImgUtil.getImage(url);
            qrConfig.setImg(image);
            qrConfig.setRatio(5);
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        QrCodeUtil.generate(text,qrConfig, ImgUtil.IMAGE_TYPE_PNG,out);
        byte[] data = out.toByteArray();
        BASE64Encoder base64Encoder = new BASE64Encoder();
        base64 = base64Encoder.encode(data);
        return "data:image/png;base64,"+base64;
    }

方法调用

    public static void main(String[] args) throws MalformedURLException {
        String s = createdQrCodeBase64("创建二维码测试", 200, 200, null);
        System.out.println(s);
    }

输出

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAD8UlEQVR42u3dQRLDIAwDQP7/6fQR
bYolLzMcSWnw5qIBzjnnae7fttvPf/v/vz2+oAMCCCCAAAIIIIAAAggggAACCCCAAAIIIIAAUgxk
eru9wLeBTX9/6fUDCCCAAAIIIIAAAggggAACCCCAAAIIIIAAAshuIOlB13RA24ENeP+AAAIIIIAA
AggggAACCCCAAAIIIIAAAggggABSC6Qg6Fq94QoQQAABBBBAAAEEEEAAAQQQQAABBBBAAAEEEEA2
A0k/uM77BwQQQAABBBBALBAggFggQAABBBBAAAEEEEAAAcTBcakF2r5hKiBoBQQQQAABBBBAAAEE
EEAAAQQQQAABBBBAAAGkGEh6f3uBjT/tHRDjAQFEgQMCiAIFBBBAAAEEEEAAAQQQQAABJLg/2ugC
EvRdXn8EAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQMLXf3fQNR1g+wVEtze8AQIIIIAAAggggAAC
CCCAAAIIIIAAAggggADSDWR6gSjA3UCvX2AECCCAAAIIIIAAAggggAACCCCAAAIIIIAAAkg0kOlB
TTsA8xu/oQwQQAABRAECAggggAACCCCAAGJ+gABifoAAAshiINODtPagcECQ1g4YEEAAAQQQQAAB
BBBAAAEEEEAAAQQQQAABBJDFQNoLZHuB/SGIm76hDBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAQSQ
YCBvF2DAhpjRB58FHKwWPT9AAAEEEEAAAQQQQAABBBBAAAEEEEAAAQQQQLqBbL/gZcEFMKM/UNPn
DwgggAACCCCAAAIIIIAAAggggAACCCCAAAJINpDbQdr0IK89aEz/gAECCCCAAAIIIIAAAggggAAC
CCCAAAIIIIAAAsibQWH7wWPbD55LB/CD3wAEEEAAAQQQQAABBBBAAAEEEEAAAQQQQAABJBjIs7yl
XyB0OyhtDxoBAQQQQAABBBBAAAEEEEAAAQQQQAABBBBAAEnPYc7T3LcDbt8w9of5AQIIIIAAAggg
gAACCCCAAAIIIIAAogABAQSQYiCCvu4NZe1B4Q/mBwgggAACCCCAAAIIIIAAAggggAACCCCAAALI
YiDTDwZrH397fZysCAgggAACCCCAAAIIIIAAAggggAACCCCAAAIIIHOBpBdYelAnKAQEEEAAAQQQ
QAABBBBAAAEEEEAAAQQQQAABBJC9Qd10wOlNUAgIIIAAAggggAACCCCAAAIIIIAAAggggAACSPIf
TH9+exAZ8AECBBBAAAEEEEAAAQQQQAABBBBAAAEEEEAAAaQYyPaDxbYXyG3gA+YHCCCAAAIIIIAA
AggggAACCCCAAAIIIIAAAkju/D70q5VyPttE1QAAAABJRU5ErkJggg==

转完之后的图片

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值