diff --git a/.gitignore b/.gitignore index ac975c4..375b90e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -testdata/ +/testdata/ vendor/ draft/ # example/ @@ -7,4 +7,5 @@ default.jpeg # *.json *.log .DS_store -js/ \ No newline at end of file +js/ +default.jpeg \ No newline at end of file diff --git a/encoder.go b/encoder.go index 8ed3b9e..1f6e6b2 100644 --- a/encoder.go +++ b/encoder.go @@ -6,7 +6,7 @@ import ( "fmt" "log" - "github.com/skip2/go-qrcode/bitset" + "github.com/yeqown/reedsolomon/binary" ) // EncMode ... @@ -26,8 +26,8 @@ const ( ) var ( - paddingByte1 = bitset.NewFromBase2String("11101100") - paddingByte2 = bitset.NewFromBase2String("00010001") + paddingByte1, _ = binary.NewFromBinaryString("11101100") + paddingByte2, _ = binary.NewFromBinaryString("00010001") ) // GetEncModeName ... @@ -49,16 +49,16 @@ func GetEncModeName(mode EncMode) string { } // getEncodeModeIndicator ... -func getEncodeModeIndicator(mode EncMode) *bitset.Bitset { +func getEncodeModeIndicator(mode EncMode) *binary.Binary { switch mode { case EncModeNumeric: - return bitset.New(false, false, false, true) + return binary.New(false, false, false, true) case EncModeAlphanumeric: - return bitset.New(false, false, true, false) + return binary.New(false, false, true, false) case EncModeByte: - return bitset.New(false, true, false, false) + return binary.New(false, true, false, false) case EncModeJP: - return bitset.New(true, false, false, false) + return binary.New(true, false, false, false) default: panic("no indicator") } @@ -67,7 +67,7 @@ func getEncodeModeIndicator(mode EncMode) *bitset.Bitset { // Encoder ... data to bit stream ... type Encoder struct { // self init - dst *bitset.Bitset + dst *binary.Binary data []byte // raw input data // initial params @@ -82,8 +82,8 @@ type Encoder struct { // 1. encode raw data into bitset // 2. append padding data // -func (e *Encoder) Encode(byts []byte) (*bitset.Bitset, error) { - e.dst = bitset.New() +func (e *Encoder) Encode(byts []byte) (*binary.Binary, error) { + e.dst = binary.New() e.data = byts // appedn mode indicator symbol diff --git a/go.mod b/go.mod index 4c72657..aa53217 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/yeqown/go-qrcode -require github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8 +require github.com/yeqown/reedsolomon v1.0.0 diff --git a/go.sum b/go.sum index 663605d..ed997d2 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8 h1:5C4yAeYifeRO+7z2/H2kxL8tJZE9ZE9LpxK6YUZPByo= -github.com/skip2/go-qrcode v0.0.0-20171229120447-cf5f9fa2f0d8/go.mod h1:PLPIyL7ikehBD1OAjmKKiOEhbvWyHGaNDjquXMcYABo= +github.com/yeqown/reedsolomon v1.0.0 h1:x1h/Ej/uJnNu8jaX7GLHBWmZKCAWjEJTetkqaabr4B0= +github.com/yeqown/reedsolomon v1.0.0/go.mod h1:P76zpcn2TCuL0ul1Fso373qHRc69LKwAw/Iy6g1WiiM= diff --git a/qrcode.go b/qrcode.go index 20513c9..d94eac4 100644 --- a/qrcode.go +++ b/qrcode.go @@ -9,9 +9,12 @@ import ( "sync" "time" - "github.com/skip2/go-qrcode/bitset" - "github.com/skip2/go-qrcode/reedsolomon" + // "github.com/skip2/go-qrcode/bitset" + // "github.com/skip2/go-qrcode/reedsolomon" + "github.com/yeqown/go-qrcode/matrix" + "github.com/yeqown/reedsolomon" + "github.com/yeqown/reedsolomon/binary" ) var ( @@ -61,9 +64,9 @@ type QRCode struct { content string // input text content rawData []byte // raw Data to transfer - dataBSet *bitset.Bitset // final data bit stream of encode data + dataBSet *binary.Binary // final data bit stream of encode data mat *matrix.Matrix // matrix grid to store final bitmap - ecBSet *bitset.Bitset // final error correction bitset + ecBSet *binary.Binary // final error correction bitset v Version // version means the size ver int // version num @@ -152,7 +155,7 @@ func (q *QRCode) analyze() error { // https://www.thonky.com/qr-code-tutorial/data-encoding func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) { var ( - bset *bitset.Bitset + bset *binary.Binary ) bset, err = q.encoder.Encode(q.rawData) if err != nil { @@ -169,7 +172,10 @@ func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) { start = end end = start + g.NumDataCodewords*8 - blocks[blockID].Data = bset.Substr(start, end) + blocks[blockID].Data, err = bset.Subset(start, end) + if err != nil { + panic(err) + } blocks[blockID].StartOffset = end - start blocks[blockID].NumECBlock = g.ECBlockwordsPerBlock @@ -182,14 +188,14 @@ func (q *QRCode) dataEncoding() (blocks []dataBlock, err error) { // dataBlock ... type dataBlock struct { - Data *bitset.Bitset + Data *binary.Binary StartOffset int // length NumECBlock int // error correction codewrods num per data block } // ecBlock ... type ecBlock struct { - Data *bitset.Bitset + Data *binary.Binary // StartOffset int // length } @@ -201,7 +207,10 @@ func (q *QRCode) errorCorrectionEncoding(dataBlocks []dataBlock) (blocks []ecBlo for idx, b := range dataBlocks { debugLogf("numOfECBlock: %d", b.NumECBlock) bset := reedsolomon.Encode(b.Data, b.NumECBlock) - blocks[idx].Data = bset.Substr(b.StartOffset, bset.Len()) + blocks[idx].Data, err = bset.Subset(b.StartOffset, bset.Len()) + if err != nil { + panic(err) + } // blocks[idx].StartOffset = b.StartOffset } return @@ -229,10 +238,10 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) { // check if bitsets initialized, or initial them if q.dataBSet == nil { - q.dataBSet = bitset.New() + q.dataBSet = binary.New() } if q.ecBSet == nil { - q.ecBSet = bitset.New() + q.ecBSet = binary.New() } for !endFlag { @@ -243,7 +252,11 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) { overflowCnt++ continue } - q.dataBSet.Append(block.Data.Substr(start, end)) + subBin, err := block.Data.Subset(start, end) + if err != nil { + panic(err) + } + q.dataBSet.Append(subBin) debugLogf("arrange data blocks info: start: %d, end: %d, len: %d, overflowCnt: %d, curIdx: %d", start, end, block.Data.Len(), overflowCnt, curIdx, ) @@ -269,7 +282,11 @@ func (q *QRCode) arrarngeBits(dataBlocks []dataBlock, ecBlocks []ecBlock) { overflowCnt++ continue } - q.ecBSet.Append(block.Data.Substr(start, end)) + subBin, err := block.Data.Subset(start, end) + if err != nil { + panic(err) + } + q.ecBSet.Append(subBin) } curIdx++ // loop finish check diff --git a/version.go b/version.go index b91ec9b..d85dd77 100644 --- a/version.go +++ b/version.go @@ -4,7 +4,8 @@ import ( "errors" "log" - "github.com/skip2/go-qrcode/bitset" + // "github.com/skip2/go-qrcode/bitset" + "github.com/yeqown/reedsolomon/binary" ) // ECLevel error correction level @@ -179,12 +180,12 @@ func (v Version) TotalNumBlocks() int { } // VerInfo Version info bitset -func (v Version) verInfo() *bitset.Bitset { +func (v Version) verInfo() *binary.Binary { if v.Ver < 7 { return nil } - result := bitset.New() + result := binary.New() result.AppendUint32(versionBitSequence[v.Ver], verInfoBitsNum) return result @@ -192,7 +193,7 @@ func (v Version) verInfo() *bitset.Bitset { // formatInfo returns the 15-bit Format Information value for a QR // code. -func (v Version) formatInfo(maskPattern int) *bitset.Bitset { +func (v Version) formatInfo(maskPattern int) *binary.Binary { formatID := 0 switch v.ECLevel { @@ -213,7 +214,7 @@ func (v Version) formatInfo(maskPattern int) *bitset.Bitset { } formatID |= maskPattern & 0x7 - result := bitset.New() + result := binary.New() result.AppendUint32(formatBitSequence[formatID].regular, formatInfoBitsNum) return result } diff --git a/version_test.go b/version_test.go index a29d1bf..7c733f3 100644 --- a/version_test.go +++ b/version_test.go @@ -4,7 +4,7 @@ import ( "reflect" "testing" - "github.com/skip2/go-qrcode/bitset" + "github.com/yeqown/reedsolomon/binary" ) // func Test_load(t *testing.T) { @@ -135,7 +135,7 @@ func TestVersion_verInfo(t *testing.T) { tests := []struct { name string fields fields - want *bitset.Bitset + want *binary.Binary }{ // TODO: Add test cases. } @@ -170,7 +170,7 @@ func TestVersion_formatInfo(t *testing.T) { name string fields fields args args - want *bitset.Bitset + want *binary.Binary }{ // TODO: Add test cases. }