errorcorrection.go raw

   1  package qr
   2  
   3  import (
   4  	"github.com/boombuler/barcode/utils"
   5  )
   6  
   7  type errorCorrection struct {
   8  	rs *utils.ReedSolomonEncoder
   9  }
  10  
  11  var ec = newErrorCorrection()
  12  
  13  func newErrorCorrection() *errorCorrection {
  14  	fld := utils.NewGaloisField(285, 256, 0)
  15  	return &errorCorrection{utils.NewReedSolomonEncoder(fld)}
  16  }
  17  
  18  func (ec *errorCorrection) calcECC(data []byte, eccCount byte) []byte {
  19  	dataInts := make([]int, len(data))
  20  	for i := 0; i < len(data); i++ {
  21  		dataInts[i] = int(data[i])
  22  	}
  23  	res := ec.rs.Encode(dataInts, int(eccCount))
  24  	result := make([]byte, len(res))
  25  	for i := 0; i < len(res); i++ {
  26  		result[i] = byte(res[i])
  27  	}
  28  	return result
  29  }
  30