Golang QRCode 生成實現
專案地址:ofollow,noindex" target="_blank">yeqown/go-qrcode
同類項目:skip2/go-qrcode 糾錯演算法和bitset使用了該庫,後續可能會考慮自己實現一遍
go-qrcode
示例
link toCODE
package main import ( "fmt" qrcode "github.com/yeqown/go-qrcode" ) func main() { qrc, err := qrcode.New("https://github.com/yeqown/go-qrcode") if err != nil { fmt.Printf("could not generate QRCode: %v", err) } // save file if err := qrc.Save("../testdata/repo-qrcode.jpeg"); err != nil { fmt.Printf("could not save image: %v", err) } }
生成結果如圖:
QRCode原理
1 資料分析(data analysis):
分析輸入資料,根據資料決定要使用的QR碼版本、容錯級別和編碼模式。低版本的QR碼無法編碼過長的資料,含有非數字字母字元的資料要使用擴充套件字元編碼模式。
2 編碼資料(data encoding):
根據選擇的編碼模式,將輸入的字串轉換成位元流,插入模式標識碼(mode indicator)和終止識別符號(terminator),把位元流切分成八位元的位元組,加入填充位元組來滿足標準的資料字碼數要求。
3 計算容錯碼(error correction coding):
對步驟二產生的位元流計算容錯碼,附在位元流之後。高版本的編碼方式可能需要將資料流切分成塊(block)再分別進行容錯碼計算。
4 組織資料(structure final message):
根據結構圖把步驟三得到的有容錯的資料流切分,準備填充。
5 填充(module placement in matrix):
把資料和功能性圖樣根據標準填充到矩陣中。
6 應用資料掩碼(data masking):
應用標準中的八個資料掩碼來變換編碼區域的資料,選擇最優的掩碼應用。講到再展開。
7 填充格式和版本資訊(format and version information):
計算格式和版本資訊填入矩陣,完成QR碼。