add more tests

add network capability to example app
This commit is contained in:
Simon Waldherr
2018-11-06 17:09:56 +01:00
parent ab40a41cfa
commit 798fa421de
13 changed files with 167 additions and 60 deletions

4
.bettercodehub.yml Normal file
View File

@@ -0,0 +1,4 @@
exclude:
- /cmd/zplgfa/.*
languages:
- go

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
*.zpl *.zpl
cmd/zplgfa/*.png cmd/zplgfa/*.png
cmd/zplgfa/zplgfa cmd/zplgfa/zplgfa
zplgfa.coverprofile
coverage.html
test.zpl.bin*

View File

@@ -17,6 +17,12 @@ So if your image file is `label.png` and the IP of your printer is `192.168.178.
zplgfa -file label.png | nc 192.168.178.42 9100 zplgfa -file label.png | nc 192.168.178.42 9100
``` ```
or via the integrated network capability:
```sh
zplgfa -file label.png -ip 192.168.178.42
```
You can also use some effects, e.g. blur: You can also use some effects, e.g. blur:
```sh ```sh

49
cmd/zplgfa/image.go Normal file
View File

@@ -0,0 +1,49 @@
package main
import (
"image"
"image/color"
"math"
)
type imageSet interface {
Set(x, y int, c color.Color)
}
func editImageInvert(img image.Image) image.Image {
b := img.Bounds()
imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
r = 65535 - r
g = 65535 - g
b = 65535 - b
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
}
return img
}
func editImageMonochrome(img image.Image) image.Image {
b := img.Bounds()
imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
if r > math.MaxUint16/2 || g > math.MaxUint16/2 || b > math.MaxUint16/2 {
r, g, b = 65535, 65535, 65535
} else {
r, g, b = 0, 0, 0
}
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
}
return img
}

View File

@@ -4,12 +4,10 @@ import (
"flag" "flag"
"fmt" "fmt"
"image" "image"
"image/color"
_ "image/gif" _ "image/gif"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"log" "log"
"math"
"os" "os"
"strings" "strings"
@@ -23,19 +21,47 @@ import (
func main() { func main() {
var filenameFlag string var filenameFlag string
var zebraCmdFlag string
var graphicTypeFlag string var graphicTypeFlag string
var imageEditFlag string var imageEditFlag string
var networkIpFlag string
var networkPortFlag string
var imageResizeFlag float64 var imageResizeFlag float64
var graphicType zplgfa.GraphicType var graphicType zplgfa.GraphicType
var cmdSent bool
flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl") flag.StringVar(&filenameFlag, "file", "", "filename to convert to zpl")
flag.StringVar(&zebraCmdFlag, "cmd", "", "send special command to printer [calib,feed]")
flag.StringVar(&graphicTypeFlag, "type", "CompressedASCII", "type of graphic field encoding") flag.StringVar(&graphicTypeFlag, "type", "CompressedASCII", "type of graphic field encoding")
flag.StringVar(&imageEditFlag, "edit", "", "manipulate the image [invert,monochrome]") 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")
flag.Float64Var(&imageResizeFlag, "resize", 1.0, "zoom/resize the image") flag.Float64Var(&imageResizeFlag, "resize", 1.0, "zoom/resize the image")
// load flag input arguments // load flag input arguments
flag.Parse() 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
}
// open file // open file
file, err := os.Open(filenameFlag) file, err := os.Open(filenameFlag)
if err != nil { if err != nil {
@@ -43,6 +69,7 @@ func main() {
return return
} }
// close file when complete
defer file.Close() defer file.Close()
// load image head information // load image head information
@@ -62,28 +89,31 @@ func main() {
} }
// select graphic field type // select graphic field type
switch graphicTypeFlag { switch strings.ToUpper(graphicTypeFlag) {
case "ASCII": case "ASCII":
graphicType = zplgfa.ASCII graphicType = zplgfa.ASCII
case "Binary": case "BINARY":
graphicType = zplgfa.Binary graphicType = zplgfa.Binary
case "CompressedASCII": case "COMPRESSEDASCII":
graphicType = zplgfa.CompressedASCII graphicType = zplgfa.CompressedASCII
default: default:
graphicType = zplgfa.CompressedASCII graphicType = zplgfa.CompressedASCII
} }
// apply image manipulation functions // apply image manipulation functions
switch { if strings.Contains(imageEditFlag, "monochrome") {
case strings.Contains(imageEditFlag, "monochrome"):
img = editImageMonochrome(img) img = editImageMonochrome(img)
case strings.Contains(imageEditFlag, "blur"): }
if strings.Contains(imageEditFlag, "blur") {
img = blur.Gaussian(img, float64(config.Width)/300) img = blur.Gaussian(img, float64(config.Width)/300)
case strings.Contains(imageEditFlag, "edge"): }
if strings.Contains(imageEditFlag, "edge") {
img = effect.Sobel(img) img = effect.Sobel(img)
case strings.Contains(imageEditFlag, "segment"): }
if strings.Contains(imageEditFlag, "segment") {
img = segment.Threshold(img, 128) img = segment.Threshold(img, 128)
case strings.Contains(imageEditFlag, "invert"): }
if strings.Contains(imageEditFlag, "invert") {
img = editImageInvert(img) img = editImageInvert(img)
} }
@@ -98,48 +128,11 @@ func main() {
// convert image to zpl compatible type // convert image to zpl compatible type
gfimg := zplgfa.ConvertToZPL(flat, graphicType) gfimg := zplgfa.ConvertToZPL(flat, graphicType)
// output zpl with graphic field date to stdout if networkIpFlag != "" {
fmt.Println(gfimg) // send zpl to printer
} sendDataToZebra(networkIpFlag, networkPortFlag, gfimg)
} else {
type imageSet interface { // output zpl with graphic field data to stdout
Set(x, y int, c color.Color) fmt.Println(gfimg)
}
func editImageInvert(img image.Image) image.Image {
b := img.Bounds()
imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
r = 65535 - r
g = 65535 - g
b = 65535 - b
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
} }
return img
}
func editImageMonochrome(img image.Image) image.Image {
b := img.Bounds()
imgSet := img.(imageSet)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
oldPixel := img.At(x, y)
r, g, b, a := oldPixel.RGBA()
if r > math.MaxUint16/2 || g > math.MaxUint16/2 || b > math.MaxUint16/2 {
r, g, b = 65535, 65535, 65535
} else {
r, g, b = 0, 0, 0
}
pixel := color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)}
imgSet.Set(x, y, pixel)
}
}
return img
} }

27
cmd/zplgfa/network.go Normal file
View File

@@ -0,0 +1,27 @@
package main
import (
"fmt"
"net"
)
func sendDataToZebra(ip, port, str string) error {
tcpAddr, err := net.ResolveTCPAddr("tcp", ip+":"+port)
conn, err := net.DialTCP("tcp4", nil, tcpAddr)
if err == nil {
defer conn.Close()
payloadBytes := []byte(fmt.Sprintf("%s\r\n\r\n", str))
_, err = conn.Write(payloadBytes)
return err
}
return err
}
func sendFeedCmdToZebra(ip, port string) error {
return sendDataToZebra(ip, port, "^xa^aa^fd ^fs^xz")
}
func sendCalibCmdToZebra(ip, port string) error {
return sendDataToZebra(ip, port, "~jc^xa^jus^xz")
}

BIN
tests/test10.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 718 B

BIN
tests/test11.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

BIN
tests/test12.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

BIN
tests/test8.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

BIN
tests/test9.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 622 B

File diff suppressed because one or more lines are too long

View File

@@ -76,15 +76,15 @@ func Test_ConvertToZPL(t *testing.T) {
default: default:
graphicType = CompressedASCII graphicType = CompressedASCII
} }
gfimg := ConvertToZPL(flat, graphicType) gfimg := ConvertToZPL(flat, graphicType)
// remove whitespace - only for the test if graphictype == "Binary" {
gfimg = strings.Replace(gfimg, " ", "", -1)
gfimg = strings.Replace(gfimg, "\n", "", -1)
switch graphictype {
case "Binary":
gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg)) gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg))
} else {
// remove whitespace - only for the test
gfimg = strings.Replace(gfimg, " ", "", -1)
gfimg = strings.Replace(gfimg, "\n", "", -1)
} }
if gfimg != zplstring { if gfimg != zplstring {