Skip to content

Commit

Permalink
Add 2D barcode support
Browse files Browse the repository at this point in the history
Signed-off-by: Maartje Eyskens <[email protected]>
  • Loading branch information
meyskens committed Apr 30, 2024
1 parent 3ff61a8 commit ff5f6d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
7 changes: 6 additions & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func main() {
return
}

p.Smooth(true)
p.Size(2, 2)
p.PrintLn("HELLO GO")
p.Size(1, 1)
Expand All @@ -44,6 +43,12 @@ func main() {
p.Barcode("MECT", escpos.BarcodeTypeCODE39)
p.Align(escpos.AlignLeft)

p.Feed(1)
p.QR("https://www.youtube.com/watch?v=dQw4w9WgXcQ", 4)
p.Aztec("Also works with Aztec codes", 4)
p.PDF417("And PDF417", 4)
p.DataMatrix("Or even cute DataMatrix", 4)

p.Feed(2)
p.Cut()
p.End()
Expand Down
44 changes: 43 additions & 1 deletion epson.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,49 @@ func (p *Printer) Barcode(barcode string, format BarcodeType) error {
return err
}

return p.PrintLn(fmt.Sprintf("%s", barcode))
return p.PrintLn(barcode)
}

// QR will print a QR code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3
func (p *Printer) QR(code string, size int) error {
return p.twodimensionBarcode("\x31", code, size)
}

// PDF417 will print a PDF417 code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3
func (p *Printer) PDF417(code string, size int) error {
return p.twodimensionBarcode("\x30", code, size)
}

// Aztec will print a Aztec code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3
func (p *Printer) Aztec(code string, size int) error {
return p.twodimensionBarcode("\x35", code, size)
}

// DataMatrix will print a DataMatrix code with given data, the size is between 2 and 16, if an invalid size is given it will default to 3
func (p *Printer) DataMatrix(code string, size int) error {
return p.twodimensionBarcode("\x36", code, size)
}

func (p *Printer) twodimensionBarcode(codetype string, code string, size int) error {
if size < 2 || size > 16 {
size = 3
}
const twoDbar = "\x1d\x28\x6b" // GS ( k

p.write(twoDbar + "\x03\x00" + codetype + "\x43" + fmt.Sprintf("%c", size)) // set size
if codetype == "\x31" {
p.write(twoDbar + "\x03\x00" + codetype + "\x45\x30\x0A") // set error correction level to L
}

codePL := len(code) + 3
codePH := codePL / 256
codePL = codePL % 256

p.write(twoDbar + fmt.Sprintf("%c%c", codePL, codePH) + codetype + "\x50\x30" + code) // send code data

p.write(twoDbar + "\x03\x00" + codetype + "\x51\x30\x0A") // print barcode

return nil
}

func (p *Printer) GetErrorStatus() (ErrorStatus, error) {
Expand Down
Binary file added test
Binary file not shown.

0 comments on commit ff5f6d2

Please sign in to comment.