-
Notifications
You must be signed in to change notification settings - Fork 1
/
errorcorrection.go
60 lines (54 loc) · 1.81 KB
/
errorcorrection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package mode
import (
"errors"
"github.com/KangSpace/gqrcode/core/cons"
"github.com/KangSpace/gqrcode/util"
"github.com/KangSpace/gqrcode/util/reedsolomon"
"strconv"
)
// Define Error Correction here
// ErrorCorrection :QRCode Error Correction(Reed-Solomon codes)
// (referred to as L,M,Q and H increasing order of capacity)
// L 7%
// M 15%
// L 25%
// H 30%
type ErrorCorrection struct {
Level cons.ErrorCorrectionLevel
// Name in (L, M, Q, H)
Name string `json:"name"`
// Capacity in 0.07, 0.15, 0.25, 0.30
RecoveryRate float32 `json:"recoveryRate"`
RecoveryPercent string `json:"recoveryPercent"`
rs *reedsolomon.ReedSolomonEncoder
}
// NewErrorCorrection : Create a new error correction
func NewErrorCorrection(level cons.ErrorCorrectionLevel) *ErrorCorrection {
var ec *ErrorCorrection
rs := newReedSolomonEncoder()
switch level {
case cons.L:
ec = &ErrorCorrection{cons.L, "L", 0.07, "7%", rs}
case cons.M:
ec = &ErrorCorrection{cons.M, "M", 0.15, "15%", rs}
case cons.Q:
ec = &ErrorCorrection{cons.Q, "Q", 0.25, "25%", rs}
case cons.H:
ec = &ErrorCorrection{cons.H, "H", 0.30, "30%", rs}
case cons.NONE:
// NONE_EC : None Error Correction for Micro QRCode M1
ec = &ErrorCorrection{cons.NONE, "-", 0, "0", rs}
default:
panic(errors.New("specified level:" + strconv.Itoa(level) + " is not support"))
}
return ec
}
func newReedSolomonEncoder() *reedsolomon.ReedSolomonEncoder {
return reedsolomon.NewReedSolomonEncoder(reedsolomon.NewGaloisField(285, 256, 0))
}
// CalcECCodewords :Get Error Correction Codewords by Reed-Solomon algorithms
func (ec *ErrorCorrection) CalcECCodewords(data []byte, ecCodewordsCount int) []byte {
dataIntArray := util.ByteArrayToIntArray(data)
eccIntArray := ec.rs.Encode(dataIntArray, ecCodewordsCount)
return util.IntArrayToByteArray(eccIntArray)
}