spring boot 生成网站二维码返回到前端示例代码 与依赖
时间: 2023-03-27 22:04:09 浏览: 67
您好,以下是 Spring Boot 生成网站二维码返回到前端的示例代码和依赖:
依赖:
```xml
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.4.</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.4.</version>
</dependency>
```
示例代码:
```java
@GetMapping("/qrcode")
public void generateQRCode(HttpServletResponse response) throws Exception {
String url = "https://www.example.com"; // 网站地址
int width = 300; // 二维码宽度
int height = 300; // 二维码高度
String format = "png"; // 二维码格式
// 生成二维码
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map<EncodeHintType, Object> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, width, height, hints);
// 输出二维码到前端
ServletOutputStream outputStream = response.getOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream);
outputStream.flush();
outputStream.close();
}
```
希望对您有帮助。