QR Code(Quick Response Code) Implement by Go
The QRCode Generator, Micro QRCode Generator, QRCode Decoder, Micro QRCode Decoder application.
According to ISO/IEC 18004-2015 Standard
Installation
go get -u github.com/KangSpace/gqrcode
Dependencies Package:
- imaging
go get -u github.com/disintegration/imaging
- image
mkdir $GOPATH/golang.org/x/
cd $GOPATH/golang.org/x/
git clone https://github.com/golang/image.git
- text
cd $GOPATH/golang.org/x/
git clone https://github.com/golang/text.git
Usage
QR Code
QRCode supports 4 character modes, and 40 versions with 4 Error correction levels.
Micro QRCode supports 4 character modes, and 4 versions with 4 Error correction levels.
4 Modes:
-
numeric
characters(10): 0 - 9
maximum characters in Version 40-L: 7089 characters.
-
alphanumeric
characters(45): 0 - 9, A - Z, SP, $, %, *, +, -, ., /, :
maximum characters in Version 40-L: 4296 characters.
Alphanumeric mode is not available in Version M1 Micro QR Code Symbol
-
byte
8-bit characters.
maximum characters in Version 40-L: 2953 characters.
Byte mode is not available in Version M1 or M2 Micro QR Code Symbol.
-
kanji
Kanji characters in accordance with the Shift JIS system based on JIS X 0209.
Only use for double-byte characters, Range are: \x8140-\x9FFC and \xE040-\xEBBF.
maximum characters in Version 40-L: 2953 characters.
Kanji mode is not available in Version M1 Or M2 Micro QR Code Symbol.
4 Error correction level:
Four levels of Reed-Solomon error correction(referred to as L, M, Q and H in increasing order of capacity) allowing recover of:
- L 7%
- M 15%
- Q 25%
- H 30%
Micro QR Code is not support H level.
Create QR Code
// create a numeric qrcode
data := "0123456"
qrcode, err := NewQRCode(data)
outFileName := "/opt/temp/numeric.png"
if err == nil{
out := output.NewPNGOutput0()
qrcode.Encode(out,outFileName)
}
// create a numeric micro qrcode
micQrcode, err := NewMicroQRCode(data)
outFileName2 := "/opt/temp/numeric_micro.png"
if err == nil{
out2 := output.NewPNGOutput0()
micQrcode.Encode(out2,outFileName2)
}
-
NewQRCode(content string) (*mode.QRCodeStruct,error)
Create a new QRCode(Model2) by input data.
There will be auto-detected the Version, Error Correction Level, and without Quiet Zone.
e.g.:
-
NewQRCodeAutoQuiet(content string) (*mode.QRCodeStruct,error)
Create a new QRCode(Model2) by input data with auto-size Quiet Zone, default is 4x modules per side.
e.g.:
-
NewMicroQRCode(content string) (*mode.QRCodeStruct,error)
Create a new Micro QRCode by input data.
e.g.:
-
NewMicroQRCodeAutoQuiet(content string) (*mode.QRCodeStruct,error)
Create a new Micro QRCode by input data with auto-size Quiet Zone, default is 2x modules per side.
e.g.:
-
NewQRCode0(content string,format cons.Format,ec *mode.ErrorCorrection,m mode.Mode,quietZone *model.QuietZone) (qr *mode.QRCodeStruct,err error)
Create a new QRCode by input data with full parameters.
Params:
Parameter |
Type |
Values |
Comments |
content |
string |
numeric,alphanumeric,and other strings |
- |
format |
cons.Format |
cons.QRCODE,cons.QrcodeModel2,cons.MicroQrcode |
cons.QRCODE same with cons.QrcodeModel2 |
ec |
*mode.ErrorCorrection |
NewErrorCorrection(level) |
level in (cons.L,cons.M,cons.Q,cons.H) |
m |
mode.Mode |
NewNumericMode(),NewAlphanumericMode(),NewKanjiModeMode(), NewByteModeMode() |
- |
quietZone |
*model.QuietZone |
model.AutoQuietZone,model.NoneQuietZone,NewQuietZone(size int) |
- |
Encode QR Code
out := output.NewPNGOutput0()
qrcode.Encode(out,outFileName)
Encode Params:
Parameter |
Type |
Values |
Comments |
out |
output.Output |
NewPNGOutput(size int), NewPNGOutput0(), NewJPGOutput(size int), NewJPGOutput0(), NewGIFOutput(size int), NewGIFOutput0() |
Newxxx0() output with auto-size for image. |
fileName |
string |
output file path |
- |
Output QRCode
output.ImageOutput implements output.Output interface to output PNG,JPG,GIF image.
out := output.NewPNGOutput(100)
out = output.NewPNGOutput0()
logoImageFilePath := "logo.png"
// add logo image into QRCode
out.AddOption(output.LogoOption(logoImageFilePath))
Output Params:
Parameter |
Type |
Values |
Comments |
size |
int |
the image size for output |
- |
Test QR Code
All QRCode test in qrcode_test.go and micro_qrcode_test.go files.
TODO list
Type |
Item |
Done Time |
QRCode Model2 |
Numeric Mode and test |
2021-05-30 22:47:57 |
Alphanumeric Mode |
2021-05-31 |
Byte Mode |
2021-05-31 |
Kanji Mode |
2021-06-01 |
ECI Mode |
NOT-IMPLEMENT |
FNC1 Mode |
NOT-IMPLEMENT |
Mixed Mode |
NOT-IMPLEMENT |
QRCode Model1 |
- |
NOT-SUPPORT |
Micro QRCode |
Micro QRCode |
2021-06-07 |
Personalized QRCode
(add logo image at center of QRCode)
|
- |
2021-06-02 |
Output
|
Image File |
2021-05-30 |
Base64 String |
2021-06-01 |
SVG |
- |
Decoder
|
QRCode |
- |
Micro QRCode |
- |