QR碼生成原理(一)html
1、什麼是QR碼java
QR碼屬於矩陣式二維碼中的一個種類,由DENSO(日本電裝)公司開發,由JIS和ISO將其標準化。QR碼的樣子其實在不少場合已經可以被看到了,我這仍是貼個圖展現一下:算法
這個圖若是被正確解碼,應該看到個人名字和郵箱。數組
2、QR碼的特色ide
說到QR碼的特色,一是高速讀取(QR就是取自「Quick Response」的首字母),對讀取速度的體驗源自於我手機上的一個軟件,象上面貼出的碼圖,經過攝像頭從拍攝到解碼到顯示內容也就三秒左右,對攝像的角度也沒有什麼要求;網站
二是高容量、高密度;理論上內容通過壓縮處理後能夠存7089個數字,4296個字母和數字混合字符,2953個8位字節數據,1817個漢字;ui
三是支持糾錯處理;糾錯處理相對複雜,目前我尚未深刻了解,按照QR碼的標準文檔說明,QR碼的糾錯分爲4個級別,分別是:this
· level L : 最大 7%的錯誤可以被糾正;編碼
· level M : 最大 15%的錯誤可以被糾正;加密
· level Q : 最大 25%的錯誤可以被糾正;
· level H : 最大 30%的錯誤可以被糾正;
四是結構化;看似無規則的圖形,其實對區域有嚴格的定義,下圖就是一個模式2、版本1的QR圖結構(關於QR碼的"模式"、"版本"將在後面進行介紹):
在上圖21*21的矩陣中,黑白的區域在QR碼規範中被指定爲固定的位置,稱爲尋像圖形(finder pattern)和定位圖形(timing pattern)。尋像圖形和定位圖形用來幫助解碼程序肯定圖形中具體符號的座標。
黃色的區域用來保存被編碼的數據內容以及糾錯信息碼。
藍色的區域,用來標識糾錯的級別(也就是Level L到Level H)和所謂的"Mask pattern",這個區域被稱爲「格式化信息」(format information)。
五是擴展能力。QR碼的Structure Append特色,使一個QR碼能夠分解成多個QR碼,反之,也能夠將多個QR碼的數據組合到一個QR碼中來。
3、QR碼的模式和版本
前面提到過QR碼的模式(Model)和版本(Version)。QR碼分爲Model1和Model2兩種模式,Model1是對QR的初始定義,Model2是對Model1的擴展,目前使用較爲廣泛的是Model2,本文的全部說明也僅用於Model2。
QR圖的大小(size)被定義爲版本(Version),版本號從1到40。版本1就是一個21*21的矩陣,每增長一個版本號,矩陣的大小就增長4個模塊(Module),所以,版本40就是一個177*177的矩陣。(版本越高,意味着存儲的內容越多,糾錯能力也越強)。
3、QR碼支持的編碼內容
QR碼支持編碼的內容包括純數字、數字和字符混合編碼、8位字節碼和包含漢字在內的多字節字符。其中:
數字:每三個爲一組壓縮成10bit。
字母數字混合:每兩個爲一組,壓縮成11bit。
8bit字節數據:無壓縮直接保存。
多字節字符:每個字符被壓縮成13bit。
編碼就是把常見的數字、字符等轉換成QR碼的方法。說具體的編碼以前,先說一下QR碼的最大容量問題。
1、最大容量
QR碼的最大容量取決於選擇的版本、糾錯級別和編碼模式(Mode:數字、字符、多字節字符等)。以版本1、糾錯級別爲Level Q的QR碼爲例,能夠存儲27個純數字,或17個字母數字混合字符或11個8bit字節數據。若是要存儲一樣多的內容同時提升糾錯級別,則須要採用更高的版本。版本1~9數據容量、糾錯碼容量對照以下表:
(version) |
(error correcting level) |
(count of data code words) |
count of EC code words |
(numeric) |
(alphanumeric) |
8bit |
1 |
L |
19 |
7 |
41 |
25 |
17 |
M |
16 |
10 |
34 |
20 |
14 |
|
Q |
13 |
13 |
27 |
16 |
11 |
|
H |
9 |
17 |
17 |
10 |
7 |
|
2 |
L |
34 |
10 |
77 |
47 |
32 |
M |
28 |
16 |
63 |
38 |
26 |
|
Q |
22 |
22 |
48 |
29 |
20 |
|
H |
16 |
28 |
34 |
20 |
14 |
|
3 |
L |
55 |
15 |
127 |
77 |
53 |
M |
44 |
26 |
101 |
61 |
42 |
|
Q |
34 |
36 |
77 |
47 |
32 |
|
H |
26 |
44 |
58 |
35 |
24 |
|
4 |
L |
80 |
20 |
187 |
114 |
78 |
M |
64 |
36 |
149 |
90 |
62 |
|
Q |
48 |
52 |
111 |
67 |
46 |
|
H |
36 |
64 |
82 |
50 |
34 |
|
5 |
L |
108 |
26 |
255 |
154 |
106 |
M |
86 |
48 |
202 |
122 |
84 |
|
Q |
62 |
72 |
144 |
87 |
60 |
|
H |
46 |
88 |
106 |
64 |
44 |
|
6 |
L |
136 |
36 |
322 |
195 |
134 |
M |
108 |
64 |
255 |
154 |
106 |
|
Q |
76 |
96 |
175 |
108 |
74 |
|
H |
60 |
112 |
139 |
84 |
58 |
|
7 |
L |
156 |
40 |
370 |
224 |
154 |
M |
124 |
72 |
293 |
178 |
122 |
|
Q |
88 |
108 |
207 |
125 |
86 |
|
H |
66 |
130 |
154 |
93 |
64 |
|
8 |
L |
194 |
48 |
461 |
279 |
192 |
M |
154 |
88 |
365 |
221 |
152 |
|
Q |
110 |
132 |
259 |
157 |
108 |
|
H |
86 |
156 |
202 |
122 |
84 |
|
9 |
L |
232 |
60 |
552 |
335 |
230 |
M |
182 |
110 |
432 |
262 |
180 |
|
Q |
132 |
160 |
312 |
189 |
130 |
|
H |
100 |
192 |
235 |
143 |
98 |
若是要了解更詳細的QR碼容量信息,能夠到電裝的網站去看看http://www.denso-wave.com/qrcode/vertable1-e.html。
下面,就舉例說明將「ABCDE123」轉換成爲版本1、Level H的QR碼轉換方法。
2、模式標識符(Mode Indicator)
QR碼的模式(Mode)就是前文提到的數字、字符、8bit字節碼、多字節碼等。對於不一樣的模式,都有對應的模式標識符(Mode Indicator)來幫助解碼程序進行匹配,模式標識符是4bit的二進制數:
1、數字模式(numeric mode): 0001
2、混合字符模式(alphanumeric mode) : 0010
3、8bit byte mode: 0100
4、日本漢字(KANJI mode) : 1000
5、中國漢字(GB2312):1101
因爲示例文本串是混合字符,所以將選擇alphanumeric mode,其標識碼爲:0010
3、文本串計數標識符(Character count indicator)
文本串計數標識符用來存儲源內容字符串的長度,在版本1-9的QR碼中,文本串長度標識符自身的長度被定義爲:
數字 : 10bit
混合字符 : 9bit
8bit 字節碼 : 8bit
多字節碼 : 8bit
在本例中,源文本串的長度爲8個字符,混合字符的長度爲9bit,所以將字符個數8編碼爲9位二進制表示:000001000
加上混合字符模式標識碼,總的編碼爲0010 000001000
4、數據內容編碼
1、數字模式下的編碼
在數字模式下,數據被限制爲3個數字一段,分紅若干段。如:"123456"將分紅"123"和 "456",分別被編碼成10bit的二進制數。「123」的10bit二進制表示法爲:0001111011,實際上就是二進制的123。
當數據的長度不足3個數字時,若是隻有1個數字則用4bit,若是有2個數字就用7個bit來表示。
如:"9876"被分紅"987"和"6"兩段,所以被表示爲"1111011011 0110"。
2、混合字符模式下的編碼
混合字符模式編碼,其字符對照表以下:
0 |
0 |
|
A |
10 |
|
K |
20 |
|
U |
30 |
|
+ |
40 |
1 |
1 |
|
B |
11 |
|
L |
21 |
|
V |
31 |
|
- |
41 |
2 |
2 |
|
C |
12 |
|
M |
22 |
|
W |
32 |
|
. |
42 |
3 |
3 |
|
D |
13 |
|
N |
23 |
|
X |
33 |
|
/ |
43 |
4 |
4 |
|
E |
14 |
|
O |
24 |
|
Y |
34 |
|
: |
44 |
5 |
5 |
|
F |
15 |
|
P |
25 |
|
Z |
35 |
|||
6 |
6 |
|
G |
16 |
|
Q |
26 |
|
[sp] |
36 |
|||
7 |
7 |
|
H |
17 |
|
R |
27 |
|
$ |
37 |
|||
8 |
8 |
|
I |
18 |
|
S |
28 |
|
% |
38 |
|||
9 |
9 |
|
J |
19 |
|
T |
29 |
|
* |
3 |
編碼方式爲:
源碼被分紅兩個字符一段,以下所示,每段的第一個字符乘上45,再用第二個數字相加。所以每段變成了11bit的2進制碼,若是字符個數只有1個,則用6bit表示。
示例:
|
|
"AB" |
"CD" |
"E1" |
"23" |
|
|
45*10+11 |
45*12+13 |
45*14+1 |
45*2+3 |
|
|
461 |
553 |
631 |
93 |
0010 |
000001000 |
00111001101 |
01000101001 |
01001110111 |
00001011101 |
3、8bit字節數據不經編碼轉換直接保存。
5、編碼終止符(Terminator)
若是編碼後的字符長度不足當前版本和糾錯級別所存儲的容量,則在後續補"0000",若是容量已滿則無需添加終止符。此時獲得的編碼串爲:
0010 000001000 00111001101 01000101001 01001110111 000010111010000
6、編成8bit碼字(Code words)
將以上的編碼再按8bit一組,造成碼字(code words):
00100000 01000001 11001101 01000101 00101001 11011100 00101110 10000
若是尾部數據不足8bit,則在尾部充0:
00100000 01000001 11001101 01000101 00101001 11011100 00101110 10000000
若是編碼後的數據不足版本及糾錯級別的最大容量,則在尾部補充 "11101100"和 "00010001",直到所有填滿。最後,版本1、Level H下的"ABCDE123"的QR碼是:
00100000 01000001 11001101 01000101 00101001 11011100 00101110 1000000011101100
十進制表示法爲:
32 65 205 69 41 220 46 128 236
1、日本漢字(KANJI)是兩個字節表示的字符碼,編碼的方式是將其轉換爲13字節的二進制碼制。
轉換步驟爲:
1、對於JIS值爲8140(hex)到9FFC(hex)之間字符:
a)將待轉換的JIS值減去8140(hex);
b)將高位字節乘以C0(hex);
c)將b)步驟生成的數據加上低位字節;
d)將結果轉換爲13位二進制串。
2、對於JIS值爲E040(hex)到EBBF(hex)之間的字符:
a)將待轉換的JIS值減去C140(hex);
b)將高位字節乘以C0(hex);
c)將b)步驟生成的數據加上低位字節;
d)將結果轉換爲13位二進制串。
2、中文漢字的與日文漢字轉換步驟類似:
1、對於第一字節爲0xA1~0xAA之間,第二字節在0xA1~0xFE之間字符:
a)第一字節減去0xA1;
b)上一步結果乘以0x60;
c)第二字節減去0xA1;
d)將b)步驟的結果加上c步驟的結果;
e)將結果轉換爲13位二進制串。
1、對於第一字節爲0xB0~0xFA之間,第二字節在0xA1~0xFE之間字符:
a)第一字節減去0xA6;
b)上一步結果乘以0x60;
c)第二字節減去0xA1;
d)將b)步驟的結果加上c步驟的結果;
e)將結果轉換爲13位二進制串。
二維碼,是一種採用黑白相間的平面幾何圖形經過相應的編碼算法來記錄文字、圖片、網址等信息的條碼圖片。以下圖
二維碼的特色:
1. 高密度編碼,信息容量大
可容納多達1850個大寫字母或2710個數字或1108個字節,或500多個漢字,比普通條碼信息容量約高几十倍。
2. 編碼範圍廣
該條碼能夠把圖片、聲音、文字、簽字、指紋等能夠數字化的信息進行編碼,用條碼錶示出來;能夠表示多種語言文字;可表示圖像數據。
3. 容錯能力強,具備糾錯功能
這使得二維條碼因穿孔、污損等引發局部損壞時,照樣能夠正確獲得識讀,損毀面積達50%仍可恢復信息。
4. 譯碼可靠性高
它比普通條碼譯碼錯誤率百萬分之二要低得多,誤碼率不超過千萬分之一。
5. 可引入加密措施
保密性、防僞性好。
6. 成本低,易製做,持久耐用
正由於以上這些特色,二維碼如今愈來愈流行,應用也是愈來愈廣(詳細瞭解請見百度百科,介紹不是本篇重點),因此掌握如何開發二維碼是很是不錯的知識儲備,所以本篇博文將爲你們講解如何生成、解析二維碼。
1、Java
所需jar包:QRCode.jar
http://download.csdn.net/detail/songylwq/5115107
TwoDimensionCode類:二維碼操做核心類
package qrcode; import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.imageio.ImageIO; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.exception.DecodingFailedException; import com.swetake.util.Qrcode; public class TwoDimensionCode { /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param imgPath 圖片路徑 */ public void encoderQRCode(String content, String imgPath) { this.encoderQRCode(content, imgPath, "png", 7); } /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param output 輸出流 */ public void encoderQRCode(String content, OutputStream output) { this.encoderQRCode(content, output, "png", 7); } /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param imgPath 圖片路徑 * @param imgType 圖片類型 */ public void encoderQRCode(String content, String imgPath, String imgType) { this.encoderQRCode(content, imgPath, imgType, 7); } /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param output 輸出流 * @param imgType 圖片類型 */ public void encoderQRCode(String content, OutputStream output, String imgType) { this.encoderQRCode(content, output, imgType, 7); } /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param imgPath 圖片路徑 * @param imgType 圖片類型 * @param size 二維碼尺寸 */ public void encoderQRCode(String content, String imgPath, String imgType, int size) { try { BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); File imgFile = new File(imgPath); // 生成二維碼QRCode圖片 ImageIO.write(bufImg, imgType, imgFile); } catch (Exception e) { e.printStackTrace(); } } /** * 生成二維碼(QRCode)圖片 * @param content 存儲內容 * @param output 輸出流 * @param imgType 圖片類型 * @param size 二維碼尺寸 */ public void encoderQRCode(String content, OutputStream output, String imgType, int size) { try { BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); // 生成二維碼QRCode圖片 ImageIO.write(bufImg, imgType, output); } catch (Exception e) { e.printStackTrace(); } } /** * 生成二維碼(QRCode)圖片的公共方法 * @param content 存儲內容 * @param imgType 圖片類型 * @param size 二維碼尺寸 * @return */ private BufferedImage qRCodeCommon(String content, String imgType, int size) { BufferedImage bufImg = null; try { Qrcode qrcodeHandler = new Qrcode(); // 設置二維碼排錯率,可選L(7%)、M(15%)、Q(25%)、H(30%),排錯率越高可存儲的信息越少,但對二維碼清晰度的要求越小 qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); // 設置設置二維碼尺寸,取值範圍1-40,值越大尺寸越大,可存儲的信息越大 qrcodeHandler.setQrcodeVersion(size); // 得到內容的字節數組,設置編碼格式 byte[] contentBytes = content.getBytes("utf-8"); // 圖片尺寸 int imgSize = 67 + 12 * (size - 1); bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); // 設置背景顏色 gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize); // 設定圖像顏色> BLACK gs.setColor(Color.BLACK); // 設置偏移量,不設置可能致使解析出錯 int pixoff = 2; // 輸出內容> 二維碼 if (contentBytes.length > 0 && contentBytes.length < 800) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800]."); } gs.dispose(); bufImg.flush(); } catch (Exception e) { e.printStackTrace(); } return bufImg; } /** * 解析二維碼(QRCode) * @param imgPath 圖片路徑 * @return */ public String decoderQRCode(String imgPath) { // QRCode 二維碼圖片的文件 File imageFile = new File(imgPath); BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } /** * 解析二維碼(QRCode) * @param input 輸入流 * @return */ public String decoderQRCode(InputStream input) { BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(input); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } public static void main(String[] args) { String imgPath = "G:/TDDOWNLOAD/Michael_QRCode.png"; String encoderContent = "Hello 大大、小小,welcome to QRCode!" + "\nMyblog [ http://sjsky.iteye.com ]" + "\nEMail [ sjsky007@gmail.com ]"; TwoDimensionCode handler = new TwoDimensionCode(); handler.encoderQRCode(encoderContent, imgPath, "png"); // try { // OutputStream output = new FileOutputStream(imgPath); // handler.encoderQRCode(content, output); // } catch (Exception e) { // e.printStackTrace(); // } System.out.println("========encoder success"); String decoderContent = handler.decoderQRCode(imgPath); System.out.println("解析結果以下:"); System.out.println(decoderContent); System.out.println("========decoder success!!!"); } }
TwoDimensionCodeImage 類:二維碼圖片對象
package qrcode; import java.awt.image.BufferedImage; import jp.sourceforge.qrcode.data.QRCodeImage; public class TwoDimensionCodeImage implements QRCodeImage { BufferedImage bufImg; public TwoDimensionCodeImage(BufferedImage bufImg) { this.bufImg = bufImg; } @Override public int getHeight() { return bufImg.getHeight(); } @Override public int getPixel(int x, int y) { return bufImg.getRGB(x, y); } @Override public int getWidth() { return bufImg.getWidth(); } }
2、.NET
所需dll:ThoughtWorks.QRCode
http://download.csdn.net/detail/songylwq/5115596
這裏代碼思路跟上述java大同小異,這裏就不給出源碼了,可參見http://download.csdn.net/detail/songylwq/5115107
上面的java代碼,筆者已經進行了簡單的封裝,方便你們適用不一樣場合,但願對你們能有所幫助。