add support for depth in barcode generations
- Introduced depth parameter to EncodeWithDepth function to allow generating barcodes with specified color depth. - Updated Encode function to use EncodeWithDepth with a default depth of 16.
This commit is contained in:
parent
f3630e1cd4
commit
87ab0677e3
|
@ -13,10 +13,11 @@ type aztecCode struct {
|
|||
*utils.BitList
|
||||
size int
|
||||
content []byte
|
||||
depth int
|
||||
}
|
||||
|
||||
func newAztecCode(size int) *aztecCode {
|
||||
return &aztecCode{utils.NewBitList(size * size), size, nil}
|
||||
func newAztecCode(size int, depth int) *aztecCode {
|
||||
return &aztecCode{utils.NewBitList(size * size), size, nil, 16}
|
||||
}
|
||||
|
||||
func (c *aztecCode) Content() string {
|
||||
|
@ -28,7 +29,7 @@ func (c *aztecCode) Metadata() barcode.Metadata {
|
|||
}
|
||||
|
||||
func (c *aztecCode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return utils.ColorModel(c.depth)
|
||||
}
|
||||
|
||||
func (c *aztecCode) Bounds() image.Rectangle {
|
||||
|
@ -37,9 +38,9 @@ func (c *aztecCode) Bounds() image.Rectangle {
|
|||
|
||||
func (c *aztecCode) At(x, y int) color.Color {
|
||||
if c.GetBit(x*c.size + y) {
|
||||
return color.Black
|
||||
return utils.BlackColor(c.depth)
|
||||
}
|
||||
return color.White
|
||||
return utils.WhiteColor(c.depth)
|
||||
}
|
||||
|
||||
func (c *aztecCode) set(x, y int) {
|
||||
|
|
|
@ -124,6 +124,11 @@ func drawBullsEye(matrix *aztecCode, center, size int) {
|
|||
|
||||
// Encode returns an aztec barcode with the given content
|
||||
func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Barcode, error){
|
||||
return EncodeWithDepth(data, minECCPercent, userSpecifiedLayers, 16)
|
||||
}
|
||||
|
||||
// Encode returns an aztec barcode with the given content
|
||||
func EncodeWithDepth(data []byte, minECCPercent int, userSpecifiedLayers int, depth int) (barcode.Barcode, error) {
|
||||
bits := highlevelEncode(data)
|
||||
eccBits := ((bits.Len() * minECCPercent) / 100) + 11
|
||||
totalSizeBits := bits.Len() + eccBits
|
||||
|
@ -215,7 +220,7 @@ func Encode(data []byte, minECCPercent int, userSpecifiedLayers int) (barcode.Ba
|
|||
alignmentMap[origCenter+i] = center + newOffset + 1
|
||||
}
|
||||
}
|
||||
code := newAztecCode(matrixSize)
|
||||
code := newAztecCode(matrixSize, depth)
|
||||
code.content = data
|
||||
|
||||
// draw data bits
|
||||
|
|
|
@ -33,7 +33,7 @@ var encodingTable = map[rune][]bool{
|
|||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
|
||||
checkValid, _ := regexp.Compile(`[ABCD][0123456789\-\$\:/\.\+]*[ABCD]$`)
|
||||
if content == "!" || checkValid.ReplaceAllString(content, "!") != "!" {
|
||||
return nil, fmt.Errorf("can not encode \"%s\"", content)
|
||||
|
@ -45,5 +45,10 @@ func Encode(content string) (barcode.Barcode, error) {
|
|||
}
|
||||
resBits.AddBit(encodingTable[r]...)
|
||||
}
|
||||
return utils.New1DCode(barcode.TypeCodabar, content, resBits), nil
|
||||
return utils.New1DCodeWithDepth(barcode.TypeCodabar, content, resBits, depth), nil
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, 16)
|
||||
}
|
||||
|
|
|
@ -156,7 +156,7 @@ func getCodeIndexList(content []rune) *utils.BitList {
|
|||
}
|
||||
|
||||
// Encode creates a Code 128 barcode for the given content
|
||||
func Encode(content string) (barcode.BarcodeIntCS, error) {
|
||||
func EncodeWithDepth(content string, depth int) (barcode.BarcodeIntCS, error) {
|
||||
contentRunes := strToRunes(content)
|
||||
if len(contentRunes) <= 0 || len(contentRunes) > 80 {
|
||||
return nil, fmt.Errorf("content length should be between 1 and 80 runes but got %d", len(contentRunes))
|
||||
|
@ -180,7 +180,12 @@ func Encode(content string) (barcode.BarcodeIntCS, error) {
|
|||
sum = sum % 103
|
||||
result.AddBit(encodingTable[sum]...)
|
||||
result.AddBit(encodingTable[stopSymbol]...)
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeCode128, content, result, sum), nil
|
||||
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode128, content, result, sum, depth), nil
|
||||
}
|
||||
|
||||
// Encode creates a Code 128 barcode for the given content
|
||||
func Encode(content string) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithDepth(content, 16)
|
||||
}
|
||||
|
||||
func EncodeWithoutChecksum(content string) (barcode.Barcode, error) {
|
||||
|
|
|
@ -113,7 +113,7 @@ func prepare(content string) (string, error) {
|
|||
|
||||
// Encode returns a code39 barcode for the given content
|
||||
// if includeChecksum is set to true, a checksum character is calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
|
||||
func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.BarcodeIntCS, error) {
|
||||
if fullASCIIMode {
|
||||
var err error
|
||||
content, err = prepare(content)
|
||||
|
@ -148,5 +148,11 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
|
|||
if err != nil {
|
||||
checkSum = 0
|
||||
}
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeCode39, content, result, int(checkSum)), nil
|
||||
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeCode39, content, result, int(checkSum), depth), nil
|
||||
}
|
||||
|
||||
// Encode returns a code39 barcode for the given content
|
||||
// if includeChecksum is set to true, a checksum character is calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16)
|
||||
}
|
||||
|
|
|
@ -74,9 +74,7 @@ func prepare(content string) (string, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// Encode returns a code93 barcode for the given content
|
||||
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
|
||||
func EncodeWithDepth(content string, includeChecksum bool, fullASCIIMode bool, depth int) (barcode.Barcode, error) {
|
||||
if fullASCIIMode {
|
||||
var err error
|
||||
content, err = prepare(content)
|
||||
|
@ -104,7 +102,13 @@ func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.B
|
|||
}
|
||||
result.AddBit(true)
|
||||
|
||||
return utils.New1DCode(barcode.TypeCode93, content, result), nil
|
||||
return utils.New1DCodeWithDepth(barcode.TypeCode93, content, result, depth), nil
|
||||
}
|
||||
|
||||
// Encode returns a code93 barcode for the given content
|
||||
// if includeChecksum is set to true, two checksum characters are calculated and added to the content
|
||||
func Encode(content string, includeChecksum bool, fullASCIIMode bool) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, includeChecksum, fullASCIIMode, 16)
|
||||
}
|
||||
|
||||
func getChecksum(content string, maxWeight int) rune {
|
||||
|
|
|
@ -11,13 +11,15 @@ type codeLayout struct {
|
|||
matrix *utils.BitList
|
||||
occupy *utils.BitList
|
||||
size *dmCodeSize
|
||||
depth int
|
||||
}
|
||||
|
||||
func newCodeLayout(size *dmCodeSize) *codeLayout {
|
||||
func newCodeLayout(size *dmCodeSize, depth int) *codeLayout {
|
||||
result := new(codeLayout)
|
||||
result.matrix = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
|
||||
result.occupy = utils.NewBitList(size.MatrixColumns() * size.MatrixRows())
|
||||
result.size = size
|
||||
result.depth = depth
|
||||
return result
|
||||
}
|
||||
|
||||
|
@ -159,7 +161,7 @@ func (l *codeLayout) SetValues(data []byte) {
|
|||
}
|
||||
|
||||
func (l *codeLayout) Merge() *datamatrixCode {
|
||||
result := newDataMatrixCode(l.size)
|
||||
result := newDataMatrixCodeWithDepth(l.size, l.depth)
|
||||
|
||||
//dotted horizontal lines
|
||||
for r := 0; r < l.size.Rows; r += (l.size.RegionRows() + 2) {
|
||||
|
|
|
@ -12,10 +12,15 @@ type datamatrixCode struct {
|
|||
*utils.BitList
|
||||
*dmCodeSize
|
||||
content string
|
||||
depth int
|
||||
}
|
||||
|
||||
func newDataMatrixCodeWithDepth(size *dmCodeSize, depth int) *datamatrixCode {
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", depth}
|
||||
}
|
||||
|
||||
func newDataMatrixCode(size *dmCodeSize) *datamatrixCode {
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, ""}
|
||||
return &datamatrixCode{utils.NewBitList(size.Rows * size.Columns), size, "", 16}
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) Content() string {
|
||||
|
@ -27,7 +32,7 @@ func (c *datamatrixCode) Metadata() barcode.Metadata {
|
|||
}
|
||||
|
||||
func (c *datamatrixCode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return utils.ColorModel(c.depth)
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) Bounds() image.Rectangle {
|
||||
|
@ -36,9 +41,9 @@ func (c *datamatrixCode) Bounds() image.Rectangle {
|
|||
|
||||
func (c *datamatrixCode) At(x, y int) color.Color {
|
||||
if c.get(x, y) {
|
||||
return color.Black
|
||||
return utils.BlackColor(c.depth)
|
||||
}
|
||||
return color.White
|
||||
return utils.WhiteColor(c.depth)
|
||||
}
|
||||
|
||||
func (c *datamatrixCode) get(x, y int) bool {
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
)
|
||||
|
||||
// Encode returns a Datamatrix barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
func EncodeWithDepth(content string, depth int) (barcode.Barcode, error) {
|
||||
data := encodeText(content)
|
||||
|
||||
var size *dmCodeSize
|
||||
|
@ -23,7 +23,7 @@ func Encode(content string) (barcode.Barcode, error) {
|
|||
}
|
||||
data = addPadding(data, size.DataCodewords())
|
||||
data = ec.calcECC(data, size)
|
||||
code := render(data, size)
|
||||
code := render(data, size, depth)
|
||||
if code != nil {
|
||||
code.content = content
|
||||
return code, nil
|
||||
|
@ -31,8 +31,13 @@ func Encode(content string) (barcode.Barcode, error) {
|
|||
return nil, errors.New("unable to render barcode")
|
||||
}
|
||||
|
||||
func render(data []byte, size *dmCodeSize) *datamatrixCode {
|
||||
cl := newCodeLayout(size)
|
||||
// Encode returns a Datamatrix barcode for the given content
|
||||
func Encode(content string) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, 16)
|
||||
}
|
||||
|
||||
func render(data []byte, size *dmCodeSize, depth int) *datamatrixCode {
|
||||
cl := newCodeLayout(size, depth)
|
||||
|
||||
cl.SetValues(data)
|
||||
|
||||
|
|
|
@ -158,7 +158,7 @@ func encodeEAN13(code string) *utils.BitList {
|
|||
}
|
||||
|
||||
// Encode returns a EAN 8 or EAN 13 barcode for the given code
|
||||
func Encode(code string) (barcode.BarcodeIntCS, error) {
|
||||
func EncodeWithDepth(code string, depth int) (barcode.BarcodeIntCS, error) {
|
||||
var checkSum int
|
||||
if len(code) == 7 || len(code) == 12 {
|
||||
code += string(calcCheckNum(code))
|
||||
|
@ -175,13 +175,18 @@ func Encode(code string) (barcode.BarcodeIntCS, error) {
|
|||
if len(code) == 8 {
|
||||
result := encodeEAN8(code)
|
||||
if result != nil {
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeEAN8, code, result, checkSum), nil
|
||||
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN8, code, result, checkSum, depth), nil
|
||||
}
|
||||
} else if len(code) == 13 {
|
||||
result := encodeEAN13(code)
|
||||
if result != nil {
|
||||
return utils.New1DCodeIntCheckSum(barcode.TypeEAN13, code, result, checkSum), nil
|
||||
return utils.New1DCodeIntCheckSumWithDepth(barcode.TypeEAN13, code, result, checkSum, depth), nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("invalid ean code data")
|
||||
}
|
||||
|
||||
// Encode returns a EAN 8 or EAN 13 barcode for the given code
|
||||
func Encode(code string) (barcode.BarcodeIntCS, error) {
|
||||
return EncodeWithDepth(code, 16)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ const (
|
|||
// Encodes the given data as PDF417 barcode.
|
||||
// securityLevel should be between 0 and 8. The higher the number, the more
|
||||
// additional error-correction codes are added.
|
||||
func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
func EncodeWithDepth(data string, securityLevel byte, depth int) (barcode.Barcode, error) {
|
||||
if securityLevel >= 9 {
|
||||
return nil, fmt.Errorf("Invalid security level %d", securityLevel)
|
||||
}
|
||||
|
@ -34,6 +34,7 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
|||
|
||||
barcode := new(pdfBarcode)
|
||||
barcode.data = data
|
||||
barcode.depth = depth
|
||||
|
||||
codeWords, err := encodeData(dataWords, columns, sl)
|
||||
if err != nil {
|
||||
|
@ -70,6 +71,13 @@ func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
|||
return barcode, nil
|
||||
}
|
||||
|
||||
// Encodes the given data as PDF417 barcode.
|
||||
// securityLevel should be between 0 and 8. The higher the number, the more
|
||||
// additional error-correction codes are added.
|
||||
func Encode(data string, securityLevel byte) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(data, securityLevel, 16)
|
||||
}
|
||||
|
||||
func encodeData(dataWords []int, columns int, sl securitylevel) ([]int, error) {
|
||||
dataCount := len(dataWords)
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@ type pdfBarcode struct {
|
|||
data string
|
||||
width int
|
||||
code *utils.BitList
|
||||
depth int
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) Metadata() barcode.Metadata {
|
||||
|
@ -23,7 +24,7 @@ func (c *pdfBarcode) Content() string {
|
|||
}
|
||||
|
||||
func (c *pdfBarcode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return utils.ColorModel(c.depth)
|
||||
}
|
||||
|
||||
func (c *pdfBarcode) Bounds() image.Rectangle {
|
||||
|
@ -34,7 +35,7 @@ func (c *pdfBarcode) Bounds() image.Rectangle {
|
|||
|
||||
func (c *pdfBarcode) At(x, y int) color.Color {
|
||||
if c.code.GetBit((y/moduleHeight)*c.width + x) {
|
||||
return color.Black
|
||||
return utils.BlackColor(c.depth)
|
||||
}
|
||||
return color.White
|
||||
return utils.WhiteColor(c.depth)
|
||||
}
|
||||
|
|
|
@ -55,7 +55,7 @@ func (e Encoding) String() string {
|
|||
}
|
||||
|
||||
// Encode returns a QR barcode with the given content, error correction level and uses the given encoding
|
||||
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
|
||||
func EncodeWithDepth(content string, level ErrorCorrectionLevel, mode Encoding, depth int) (barcode.Barcode, error) {
|
||||
bits, vi, err := mode.getEncoder()(content, level)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -63,19 +63,23 @@ func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.
|
|||
|
||||
blocks := splitToBlocks(bits.IterateBytes(), vi)
|
||||
data := blocks.interleave(vi)
|
||||
result := render(data, vi)
|
||||
result := render(data, vi, depth)
|
||||
result.content = content
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func render(data []byte, vi *versionInfo) *qrcode {
|
||||
func Encode(content string, level ErrorCorrectionLevel, mode Encoding) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, level, mode, 16)
|
||||
}
|
||||
|
||||
func render(data []byte, vi *versionInfo, depth int) *qrcode {
|
||||
dim := vi.modulWidth()
|
||||
results := make([]*qrcode, 8)
|
||||
for i := 0; i < 8; i++ {
|
||||
results[i] = newBarcode(dim)
|
||||
results[i] = newBarCodeWithDepth(dim, depth)
|
||||
}
|
||||
|
||||
occupied := newBarcode(dim)
|
||||
occupied := newBarCodeWithDepth(dim, depth)
|
||||
|
||||
setAll := func(x int, y int, val bool) {
|
||||
occupied.Set(x, y, true)
|
||||
|
|
14
qr/qrcode.go
14
qr/qrcode.go
|
@ -13,6 +13,7 @@ type qrcode struct {
|
|||
dimension int
|
||||
data *utils.BitList
|
||||
content string
|
||||
depth int
|
||||
}
|
||||
|
||||
func (qr *qrcode) Content() string {
|
||||
|
@ -24,7 +25,7 @@ func (qr *qrcode) Metadata() barcode.Metadata {
|
|||
}
|
||||
|
||||
func (qr *qrcode) ColorModel() color.Model {
|
||||
return color.Gray16Model
|
||||
return utils.ColorModel(qr.depth)
|
||||
}
|
||||
|
||||
func (qr *qrcode) Bounds() image.Rectangle {
|
||||
|
@ -33,9 +34,9 @@ func (qr *qrcode) Bounds() image.Rectangle {
|
|||
|
||||
func (qr *qrcode) At(x, y int) color.Color {
|
||||
if qr.Get(x, y) {
|
||||
return color.Black
|
||||
return utils.BlackColor(qr.depth)
|
||||
}
|
||||
return color.White
|
||||
return utils.WhiteColor(qr.depth)
|
||||
}
|
||||
|
||||
func (qr *qrcode) Get(x, y int) bool {
|
||||
|
@ -158,9 +159,14 @@ func (qr *qrcode) calcPenaltyRule4() uint {
|
|||
return uint(math.Min(floor, ceil) * 10)
|
||||
}
|
||||
|
||||
func newBarcode(dim int) *qrcode {
|
||||
func newBarCodeWithDepth(dim int, depth int) *qrcode {
|
||||
res := new(qrcode)
|
||||
res.dimension = dim
|
||||
res.data = utils.NewBitList(dim * dim)
|
||||
res.depth = depth
|
||||
return res
|
||||
}
|
||||
|
||||
func newBarcode(dim int) *qrcode {
|
||||
return newBarCodeWithDepth(dim, 16)
|
||||
}
|
||||
|
|
|
@ -78,8 +78,8 @@ func AddCheckSum(content string) (string, error) {
|
|||
return content + string(utils.IntToRune(sum%10)), nil
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
||||
// Encode creates a codabar barcode for the given content and depth
|
||||
func EncodeWithDepth(content string, interleaved bool, depth int) (barcode.Barcode, error) {
|
||||
if content == "" {
|
||||
return nil, errors.New("content is empty")
|
||||
}
|
||||
|
@ -131,8 +131,13 @@ func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
|||
resBits.AddBit(mode.end...)
|
||||
|
||||
if interleaved {
|
||||
return utils.New1DCode(barcode.Type2of5Interleaved, content, resBits), nil
|
||||
return utils.New1DCodeWithDepth(barcode.Type2of5Interleaved, content, resBits, depth), nil
|
||||
} else {
|
||||
return utils.New1DCode(barcode.Type2of5, content, resBits), nil
|
||||
return utils.New1DCodeWithDepth(barcode.Type2of5, content, resBits, depth), nil
|
||||
}
|
||||
}
|
||||
|
||||
// Encode creates a codabar barcode for the given content
|
||||
func Encode(content string, interleaved bool) (barcode.Barcode, error) {
|
||||
return EncodeWithDepth(content, interleaved, 16)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue