From 0e874e3e2fa47c777040b90917f48545ecbd6aef Mon Sep 17 00:00:00 2001 From: Joseph Paul Date: Fri, 12 Jul 2024 14:56:22 +0200 Subject: [PATCH 1/2] Add Printer.Image() method to print images directly --- epson.normal.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/epson.normal.go b/epson.normal.go index 2253cac..9dbde34 100644 --- a/epson.normal.go +++ b/epson.normal.go @@ -6,6 +6,7 @@ package escpos import ( "fmt" + "image" "os" "path" "strings" @@ -76,8 +77,13 @@ func (p *Printer) AztecViaImage(data string, width, height int) error { return fmt.Errorf("failed to scale aztec code: %w", err) } - xL, xH, yL, yH, imgData := printImage(aztecCode) - p.write("\x1dv\x30\x00" + string(append([]byte{xL, xH, yL, yH}, imgData...))) + return p.Image(aztecCode) +} - return nil +// Image prints a raster image +// +// The image must be narrower than the printer's pixel width +func (p *Printer) Image(img image.Image) error { + xL, xH, yL, yH, imgData := printImage(img) + return p.write("\x1dv\x30\x00" + string(append([]byte{xL, xH, yL, yH}, imgData...))) } From a70b92fd4ffc64bab7b3e84bba0890091e568e48 Mon Sep 17 00:00:00 2001 From: Joseph Paul Date: Wed, 17 Jul 2024 21:05:06 +0200 Subject: [PATCH 2/2] Pass scale argument to AztecViaImage() method --- epson.normal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/epson.normal.go b/epson.normal.go index 9dbde34..1a8081e 100644 --- a/epson.normal.go +++ b/epson.normal.go @@ -66,12 +66,12 @@ func (p *Printer) AztecViaImage(data string, width, height int) error { if width < 1 { width = 500 } + aztecCode, err := aztec.Encode([]byte(data), aztec.DEFAULT_EC_PERCENT, aztec.DEFAULT_LAYERS) if err != nil { return fmt.Errorf("failed to encode aztec code: %w", err) } - // Scale the barcode to 200x200 pixels aztecCode, err = barcode.Scale(aztecCode, width, height) if err != nil { return fmt.Errorf("failed to scale aztec code: %w", err)