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:
1 parent
f3630e1cd4
commit
87ab0677e3
15 files changed
+114
-47
No files matched your search
@@ -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)
|
||||
|
||||
|
||||
Reference in new issue
Block a user