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
|
|
|
)
|
|
|
|
|
|
2018-11-06 17:18:12 +01:00
|
|
|
func specialCmds(zebraCmdFlag, networkIpFlag, networkPortFlag string) bool {
|
|
|
|
|
var cmdSent bool
|
2019-04-28 16:35:16 +02:00
|
|
|
if networkIpFlag == "" {
|
|
|
|
|
return cmdSent
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(zebraCmdFlag, "cancel") {
|
2018-11-06 17:18:12 +01:00
|
|
|
if err := sendCancelCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:35:16 +02:00
|
|
|
if strings.Contains(zebraCmdFlag, "calib") {
|
2018-11-06 17:18:12 +01:00
|
|
|
if err := sendCalibCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:35:16 +02:00
|
|
|
if strings.Contains(zebraCmdFlag, "feed") {
|
2018-11-06 17:18:12 +01:00
|
|
|
if err := sendFeedCmdToZebra(networkIpFlag, networkPortFlag); err == nil {
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:35:16 +02:00
|
|
|
if strings.Contains(zebraCmdFlag, "info") {
|
2018-11-06 17:18:12 +01:00
|
|
|
info, err := getInfoFromZebra(networkIpFlag, networkPortFlag)
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Println(info)
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:35:16 +02:00
|
|
|
if strings.Contains(zebraCmdFlag, "config") {
|
2018-11-06 17:18:12 +01:00
|
|
|
info, err := getConfigFromZebra(networkIpFlag, networkPortFlag)
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Println(info)
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:35:16 +02:00
|
|
|
if strings.Contains(zebraCmdFlag, "diag") {
|
2018-11-06 17:18:12 +01:00
|
|
|
info, err := getDiagFromZebra(networkIpFlag, networkPortFlag)
|
|
|
|
|
if err == nil {
|
|
|
|
|
fmt.Println(info)
|
|
|
|
|
cmdSent = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return cmdSent
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-19 19:52:47 +02:00
|
|
|
func main() {
|
|
|
|
|
var filenameFlag string
|
2018-11-06 17:09:56 +01:00
|
|
|
var zebraCmdFlag string
|
2018-10-19 19:52:47 +02:00
|
|
|
var graphicTypeFlag string
|
|
|
|
|
var imageEditFlag string
|
2018-11-06 17:09:56 +01:00
|
|
|
var networkIpFlag string
|
|
|
|
|
var networkPortFlag string
|
2018-10-19 19:52:47 +02:00
|
|
|
var imageResizeFlag float64
|
|
|
|
|
var graphicType zplgfa.GraphicType
|
|
|
|
|
|
|
|
|
|
flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl")
|
2018-11-06 17:18:12 +01:00
|
|
|
flag.StringVar(&zebraCmdFlag, "cmd", "", "send special command to printer [cancel,calib,feed,info,config,diag]")
|
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]")
|
2018-11-06 17:09:56 +01:00
|
|
|
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()
|
|
|
|
|
|
2018-11-06 17:09:56 +01:00
|
|
|
// send special commands to printer
|
2018-11-06 17:18:12 +01:00
|
|
|
cmdSent := specialCmds(zebraCmdFlag, networkIpFlag, networkPortFlag)
|
2018-11-06 17:09:56 +01:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 17:09:56 +01:00
|
|
|
// 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
|
2018-11-06 17:09:56 +01:00
|
|
|
switch strings.ToUpper(graphicTypeFlag) {
|
2018-10-19 19:52:47 +02:00
|
|
|
case "ASCII":
|
|
|
|
|
graphicType = zplgfa.ASCII
|
2018-11-06 17:09:56 +01:00
|
|
|
case "BINARY":
|
2018-10-19 19:52:47 +02:00
|
|
|
graphicType = zplgfa.Binary
|
2018-11-06 17:09:56 +01:00
|
|
|
case "COMPRESSEDASCII":
|
2018-10-19 19:52:47 +02:00
|
|
|
graphicType = zplgfa.CompressedASCII
|
|
|
|
|
default:
|
|
|
|
|
graphicType = zplgfa.CompressedASCII
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// apply image manipulation functions
|
2018-11-06 17:09:56 +01:00
|
|
|
if strings.Contains(imageEditFlag, "monochrome") {
|
2018-10-19 19:52:47 +02:00
|
|
|
img = editImageMonochrome(img)
|
2018-11-06 17:09:56 +01:00
|
|
|
}
|
|
|
|
|
if strings.Contains(imageEditFlag, "blur") {
|
2018-10-19 19:52:47 +02:00
|
|
|
img = blur.Gaussian(img, float64(config.Width)/300)
|
2018-11-06 17:09:56 +01:00
|
|
|
}
|
|
|
|
|
if strings.Contains(imageEditFlag, "edge") {
|
2018-10-19 19:52:47 +02:00
|
|
|
img = effect.Sobel(img)
|
2018-11-06 17:09:56 +01:00
|
|
|
}
|
|
|
|
|
if strings.Contains(imageEditFlag, "segment") {
|
2018-10-19 19:52:47 +02:00
|
|
|
img = segment.Threshold(img, 128)
|
2018-11-06 17:09:56 +01:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
2018-11-06 17:09:56 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|