JAVA 生成二维码 并设置 失效机制
1.前言:我这里设置的失效机制用数据库记录每一张二维码的实效时间,二维码生成后上传至OSS
1.1 数据库设计
CREATE TABLE `qz_date` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
`uuid` varchar(255) DEFAULT NULL COMMENT '二维码标号',
`url` varchar(255) DEFAULT NULL COMMENT '二维码地址',
`end_time` datetime DEFAULT NULL COMMENT '失效时间',
`created_at` datetime DEFAULT NULL COMMENT '创建时间',
`last_modified` datetime DEFAULT NULL COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4;
2 二维码生成
2.1 导入以来
<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>
2.2 生成和解析二维码工具类
@Slf4j
public class QRCodeUtils {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
public static BufferedImage createImage(String content, String imgPath,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_COD

本文介绍如何使用JAVA生成带有失效机制的二维码,并将其上传至OSS。文章详细描述了数据库设计、二维码生成及解析的方法,并提供了具体实现的代码示例。
最低0.47元/天 解锁文章





