Java使用Zxing二維碼生成的簡(jiǎn)單示例
1、二維碼簡(jiǎn)介
二維條形碼是用某種特定的幾何圖形按一定規(guī)律在平面(二維方向上)分布的黑白相間的圖形記錄數(shù)據(jù)符號(hào)信息的,在代碼編制上巧妙地利用構(gòu)成計(jì)算機(jī)內(nèi)部邏輯基礎(chǔ)的“0”、“1”比特流的概念,使用若干個(gè)與二進(jìn)制相對(duì)應(yīng)的幾何形體來(lái)表示文字?jǐn)?shù)值內(nèi)容信息,通過(guò)圖象輸入設(shè)備或光電掃描設(shè)備自動(dòng)識(shí)讀以實(shí)現(xiàn)信息自動(dòng)處理。二維碼具有條碼技術(shù)的一些共性:每種碼制有其特定的字符集;每個(gè)字符占有一定的寬度;具有一定的校驗(yàn)功能等。同時(shí)還具有對(duì)不同行的信息自動(dòng)識(shí)別功能、及處理圖形旋轉(zhuǎn)變化等特點(diǎn)。
二維碼糾錯(cuò)級(jí)別
二維碼糾錯(cuò)級(jí)別指的是在識(shí)別二維碼時(shí),對(duì)于損壞或模糊的二維碼的容錯(cuò)能力。
一般來(lái)說(shuō),二維碼有四個(gè)糾錯(cuò)級(jí)別:
- L (低):可以糾正7%左右的錯(cuò)誤。
- M (中):可以糾正15%左右的錯(cuò)誤。
- Q (高):可以糾正25%左右的錯(cuò)誤。
- H (高):可以糾正30%左右的錯(cuò)誤。
總結(jié):一般來(lái)說(shuō),使用較高的糾錯(cuò)級(jí)別會(huì)導(dǎo)致生成的二維碼更大,但是它的容錯(cuò)能力也會(huì)更強(qiáng)。
2、ZXing簡(jiǎn)介
ZXing(Zebra Crossing)是Google開(kāi)發(fā)的一個(gè)二維碼解析和生成的開(kāi)源庫(kù)。
官網(wǎng)地址:http://code.google.com/p/zxing/
3、示例
通過(guò)Java調(diào)用Zxing實(shí)現(xiàn)二維碼的生成
3.1 搭建一個(gè)maven項(xiàng)目,引入Zxing依賴包
<dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.0</version> </dependency>
3.2 創(chuàng)建QrCodeUtil.java 類
具體實(shí)現(xiàn)代碼如下:
package QrCodeUtil; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Date; import java.util.Hashtable; import javax.imageio.ImageIO; import com.alibaba.druid.util.StringUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; /** * 生成二維碼 */ public class QrCodeUtil { private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static final int margin = 0; private static final int LogoPart = 4; public static void main(String[] args) throws WriterException { //二維碼內(nèi)容 String content = "IT技術(shù)分享社區(qū),一個(gè)有態(tài)度的互聯(lián)網(wǎng)社區(qū)交流平臺(tái)"; String logoPath = "D:\\logo.png"; // 二維碼中間的logo信息 非必須 String format = "jpg"; int width = 120; // 二維碼寬度 int height = 120;// 二維碼高度 // 設(shè)置二維碼矩陣的信息 BitMatrix bitMatrix = setBitMatrix(content, width, height); // 設(shè)置輸出流 OutputStream outStream = null; String path = "d:/Code" + new Date().getTime() + ".png";//設(shè)置二維碼的文件名 try { outStream = new FileOutputStream(new File(path)); // 目前 針對(duì)容錯(cuò)等級(jí)為H reduceWhiteArea 二維碼空白區(qū)域的大小 根據(jù)實(shí)際情況設(shè)置,如果二維碼內(nèi)容長(zhǎng)度不固定的話 需要自己根據(jù)實(shí)際情況計(jì)算reduceWhiteArea的大小 writeToFile(bitMatrix, format, outStream, logoPath, 5); outStream.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 設(shè)置生成二維碼矩陣信息 * @param content 二維碼圖片內(nèi)容 * @param width 二維碼圖片寬度 * @param height 二維碼圖片高度 * @throws WriterException */ private static BitMatrix setBitMatrix(String content, int width, int height) throws WriterException { BitMatrix bitMatrix = null; Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 指定編碼方式,避免中文亂碼 hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定糾錯(cuò)等級(jí) 如果二維碼里面的內(nèi)容比較多的話推薦使用H 容錯(cuò)率30%, 這樣可以避免一些掃描不出來(lái)的問(wèn)題 hints.put(EncodeHintType.MARGIN, margin); // 指定二維碼四周白色區(qū)域大小 官方的這個(gè)方法目前沒(méi)有沒(méi)有作用默認(rèn)設(shè)置為0 bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); return bitMatrix; } /** * @param matrix * @param format * @param outStream * @param logoPath logo圖片 * @param reduceWhiteArea 二維碼空白區(qū)域設(shè)置 * @throws IOException */ private static void writeToFile(BitMatrix matrix, String format, OutputStream outStream, String logoPath, int reduceWhiteArea) throws IOException { BufferedImage image = toBufferedImage(matrix, reduceWhiteArea); // 如果設(shè)置了二維碼里面的logo 加入LOGO水印 if (!StringUtils.isEmpty(logoPath)) { image = addLogo(image, logoPath); } ImageIO.write(image, format, outStream); } /** * * @param matrix * @param reduceWhiteArea * @return */ private static BufferedImage toBufferedImage(BitMatrix matrix, int reduceWhiteArea) { int width = matrix.getWidth(); int height = matrix.getHeight(); BufferedImage image = new BufferedImage(width - 2 * reduceWhiteArea, height - 2 * reduceWhiteArea, BufferedImage.TYPE_3BYTE_BGR); for (int x = reduceWhiteArea; x < width - reduceWhiteArea; x++) { for (int y = reduceWhiteArea; y < height - reduceWhiteArea; y++) { image.setRGB(x - reduceWhiteArea, y - reduceWhiteArea, matrix.get(x, y) ? BLACK : WHITE); } } return image; } /** * 給二維碼圖片中繪制logo信息 非必須 * @param image 二維碼圖片 * @param logoPath logo圖片路徑 */ private static BufferedImage addLogo(BufferedImage image, String logoPath) throws IOException { Graphics2D g = image.createGraphics(); BufferedImage logoImage = ImageIO.read(new File(logoPath)); // 計(jì)算logo圖片大小,可適應(yīng)長(zhǎng)方形圖片,根據(jù)較短邊生成正方形 int width = image.getWidth() < image.getHeight() ? image.getWidth() / LogoPart : image.getHeight() / LogoPart; int height = width; // 計(jì)算logo圖片放置位置 int x = (image.getWidth() - width) / 2; int y = (image.getHeight() - height) / 2; // 在二維碼圖片上繪制中間的logo g.drawImage(logoImage, x, y, width, height, null); // 繪制logo邊框,可選 g.setStroke(new BasicStroke(2)); // 畫(huà)筆粗細(xì) g.setColor(Color.WHITE); // 邊框顏色 g.drawRect(x, y, width, height); // 矩形邊框 logoImage.flush(); g.dispose(); return image; } }
總結(jié)
到此這篇關(guān)于Java使用Zxing二維碼生成的文章就介紹到這了,更多相關(guān)Java用Zxing二維碼生成內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java正則表達(dá)式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
什么是正則表達(dá)式,正則表達(dá)式的作用是什么?這篇文章主要為大家詳細(xì)介紹了Java正則表達(dá)式的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Spring整合SpringMVC + Mybatis基礎(chǔ)框架的配置文件詳解
這篇文章主要介紹了Spring整合SpringMVC + Mybatis基礎(chǔ)框架的配置文件,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法實(shí)例
在應(yīng)用程序啟動(dòng)后,可以自動(dòng)執(zhí)行建庫(kù)、建表等SQL腳本,下面這篇文章主要給大家介紹了關(guān)于SpringBoot啟動(dòng)執(zhí)行sql腳本的3種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01Java防止頻繁請(qǐng)求、重復(fù)提交的操作代碼(后端防抖操作)
在客戶端網(wǎng)絡(luò)慢或者服務(wù)器響應(yīng)慢時(shí),用戶有時(shí)是會(huì)頻繁刷新頁(yè)面或重復(fù)提交表單的,這樣是會(huì)給服務(wù)器造成不小的負(fù)擔(dān)的,同時(shí)在添加數(shù)據(jù)時(shí)有可能造成不必要的麻煩,今天通過(guò)本文給大家介紹下Java防止頻繁請(qǐng)求、重復(fù)提交的操作代碼,一起看看吧2022-04-04JavaWeb中的filter過(guò)濾敏感詞匯案例詳解
敏感詞、文字過(guò)濾是一個(gè)網(wǎng)站必不可少的功能,本篇文章主要介紹了JavaWeb中的filter過(guò)濾敏感詞匯案例,具有一定的參考價(jià)值,有需要的可以了解一下,2016-11-11IDEA報(bào)錯(cuò)之前言中不允許有內(nèi)容問(wèn)題及解決
當(dāng)使用IntelliJ IDEA時(shí),可能會(huì)遇到報(bào)錯(cuò)信息“前言中不允許有內(nèi)容”,這通常是由于XML文件是以帶有BOM頭的UTF-8格式保存的,導(dǎo)致IDE的解析出錯(cuò),解決辦法是在IDEA中調(diào)整文件編碼設(shè)置為無(wú)BOM的UTF-8,然后用文本編輯器(如Notepad++)2024-10-10關(guān)于slf4j_log4j2源碼學(xué)習(xí)心得
這篇文章主要介紹了slf4j_log4j2源碼學(xué)習(xí)心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12