spring二维码生成 存入数据库

pom.xml引入依赖

<!-- 二维码相关依赖 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>core</artifactId>
    <version>3.3.3</version>
</dependency>

<!-- 二维码相关依赖 -->
<dependency>
    <groupId>com.google.zxing</groupId>
    <artifactId>javase</artifactId>
    <version>3.3.3</version>
</dependency>

<!-- Hutool工具类 -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.6.3</version>
</dependency>



主方法 
public class Main {

    public static void main(String[] args) throws IOException {

        String data = "Hello World";// 内容   微信不支持中文显示
        int width1 = 150;// QR码宽度
        int height1 = 150;// QR码高度
        
        String directoryPath = "D:\\RQcodeStoragePath";
        //判断文件夹是否已存在
        File directory = new File(directoryPath);
        if (!directory.exists()){
            directory.mkdir();
        }
        UUID uuid = UUID.randomUUID();
        String filePath1 = directoryPath + "\\" + uuid + goods.getName() + ".png";// QR码存放路径

        generateQrCode(data, width1, height1, filePath1);
        //将二维码转换成Base64字符串(java类型为String   数据库类型为mediumtext)
        String encodeToString =         Base64.getEncoder().encodeToString(toByteArray(directoryPath + "\\" + uuid +         goods.getName() + ".png"));
        //将带有二维码Base64字符串存入数据库中
        String base64Model = "data:image/png;base64,"+encodeToString;

                //后面sql根据实际情况写

    }
}
//Base64转码
private static byte[] toByteArray(String fileName) throws IOException {
    File file = new File(fileName);
    FileInputStream inputStream = new FileInputStream(file);
    byte[] bytes = new byte[(int) file.length()];
    inputStream.read(bytes);
    inputStream.close();
    return bytes;
}

生成二维码工具类

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QRCodeUtil {

    public static void generateQrCode(String data, int width, int height, String filePath) {
        try {
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置字符编码
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 错误纠正级别
            hints.put(EncodeHintType.MARGIN, 1); // 二维码边距

            MultiFormatWriter writer = new MultiFormatWriter();
            BitMatrix bitMatrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height, hints);

            // 创建BufferedImage对象来表示QR码
            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) ? Color.BLACK.getRGB() : Color.WHITE.getRGB());
                }
            }

            // 将QR码保存到文件
            File qrCodeFile = new File(filePath);
            ImageIO.write(image, "png", qrCodeFile);
            log.info("QR码已生成并保存到: {}", filePath);
        } catch (Exception e) {
            log.error("QR码生成失败:{}", e.getMessage());
        }
    }

}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值