java Spring Boot生成图片二维码

首先 我们要引入依赖

pom.xml中插入

xml 复制代码
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.4.1</version>
</dependency>

然后 直接在接口类中写入函数

java 复制代码
@GetMapping(value = "/qrcode/{text}")
public void generateQRCode(@PathVariable("text") String text, HttpServletResponse response) throws IOException, WriterException {
    response.setContentType(MediaType.IMAGE_PNG_VALUE);
    OutputStream outputStream = response.getOutputStream();

    Map<EncodeHintType, Object> hints = new HashMap<>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

    BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, 200, 200, hints);
    MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream);

    outputStream.flush();
    outputStream.close();
}

然后 直接访问 端口+前缀配置/qrcode/要生成的二维码文本内容

例如 这里我访问

http://localhost/books/qrcode/1992

然后 接口就会返回一个二维码 给我们 这里 因为CSDN平台不支持 禁止二维码图片 我就用画笔破获一下 免得被和谐了

然后用手机扫描二维码

就可以带出我们文本的内容

相关推荐
lssjzmn6 分钟前
Spring Web 异步响应实战:从 CompletableFuture 到 ResponseBodyEmitter 的全链路优化
java·前端·后端·springboot·异步·接口优化
new_daimond19 分钟前
二级缓存在实际项目中的应用
java
一只乔哇噻26 分钟前
java后端工程师进修ing(研一版 || day41)
java·开发语言·学习·算法
愿时间能学会宽恕27 分钟前
SpringBoot后端开发常用工具详细介绍——SpringSecurity认证用户保证安全
spring boot·后端·安全
钮钴禄·爱因斯晨36 分钟前
深入剖析LLM:从原理到应用与挑战
开发语言·人工智能
User_芊芊君子44 分钟前
【Java】设计模式——单例、工厂、代理模式
java·设计模式·代理模式
六点半8881 小时前
【C++】C++11 篇二
开发语言·c++
2301_803554521 小时前
正向代理,反向代理,负载均衡还有nginx
java·nginx·负载均衡
要开心吖ZSH1 小时前
软件设计师备考-(十六)数据结构及算法应用(重要)
java·数据结构·算法·软考·软件设计师
DDDDDDDRDDR1 小时前
C++容器:list
开发语言·c++·stl