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

然后用手机扫描二维码

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

相关推荐
并不喜欢吃鱼2 分钟前
从零开始C++----四.vector的使用与底层实现
开发语言·c++
CQU_JIAKE2 分钟前
4.17【a]
java
Devin~Y4 分钟前
从Spring Boot到Spring AI:音视频AIGC内容社区Java大厂面试三轮连环问(含Kafka/Redis/安全/可观测性答案)
java·spring boot·redis·spring cloud·kafka·spring security·resilience4j
Rsun045515 分钟前
14、Java 模板方法模式从入门到实战
java·python·模板方法模式
二妹的三爷12 分钟前
私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)
spring boot·后端·ui
沐雪轻挽萤12 分钟前
17. C++17新特性-并行算法 (Parallel Algorithms)
java·开发语言·c++
墨澜逸客13 分钟前
华胥祭坛志---文/墨澜逸客
开发语言·深度学习·学习·百度·php·学习方法·新浪微博
覆东流15 分钟前
第3天:Python print深入与格式化输出
开发语言·后端·python
StockTV24 分钟前
SpringBoot对接黄金白银期货数据API
java·spring boot·后端
加号334 分钟前
C# 基于MD5实现密码加密功能,附源码
开发语言·c#·密码加密