Skip to content

Commit 47cce14

Browse files
committed
Refactor image handling and dither mode in receipt printing logic
1 parent eb3fbaf commit 47cce14

5 files changed

Lines changed: 45 additions & 38 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ Prints an image from base64-encoded data.
179179
"type": "image",
180180
"data": "data:image/png;base64,iVBORw0KGgoAAAANSU...",
181181
"alignment": "center",
182-
"dither-mode": "floydsteinberg"
182+
"dither_mode": "floydsteinberg"
183183
}
184184
```
185185

handlers.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"image"
56
"sync"
67

78
"github.com/gin-gonic/gin"
@@ -48,7 +49,8 @@ type Image struct {
4849
Type string `json:"type"`
4950
Data string `json:"data"`
5051
Alignment AlignmentType `json:"alignment"`
51-
DitherMode DitherMode `json:"dither_mode"`
52+
DitherMode string `json:"dither_mode"`
53+
img image.Image // decoded image, not directly unmarshaled
5254
}
5355

5456
// Global mutex to serialize printer access
@@ -107,6 +109,7 @@ func handlePrint(c *gin.Context) {
107109
p.QR(v.Code, v.Size)
108110
case Image:
109111
// Print image (you'll need to decode base64 and process)
112+
p.Align(v.Alignment.ToEscposAlignment())
110113
p.Image(processImage(v))
111114
}
112115
}

main.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package main
22

33
import (
4-
"encoding/base64"
54
"fmt"
65
"image"
76
"image/color"
8-
"image/png"
97
"os"
10-
"strings"
118

129
"github.com/gin-contrib/cors"
1310
"github.com/gin-gonic/gin"
@@ -50,32 +47,24 @@ func main() {
5047
router.Run() // listen and serve on 0.0.0.0:8080 (or whatever is set as PORT environment variable)
5148
}
5249

53-
func processImage(image Image) image.Image {
50+
func processImage(i Image) image.Image {
5451

5552
palette := []color.Color{
5653
color.Black, color.White,
5754
}
58-
5955
d := dither.NewDitherer(palette)
6056

61-
reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(image.Data))
62-
img, err := png.Decode(reader)
63-
if err != nil {
64-
fmt.Printf("failed to decode image: %v", err)
65-
return nil
66-
}
67-
68-
// Create a new image from the decoded data
69-
7057
// Apply dither mode if specified
71-
switch image.DitherMode {
72-
case FloydSteinberg:
58+
switch i.DitherMode {
59+
case "floydsteinberg":
7360
d.Matrix = dither.FloydSteinberg
74-
case None:
75-
return img // No dithering, return original image
61+
return d.Dither(i.img)
62+
case "none":
63+
return i.img
64+
default:
65+
return i.img
7666
}
7767

78-
return d.Dither(img)
7968
}
8069

8170
/*

models.go

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package main
22

33
import (
4+
"bytes"
5+
"encoding/base64"
46
"encoding/json"
57
"fmt"
8+
"image/png"
9+
"strings"
610

711
"github.com/mect/go-escpos"
812
)
@@ -181,26 +185,37 @@ func (t BarcodeType) ToEscposBarcodeType() escpos.BarcodeType {
181185

182186
}
183187

184-
type DitherMode string
185-
186-
const (
187-
None DitherMode = "none"
188-
FloydSteinberg DitherMode = "floydsteinberg"
189-
)
188+
func (i *Image) UnmarshalJSON(data []byte) error {
189+
var aux struct {
190+
Data string `json:"data"`
191+
DitherMode string `json:"dither_mode"`
192+
Alignment AlignmentType `json:"alignment"`
193+
}
190194

191-
func (d *DitherMode) UnmarshalJSON(data []byte) error {
192-
var s string
193-
if err := json.Unmarshal(data, &s); err != nil {
195+
if err := json.Unmarshal(data, &aux); err != nil {
194196
return err
195197
}
196198

197-
switch s {
198-
case "none", "floydsteinberg":
199-
*d = DitherMode(s)
200-
return nil
201-
default:
202-
return fmt.Errorf("invalid dither mode: %s", s)
199+
i.Data = aux.Data
200+
i.DitherMode = strings.ToLower(aux.DitherMode)
201+
i.Alignment = aux.Alignment
202+
203+
b64 := aux.Data
204+
if after, ok := strings.CutPrefix(b64, "data:image/png;base64,"); ok {
205+
b64 = after
206+
}
207+
decoded, err := base64.StdEncoding.DecodeString(b64)
208+
if err != nil {
209+
return err
210+
}
211+
img, err := png.Decode(bytes.NewReader(decoded))
212+
if err != nil {
213+
return err
203214
}
215+
216+
i.img = img
217+
return nil
218+
204219
}
205220

206221
// Helper functions to convert custom types to escpos types

sample.service

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ StartLimitIntervalSec=0
66
Type=simple
77
Restart=always
88
RestartSec=1
9-
User=coda
10-
ExecStart=/home/coda/simpleprint/simpleprint
9+
User=root
10+
ExecStart=/usr/bin/simpleprint
1111
EnvironmentFile=/home/coda/simpleprint/.env
1212

1313
[Install]

0 commit comments

Comments
 (0)