Java 生成二维码
二维码是指用某种特定的几何图形按一定规律在平面分布的黑白相间的图形记录数据符号信息的图形。
优点
- 高密度编码,信息容量大
- 编码范围广
- 容错能力强
- 译码可靠性高
- 可引入加密措施
- 成本低,易制作,持久耐用
缺点
- 成为手机病毒、钓鱼网站传播的新渠道
- 信息泄露
二维码标准
- PDF417:不支持中文
- DM:专利未公开,需支付专利费用
- QR Code:专利公开,支持中文
QR Code(Quick Response Code) 相较于其他标准的二维码,具有识读速度快、数据密度大、占用空间小的优势。
QR Code 的纠错能力
- L 级:约可纠错7%的数据码字
- M 级:约可纠错15%的数据码字
- Q 级:约可纠错25%的数据码字
- H 级:约可纠错30%的数据码字
生成方法
1 zxing
- 引入依赖
implementation 'com.google.zxing:core:3.3.3'
implementation 'com.google.zxing:javase:3.3.3'
- 创建二维码方法
public static void createQRCodeByZXing(String format, String content, Path file) {
Map<EncodeHintType, Object> hint = new HashMap<>(4);
hint.put(EncodeHintType.CHARACTER_SET, "utf-8");
hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hint.put(EncodeHintType.MARGIN, 2);
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 500, 500, hint);
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
- 测试方法
public class QrCodeUtilsTest {
private static final String format = "png";
private static final String content = "https://github.com/leifchen";
@Test
public void createQRCodeByZXing() {
Path file = new File("E:/zxing.png").toPath();
QrCodeUtils.createQRCodeByZXing(format, content, file);
}
}
- 二维码图片
2 qrcode
- 引入本地依赖
implementation fileTree(dir:'lib',include:['*.jar'])
- 创建二维码方法
public static void createQRCode(String format, String content, File file) {
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeErrorCorrect('M');
qrcode.setQrcodeEncodeMode('B');
qrcode.setQrcodeVersion(10);
int width = 67 + 12 * (10 - 1);
int height = 67 + 12 * (10 - 1);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.setBackground(Color.WHITE);
graphics.setColor(Color.BLACK);
graphics.clearRect(0, 0, WIDTH, HEIGHT);
// 偏移量
int offset = 2;
byte[] bytes = new byte[0];
try {
bytes = content.getBytes("gb2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (bytes.length > 0 && bytes.length < 120) {
boolean[][] s = qrcode.calQrcode(bytes);
for (int i = 0; i < s.length; i++) {
for (int j = 0; j < s.length; j++) {
if (s[j][i]) {
graphics.fillRect(j * 3 + offset, i * 3 + offset, 3, 3);
}
}
}
}
graphics.dispose();
bufferedImage.flush();
try {
ImageIO.write(bufferedImage, format, file);
} catch (IOException e) {
e.printStackTrace();
}
}
- 测试方法
public class QrCodeUtilsTest {
private static final String format = "png";
private static final String content = "https://github.com/leifchen";
@Test
public void createQRCode() {
File file = new File("E:/qrcode.png");
QrCodeUtils.createQRCode(format, content, file);
}
}
- 二维码图片
3 jquery-qrcode
- 创建二维码方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>QR-Code</title>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.qrcode.min.js"></script>
</head>
<body>
<div id="qrcode"></div>
<script>
$('#qrcode').qrcode({width: 500, height: 500, text: "https://github.com/leifchen"});
</script>
</body>
</html>
- 二维码图片