-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
101 lines (84 loc) · 2.33 KB
/
main.go
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
// main.go
package main
import (
"flag"
"fmt"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"log"
"os"
)
func main() {
address := flag.String("a", "", "MAC address of the target device")
noCheck := flag.Bool("no-check", false, "Skips image check")
density := flag.Int("d", 2, "Printer density (1~3)")
labelType := flag.Int("t", 1, "Label type (1~3)")
quantity := flag.Int("n", 1, "Number of copies")
flag.Parse()
if *address == "" {
fmt.Println("Error: MAC address is required.")
return
}
imgFile := flag.Arg(0)
img, err := loadImage(imgFile)
if err != nil {
fmt.Printf("Error loading image: %v\n", err)
return
}
if img.Bounds().Max.X/img.Bounds().Max.Y > 1 {
// Rotate clockwise 90 degrees, so the upper line (left line) prints first.
img = rotateImage(img, 270)
}
*noCheck = true
*address = "08:13:F4:C4:34:53"
if !*noCheck && (img.Bounds().Max.X != 96 || img.Bounds().Max.Y >= 600) {
fmt.Println("Error: Image dimensions are not valid.")
return
}
printer, err := NewPrinterClient(*address)
if err != nil {
log.Fatalln(err)
}
printer.SetLabelType(byte(*labelType))
printer.SetLabelDensity(byte(*density))
printer.StartPrint()
printer.AllowPrintClear()
printer.StartPagePrint()
printer.SetDimension(img.Bounds().Max.Y, img.Bounds().Max.X)
printer.SetQuantity(uint16(*quantity))
packets := naiveEncoder(img)
for _, pkt := range packets {
printer.send(pkt)
}
printer.EndPagePrint()
for printer.GetPrintStatus().Page != uint16(*quantity) {
fmt.Println(printer.GetPrintStatus())
}
printer.EndPrint()
}
func loadImage(filename string) (image.Image, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return img, nil
}
func rotateImage(img image.Image, i int) image.Image {
rotated := image.NewRGBA64(image.Rect(0, 0, img.Bounds().Dy(), img.Bounds().Dx()))
for x := img.Bounds().Min.X; x < img.Bounds().Max.X; x++ {
for y := img.Bounds().Min.Y; y < img.Bounds().Max.Y; y++ {
srcX := img.Bounds().Max.X - x - 1
srcY := y
srcColor := color.NRGBAModel.Convert(img.At(srcX, srcY)).(color.NRGBA)
rotated.SetRGBA64(y, x, color.RGBA64(color.NRGBA64{R: uint16(srcColor.R), G: uint16(srcColor.G), B: uint16(srcColor.B), A: uint16(srcColor.A)}))
}
}
return rotated
}