生成二维码

引入maven依赖

<!-- google二维码生成包 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.0</version>
</dependency>

<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>2.0</version>
</dependency>

 

String code_url="java程序猴";//定义转成二维码的内容
if(code_url==null){
throw new NullPointerException();
}

//生成二维码配置
Map<EncodeHintType,Object> hints=new HashMap<>();
//设置纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
//编码类型
hints.put(EncodeHintType.CHARACTER_SET,"UTF-8");
//bitMatrix对象生成二维码
BitMatrix bitMatrix=new MultiFormatWriter().encode(code_url, BarcodeFormat.QR_CODE,400,400,hints);
OutputStream out=response.getOutputStream();

MatrixToImageWriter.writeToStream(bitMatrix,"png",out);

此种方式是我常用的生成二维码方式  感觉很好用  各位大神请笑纳!

推荐二维码学习地址:

https://coolshell.cn/articles/10590.html

posted @ 2020-05-18 15:10  java程序猴  阅读(158)  评论(0编辑  收藏  举报