SpringBoot生成二维码 扫描并可下载文件

生成二维码 扫描并可下载文件

适当根据自己的业务需求变通,然后就能轻松使用了,不用再浪费大量时间去写二维码生成

pom.xml 依赖

        <!--二维码生成-->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>javase</artifactId>
            <version>3.3.0</version>
        </dependency>

application.yaml

使用了若依 profile 文件路径映射 不是若依的,记得修改映射方式

在这里插入图片描述

Controller

   /**
    * 生成二维码
    * */
    @ApiOperation(value = "生成二维码", notes = "")
    @PostMapping(value = "/qrCode")
    public AjaxResult qrCode(){
   
        try {
   
        	//若依系统封装的Config  不是若依使用  getProfile() 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对您的问题,可以使用以下步骤来实现: 1. 添加依赖:在 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> ``` 2. 创建二维码:使用以下代码生成二维码图片并保存到本地: ```java import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; public class QRCodeUtil { /** * 生成二维码 * * @param content 二维码内容 * @param width 二维码宽度 * @param height 二维码高度 * @param format 二维码格式 * @param outputFile 二维码输出文件 * @throws WriterException * @throws IOException */ public static void createQRCode(String content, int width, int height, String format, File outputFile) throws WriterException, IOException { HashMap<EncodeHintType, Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.MARGIN, 2); BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF); } } ImageIO.write(image, format, outputFile); } } ``` 3. 将二维码图片展示给用户:在 Spring Boot 中可以使用 Thymeleaf 模板引擎来展示二维码图片,具体代码如下: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Sign In</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </head> <body> <div> <img th:src="@{'file://' + ${qrCodeFile.getAbsolutePath()}}" th:alt="二维码"/> </div> </body> </html> ``` 4. 扫描二维码:用户使用扫码器扫描二维码,获取二维码中的内容,完成签到操作。 以上是实现 Spring Boot 生成二维码并用扫码器扫描完成签到的基本步骤,具体实现还需要根据实际业务场景进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值