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 -5
View File
@@ -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)
+10 -4
View File
@@ -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)
}