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:
zhaori96 committed 2024-07-24 10:29:23 -03:00
1 parent f3630e1cd4
commit 87ab0677e3
15 files changed
+114 -47

No files matched your search

+9 -1
View File
@@ -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)
+4 -3
View File
@@ -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)
}