Springboot使用zxing實(shí)現(xiàn)二維碼生成和解析
今天在看一個(gè)開源項(xiàng)目的時(shí)候發(fā)現(xiàn)一個(gè)工具類,一個(gè)簡(jiǎn)單的生成二維碼的工具類,測(cè)試了下很是方便。
雖然在平常的開發(fā)中沒有使用過(guò),為了以后的場(chǎng)景做個(gè)備份
1、簡(jiǎn)介
GitHub 開源地址: github.com/zxing/zxing
zxing 二進(jìn)制包下載地址:repo1.maven.org/maven2/com/google/zxing
zxing Maven 倉(cāng)庫(kù)地址:mvnrepository.com/artifact/com.google.zxing
ZXing支持各種條形碼,二維碼掃描,由多個(gè)模塊組成, 而且支持PC端,移動(dòng)端。
2、做個(gè)例子
2.1 添加依賴
如果要生成二維碼圖片,那么我們只需要它的核心庫(kù)即可
如果你想通過(guò)命令行讀取二維碼圖片,那么我們需要使用它的JavaSE庫(kù)。您可以為其添加以下依賴項(xiàng)。
<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() { } /** * 根據(jù)二維矩陣的碎片 生成對(duì)應(yīng)的二維碼圖像緩沖 * * @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 測(cè)試
這里直接上測(cè)試代碼
public static void main(String[] args) throws IOException { writeInfoToJpgFile("關(guān)注我公眾號(hào):程序這點(diǎn)事","E:\\work\\test.jpg"); }
文章中不讓貼二維碼,測(cè)試一下你就知道,這里直接使用支付寶的識(shí)別
到此這篇關(guān)于Springboot使用zxing實(shí)現(xiàn)二維碼生成和解析的文章就介紹到這了,更多相關(guān)Springboot zxing二維碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
簡(jiǎn)單了解SpringBoot過(guò)濾器及使用方式
這篇文章主要介紹了簡(jiǎn)單了解SpringBoot過(guò)濾器及使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04java構(gòu)建OAuth2授權(quán)服務(wù)器
本文主要介紹了java構(gòu)建OAuth2授權(quán)服務(wù)器,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及Spr
這篇文章主要介紹了淺談SpringMVC的攔截器(Interceptor)和Servlet 的過(guò)濾器(Filter)的區(qū)別與聯(lián)系 及SpringMVC 的配置文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E
這篇文章主要給大家介紹了SpringBoot整合FTP實(shí)現(xiàn)文件傳輸?shù)牟襟E,文中的流程步驟和代碼示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-11-11關(guān)于application.yml基礎(chǔ)配置以及讀取方式
這篇文章主要介紹了關(guān)于application.yml基礎(chǔ)配置以及讀取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07