From abbe1eeb786c696faa5602443cb1a85c1f4eff16 Mon Sep 17 00:00:00 2001 From: William Gill Date: Sun, 12 Jan 2025 22:34:27 -0600 Subject: [PATCH] Add Image printing from URL. Type 7, text field URL. --- service/printer.go | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/service/printer.go b/service/printer.go index eed3da3..67ce223 100644 --- a/service/printer.go +++ b/service/printer.go @@ -5,8 +5,13 @@ import ( fifo "github.com/foize/go.fifo" "github.com/justinmichaelvieira/escpos" "github.com/liamg/tml" + "image" "net" + "net/http" "uberbringer/config" + + _ "image/jpeg" + _ "image/png" ) func newPrintJob(input DataStruct, queue *fifo.Queue) { @@ -47,10 +52,8 @@ func printJobCreate(input DataStruct, queue *fifo.Queue) { p.Initialize() p.DefaultLineSpacing() - for i, item := range input.Data { - var SeqNum = i + 1 - tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " - creating command " + string(SeqNum) + "\n") - if item.Type <= 0 || item.Type > 6 { + for _, item := range input.Data { + if item.Type <= 0 || item.Type > 7 { tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " bad type\n") return } else if item.Size <= 0 || item.Size > 16 { @@ -89,9 +92,30 @@ func printJobCreate(input DataStruct, queue *fifo.Queue) { } p.WriteRaw([]byte(hex_decoded)) - } + case 7: + // Print Image from URL + response, err := http.Get(item.Text) + if err != nil { + tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " image load failed\n") + return + } + defer response.Body.Close() - tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " good type\n") + if response.StatusCode != 200 { + tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " image load failed - received non 200 response code\n") + return + } + + img, _, err := image.Decode(response.Body) + if err != nil { + tml.Printf("[" + config.Project + "]: Job #" + input.JobID + " image decode failed\n") + return + } + + p.PrintImage(img) + + response.Body.Close() + } } } @@ -106,4 +130,5 @@ func printJobCreate(input DataStruct, queue *fifo.Queue) { tml.Printf("[" + config.Project + "]: Done printing #" + input.JobID + "\n") + socket.Close() }