Springboot使用zxing實現二維碼生成和解析
今天在看一個開源項目的時候發(fā)現一個工具類,一個簡單的生成二維碼的工具類,測試了下很是方便。
雖然在平常的開發(fā)中沒有使用過,為了以后的場景做個備份
1、簡介
GitHub 開源地址: github.com/zxing/zxing
zxing 二進制包下載地址:repo1.maven.org/maven2/com/google/zxing
zxing Maven 倉庫地址:mvnrepository.com/artifact/com.google.zxing
ZXing支持各種條形碼,二維碼掃描,由多個模塊組成, 而且支持PC端,移動端。
2、做個例子
2.1 添加依賴
如果要生成二維碼圖片,那么我們只需要它的核心庫即可
如果你想通過命令行讀取二維碼圖片,那么我們需要使用它的JavaSE庫。您可以為其添加以下依賴項。
<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>
2.2 工具類
package com.pdool.demo;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* 二維碼生成工具
* </pre>
*/
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
/**
* 根據二維矩陣的碎片 生成對應的二維碼圖像緩沖
*
* @param matrix 二維矩陣的碎片 包含 寬高 行,字節(jié)
* @return 二維碼圖像緩沖
* @see BitMatrix
*/
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
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, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
/**
* 二維碼生成文件
*
* @param matrix 二維矩陣的碎片 包含 寬高 行,字節(jié)
* @param format 格式
* @param file 保持的文件地址
* @throws IOException 文件保存異常
*/
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
/**
* 二維碼生成流
*
* @param matrix 二維矩陣的碎片 包含 寬高 行,字節(jié)
* @param format 格式
* @param stream 保持的文件輸出流
* @throws IOException 文件保存異常
*/
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
/**
* 二維碼信息寫成JPG文件
*
* @param content 二維碼信息
* @param fileUrl 文件地址
*/
public static void writeInfoToJpgFile(String content, String fileUrl) {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = multiFormatWriter.encode(content,
BarcodeFormat.QR_CODE, 250, 250, hints);
File file1 = new File(fileUrl);
MatrixToImageWriter.writeToFile(bitMatrix, "jpg", file1);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 二維碼信息寫成JPG BufferedImage
*
* @param content 二維碼信息
* @return JPG BufferedImage
*/
public static BufferedImage writeInfoToJpgBuff(String content) {
BufferedImage re = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
BitMatrix bitMatrix = multiFormatWriter.encode(content,
BarcodeFormat.QR_CODE, 250, 250, hints);
re = MatrixToImageWriter.toBufferedImage(bitMatrix);
}
catch (Exception e) {
e.printStackTrace();
}
return re;
}
public static void main(String[] args) throws IOException {
}
}
2.3 測試
這里直接上測試代碼
public static void main(String[] args) throws IOException {
writeInfoToJpgFile("關注我公眾號:程序這點事","E:\\work\\test.jpg");
}
文章中不讓貼二維碼,測試一下你就知道,這里直接使用支付寶的識別

到此這篇關于Springboot使用zxing實現二維碼生成和解析的文章就介紹到這了,更多相關Springboot zxing二維碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring Boot Logging Level設置為off時的Bug
這篇文章主要介紹了Spring Boot Logging Level設置為off時的Bug,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot實現https雙向傳輸協(xié)議的示例代碼
本文主要介紹了springboot實現https雙向傳輸協(xié)議的示例代碼,包含配置證書和私鑰路徑、調用請求方法等步驟,具有一定的參考價值,感興趣的可以了解一下2025-03-03
Java實戰(zhàn)項目練習之球館在線預約系統(tǒng)的實現
理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+maven+freemark+Mysql實現一個球館在線預約系統(tǒng),大家可以在過程中查缺補漏,提升水平2022-01-01
Java中String的JdbcTemplate連接SQLServer數據庫的方法
這篇文章主要介紹了Java中String的JdbcTemplate連接SQLServer數據庫的方法,在研發(fā)過程中我們需要與其他系統(tǒng)對接的場景,連接SQLServer拉取數據,所以就用jdbc連接數據庫的方式連接外部數據源,需要的朋友可以參考下2021-10-10

