Merge branch 'master' into master
2
.gitignore
vendored
@@ -1,3 +1,3 @@
|
||||
*.zpl
|
||||
*.png
|
||||
cmd/zplgfa/*.png
|
||||
cmd/zplgfa/zplgfa
|
||||
|
||||
15
.travis.yml
Normal file
@@ -0,0 +1,15 @@
|
||||
language: go
|
||||
go:
|
||||
- 1.11
|
||||
env:
|
||||
global:
|
||||
- PATH=$HOME/gopath/bin:$PATH
|
||||
- secure: BXdNmYUBPDjK2Mh+fbfc/UGPF/jLE8NXzKD3BK6Qm+Na6b6xrKbDnSpheScxFeJJW4LL7ZG2aIyg/4eFTnWFF0DYhrRRol83WLba40HvbEO37sYutnSsPBVJ8DZ6G5DcwdIFg8cG8oLtx4GqEtcsbLi+YXSC6Tat3tt718SBtCQu0VIVSG3RIRIKfgtKkNmj4b5NOxDPaZ1M6UUIF/bqOrafh/v2B+VulWrqh3uSEI1jMurLLUaW4u9Qh83OgspAwZ5Lr+pQFd+YcJqTzCD63YLfk1rMb5WbbdIuBhO4BTSo21ou6t109w7504vTVANBAgnMQqQrWX0M3HRtpOt3ZUSlh4x4WUn9OHtZEzPrVsOOXGo2CfS+PmdMlhijuGycdMBah2Vz/9mCp93CSX2en+lKCpKOs3vb5w9WoGadW/KAWCflHpEQFQyM+F1LD9Jk+88yhA+dgwEIWbcf4cXQhyGTd/ZA2tyszgAo7UqWTOkbu+JQwoHabo3yN6z/PlXG27cNCfYyJ/j+q5Xfy5MP6XepWi4GM+JurP6ssRRmT2T4VNVXXOfmn94+HudElEDrjkSFSRj7tahX1kmERpgF4Jq8XDwWw9XLR902iZ66Yqz+0P3qPmQsOx9gnnYELhdhYRIRGVmCfkWXQgUlFijRqUAwNGhGHGd6OESQUzRQZ0g=
|
||||
install:
|
||||
- go get github.com/mattn/goveralls
|
||||
- go get github.com/modocache/gover
|
||||
- go get -u -t simonwaldherr.de/go/zplgfa/...
|
||||
- go get -u -t github.com/simonwaldherr/zplgfa/...
|
||||
script:
|
||||
- go test -coverprofile=.coverprofile github.com/simonwaldherr/zplgfa/...
|
||||
- goveralls -coverprofile=.coverprofile -service=travis-ci -repotoken $COVERALLS_TOKEN
|
||||
@@ -1,14 +1,18 @@
|
||||
# ZPLGFA Golang Package
|
||||
|
||||
[](https://godoc.org/github.com/SimonWaldherr/zplgfa) [](https://app.fossa.io/projects/git%2Bgithub.com%2FSimonWaldherr%2Fzplgfa?ref=badge_shield)
|
||||
|
||||
[](https://godoc.org/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://travis-ci.org/SimonWaldherr/zplgfa)
|
||||
[](https://coveralls.io/github/SimonWaldherr/zplgfa?branch=master)
|
||||
[](https://goreportcard.com/report/github.com/SimonWaldherr/zplgfa)
|
||||
[](https://codebeat.co/projects/github-com-simonwaldherr-zplgfa-master)
|
||||
[](https://bettercodehub.com/results/SimonWaldherr/zplgfa)
|
||||
[](https://app.fossa.io/projects/git%2Bgithub.com%2FSimonWaldherr%2Fzplgfa?ref=badge_shield)
|
||||
[](https://raw.githubusercontent.com/SimonWaldherr/zplgfa/master/LICENSE)
|
||||
|
||||
The ZPLGFA Golang package implements some functions to convert PNG, JPEG and GIF files to ZPL compatible ^GF-elements ([Graphic Fields](https://www.zebra.com/us/en/support-downloads/knowledge-articles/gf-graphic-field-zpl-command.html)).
|
||||
|
||||
If you need a ready to use application and don't want to hassle around with source code, take a look at the [ZPLGFA CLI Tool](https://github.com/SimonWaldherr/zplgfa/tree/master/cmd/zplgfa) which is based on this package.
|
||||
|
||||
## install
|
||||
|
||||
1. [install Golang](https://golang.org/doc/install)
|
||||
|
||||
7
tests/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# ZPLGFA Tests
|
||||
|
||||
This folder contains some test cases for the ZPLGFA package.
|
||||
The test coverage can be seen at the [coveralls](https://coveralls.io/github/SimonWaldherr/zplgfa?branch=master) project page.
|
||||
|
||||
[](https://coveralls.io/github/SimonWaldherr/zplgfa?branch=master)
|
||||
|
||||
BIN
tests/test.png
Normal file
|
After Width: | Height: | Size: 18 KiB |
BIN
tests/test2.png
Normal file
|
After Width: | Height: | Size: 37 KiB |
BIN
tests/test3.png
Normal file
|
After Width: | Height: | Size: 20 KiB |
BIN
tests/test4.jpg
Normal file
|
After Width: | Height: | Size: 27 KiB |
BIN
tests/test5.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
tests/test6.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
BIN
tests/test7.png
Normal file
|
After Width: | Height: | Size: 2.7 KiB |
42
tests/tests.json
Normal file
@@ -89,7 +89,6 @@ func getRepeatCode(repeatCount int, char string) string {
|
||||
|
||||
// CompressASCII compresses the ASCII data of a ZPL Graphic Field using RLE
|
||||
func CompressASCII(in string) string {
|
||||
in = strings.ToUpper(in)
|
||||
var curChar string
|
||||
var lastChar string
|
||||
var lastCharSince int
|
||||
@@ -171,7 +170,9 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
||||
index = 0
|
||||
}
|
||||
}
|
||||
hexstr := hex.EncodeToString(line)
|
||||
|
||||
hexstr := strings.ToUpper(hex.EncodeToString(line))
|
||||
|
||||
switch graphicType {
|
||||
case ASCII:
|
||||
GraphicFieldData += fmt.Sprintln(hexstr)
|
||||
@@ -185,9 +186,6 @@ func ConvertToGraphicField(source image.Image, graphicType GraphicType) string {
|
||||
lastLine = curLine
|
||||
case Binary:
|
||||
GraphicFieldData += fmt.Sprintf("%s", line)
|
||||
default:
|
||||
graphicType = CompressedASCII
|
||||
GraphicFieldData += fmt.Sprintln(CompressASCII(hexstr))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,95 @@
|
||||
package zplgfa
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type zplTest struct {
|
||||
Filename string `json:"filename"`
|
||||
Zplstring string `json:"zplstring"`
|
||||
Graphictype string `json:"graphictype"`
|
||||
}
|
||||
|
||||
var zplTests []zplTest
|
||||
|
||||
func init() {
|
||||
jsonstr, _ := ioutil.ReadFile("./tests/tests.json")
|
||||
json.Unmarshal(jsonstr, &zplTests)
|
||||
}
|
||||
|
||||
func Test_CompressASCII(t *testing.T) {
|
||||
if str := CompressASCII("FFFFFFFF000000"); str != "NFL0" {
|
||||
t.Fatalf("CompressASCII failed")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_ConvertToZPL(t *testing.T) {
|
||||
var graphicType GraphicType
|
||||
for i, testcase := range zplTests {
|
||||
filename, zplstring, graphictype := testcase.Filename, testcase.Zplstring, testcase.Graphictype
|
||||
// open file
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Printf("Warning: could not open the file \"%s\": %s\n", filename, err)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// flatten image
|
||||
flat := FlattenImage(img)
|
||||
|
||||
// convert image to zpl compatible type
|
||||
switch graphictype {
|
||||
case "ASCII":
|
||||
graphicType = ASCII
|
||||
case "Binary":
|
||||
graphicType = Binary
|
||||
case "CompressedASCII":
|
||||
graphicType = CompressedASCII
|
||||
default:
|
||||
graphicType = CompressedASCII
|
||||
}
|
||||
gfimg := ConvertToZPL(flat, graphicType)
|
||||
|
||||
// remove whitespace - only for the test
|
||||
gfimg = strings.Replace(gfimg, " ", "", -1)
|
||||
gfimg = strings.Replace(gfimg, "\n", "", -1)
|
||||
|
||||
switch graphictype {
|
||||
case "Binary":
|
||||
gfimg = base64.StdEncoding.EncodeToString([]byte(gfimg))
|
||||
}
|
||||
|
||||
if gfimg != zplstring {
|
||||
log.Printf("ConvertToZPL Test for file \"%s\" failed, wanted: \n%s\ngot: \n%s\n", filename, zplstring, gfimg)
|
||||
t.Fatalf("Testcase %d ConvertToZPL failed", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||