Files
ZPLify/cmd/zplgfa/main.go

139 lines
3.5 KiB
Go
Raw Normal View History

2018-10-19 19:52:47 +02:00
package main
import (
"flag"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"os"
"strings"
2018-11-04 16:52:42 +01:00
"github.com/anthonynsimon/bild/blur"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/segment"
"github.com/nfnt/resize"
"simonwaldherr.de/go/zplgfa"
2018-10-19 19:52:47 +02:00
)
func main() {
var filenameFlag string
var zebraCmdFlag string
2018-10-19 19:52:47 +02:00
var graphicTypeFlag string
var imageEditFlag string
var networkIpFlag string
var networkPortFlag string
2018-10-19 19:52:47 +02:00
var imageResizeFlag float64
var graphicType zplgfa.GraphicType
var cmdSent bool
2018-10-19 19:52:47 +02:00
flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl")
flag.StringVar(&zebraCmdFlag, "cmd", "", "send special command to printer [calib,feed]")
2018-10-19 19:52:47 +02:00
flag.StringVar(&graphicTypeFlag, "type", "CompressedASCII", "type of graphic field encoding")
flag.StringVar(&imageEditFlag, "edit", "", "manipulate the image [invert,monochrome]")
flag.StringVar(&networkIpFlag, "ip", "", "send zpl to printer")
flag.StringVar(&networkPortFlag, "port", "9100", "network port of printer")
2018-10-19 19:52:47 +02:00
flag.Float64Var(&imageResizeFlag, "resize", 1.0, "zoom/resize the image")
// load flag input arguments
flag.Parse()
// send special commands to printer
if strings.Contains(zebraCmdFlag, "calib") && networkIpFlag != "" {
if err := sendCalibCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
cmdSent = true
}
}
if strings.Contains(zebraCmdFlag, "feed") && networkIpFlag != "" {
if err := sendFeedCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
cmdSent = true
}
}
// check input parameter
if filenameFlag == "" {
if cmdSent {
return
}
log.Printf("Warning: no input file specified\n")
return
}
2018-10-19 19:52:47 +02:00
// open file
file, err := os.Open(filenameFlag)
if err != nil {
log.Printf("Warning: could not open the file \"%s\": %s\n", filenameFlag, err)
return
}
// close file when complete
2018-10-19 19:52:47 +02:00
defer file.Close()
// load image head information
config, format, err := image.DecodeConfig(file)
if err != nil {
log.Printf("Warning: image not compatible, format: %s, config: %v, error: %s\n", format, config, err)
}
// reset file pointer to the beginning of the file
file.Seek(0, 0)
// load and decode image
img, _, err := image.Decode(file)
if err != nil {
log.Printf("Warning: could not decode the file, %s\n", err)
return
}
// select graphic field type
switch strings.ToUpper(graphicTypeFlag) {
2018-10-19 19:52:47 +02:00
case "ASCII":
graphicType = zplgfa.ASCII
case "BINARY":
2018-10-19 19:52:47 +02:00
graphicType = zplgfa.Binary
case "COMPRESSEDASCII":
2018-10-19 19:52:47 +02:00
graphicType = zplgfa.CompressedASCII
default:
graphicType = zplgfa.CompressedASCII
}
// apply image manipulation functions
if strings.Contains(imageEditFlag, "monochrome") {
2018-10-19 19:52:47 +02:00
img = editImageMonochrome(img)
}
if strings.Contains(imageEditFlag, "blur") {
2018-10-19 19:52:47 +02:00
img = blur.Gaussian(img, float64(config.Width)/300)
}
if strings.Contains(imageEditFlag, "edge") {
2018-10-19 19:52:47 +02:00
img = effect.Sobel(img)
}
if strings.Contains(imageEditFlag, "segment") {
2018-10-19 19:52:47 +02:00
img = segment.Threshold(img, 128)
}
if strings.Contains(imageEditFlag, "invert") {
2018-10-19 19:52:47 +02:00
img = editImageInvert(img)
}
// resize image
if imageResizeFlag != 1.0 {
img = resize.Resize(uint(float64(config.Width)*imageResizeFlag), uint(float64(config.Height)*imageResizeFlag), img, resize.MitchellNetravali)
}
// flatten image
flat := zplgfa.FlattenImage(img)
// convert image to zpl compatible type
gfimg := zplgfa.ConvertToZPL(flat, graphicType)
if networkIpFlag != "" {
// send zpl to printer
sendDataToZebra(networkIpFlag, networkPortFlag, gfimg)
} else {
// output zpl with graphic field data to stdout
fmt.Println(gfimg)
2018-10-19 19:52:47 +02:00
}
}