-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
152 lines (138 loc) · 3.1 KB
/
Copy pathmain.go
File metadata and controls
152 lines (138 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
"image"
"image/color"
"os"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/makeworld-the-better-one/dither/v2"
"github.com/mect/go-escpos"
)
func main() {
_ = godotenv.Overload(".env")
// printer setup
printerPath, found := os.LookupEnv("PRINTER_PATH")
if !found {
printerPath = ""
}
p, err := escpos.NewUSBPrinterByPath(printerPath)
if err != nil {
fmt.Println("No Printa Found!!")
fmt.Println("Failed to connect to printer:", err)
return
}
p.Init()
p.Smooth(true)
if os.Getenv("GIN_MODE") == "release" {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
router.Use(cors.Default())
router.Use(func(c *gin.Context) {
c.Set("printer", p)
c.Next()
})
router.POST("/print", handlePrint)
fmt.Printf("Listening and serving on 0.0.0.0:%s\n", os.Getenv("PORT"))
router.Run() // listen and serve on 0.0.0.0:8080 (or whatever is set as PORT environment variable)
}
func processImage(i Image) image.Image {
palette := []color.Color{
color.Black, color.White,
}
d := dither.NewDitherer(palette)
// Apply dither mode if specified
switch i.DitherMode {
case "floydsteinberg":
d.Matrix = dither.FloydSteinberg
return d.Dither(i.img)
case "atkinson":
d.Matrix = dither.Atkinson
return d.Dither(i.img)
case "clustereddot8x8":
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot8x8, 0.5)
return d.Dither(i.img)
case "clustereddot4x4":
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot4x4, 0.5)
return d.Dither(i.img)
case "clustereddot6x6":
d.Mapper = dither.PixelMapperFromMatrix(dither.ClusteredDot6x6, 0.5)
return d.Dither(i.img)
case "burkes":
d.Matrix = dither.Burkes
return d.Dither(i.img)
case "jarvisjudiceninke":
d.Matrix = dither.JarvisJudiceNinke
return d.Dither(i.img)
case "sierra":
d.Matrix = dither.Sierra
return d.Dither(i.img)
case "sierralite":
d.Matrix = dither.SierraLite
return d.Dither(i.img)
case "stucki":
d.Matrix = dither.Stucki
return d.Dither(i.img)
case "stevenpigeon":
d.Matrix = dither.StevenPigeon
return d.Dither(i.img)
case "simple2d":
d.Matrix = dither.Simple2D
return d.Dither(i.img)
case "tworowsierra":
d.Matrix = dither.TwoRowSierra
return d.Dither(i.img)
case "falsefloydsteinberg":
d.Matrix = dither.FalseFloydSteinberg
return d.Dither(i.img)
case "none":
return i.img
default:
return i.img
}
}
/*
Let's define a JSON schema for what we need
{
"receipt": [
{
"type": "line",
"content": "Hello World",
"font-size": 2,
"font": "A",
"alignment": "center",
"underline": false
},
{
"type": "feed",
"lines": 2
},
{
"type": "barcode",
"code": "123456789012",
"barcode-type": "CODE128"
},
{
"type": "qr",
"code": "https://example.com",
"size": 8
},
{
"type": "image",
"data": "data:image/png;base64,iVBORw0KGgoAAAANSU...",
"alignment": "center",
"dither-mode": "floydsteinberg"
},
{
"type": "text",
"content": "Multi-line text\nwith newlines",
"font-size": 1,
"font": "A",
"alignment": "left",
"underline": false
}
]
}
*/