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

然后用手机扫描二维码

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

相关推荐
plainGeekDev30 分钟前
null 判断 → Kotlin 可空类型
android·java·kotlin
糖拌西瓜皮33 分钟前
Java开发者视角:深入理解Node.js异步编程模型
java·后端·node.js
plainGeekDev34 分钟前
getter/setter → Kotlin 属性
android·java·kotlin
一线大码1 小时前
Smart-Doc 的简单使用
java·后端·restful
MacroZheng2 小时前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端
虚无境2 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·prometheus·webhook
NE_STOP17 小时前
Vide Coding--AI编程工具的选择
java
LDR00618 小时前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言