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平台不支持 禁止二维码图片 我就用画笔破获一下 免得被和谐了

然后用手机扫描二维码

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

相关推荐
小毅&Nora18 小时前
【Java线程安全实战】⑨ CompletableFuture的高级用法:从基础到高阶,结合虚拟线程
java·线程安全·虚拟线程
码农小韩18 小时前
基于Linux的C++学习——动态数组容器vector
linux·c语言·开发语言·数据结构·c++·单片机·学习
冰冰菜的扣jio18 小时前
Redis缓存中三大问题——穿透、击穿、雪崩
java·redis·缓存
木风小助理18 小时前
`mapfile`命令详解:Bash中高效的文本至数组转换工具
开发语言·chrome·bash
yyy(十一月限定版)19 小时前
初始matlab
开发语言·matlab
LawrenceLan19 小时前
Flutter 零基础入门(九):构造函数、命名构造函数与 this 关键字
开发语言·flutter·dart
listhi52019 小时前
基于MATLAB的支持向量机(SVM)医学图像分割方法
开发语言·matlab
小璐猪头19 小时前
专为 Spring Boot 设计的 Elasticsearch 日志收集 Starter
java
hui函数19 小时前
如何解决 pip install 编译报错 g++: command not found(缺少 C++ 编译器)问题
开发语言·c++·pip
Tisfy19 小时前
网站访问耗时优化 - 从数十秒到几百毫秒的“零成本”优化过程
服务器·开发语言·性能优化·php·网站·建站