ZPL: Binary B64 and compressed Z64 encoding
Asked Answered
M

2

6

Maybe someone can help me with my Zebra ZPL problem. The ZPL manual doesn't really help me. I want to transfer binary (with ZPL B64) and compressed binary (with ZPL Z64) image data to the printer.

I was able to find the following information:

  • with B64 the data is encoded in Base64 format.
  • with Z64 the data is first compressed with LZ77 and then encoded with Base64.
  • A CRC digit is appended to both encodings. (must probably be CRC-16) But I don't get a valid CRC check digit calculated!

Has any of you done that yet?

  • With which parameters is the LZ77 algorithm used?
  • Which CRC is used and with which start polynomial?
  • Maybe someone even has a C# code for this problem?

Thank you very much.

Macao answered 13/12, 2019 at 9:50 Comment(0)
I
7

The ZPL manual doesn't really help me.

Tell me about it!

  • The "LZ77" algorithm mentioned in the manual is in fact the ZLIB format. I used http://zlib.net for that.
  • The "CRC" mentioned in the manual is in fact CRC16-CCITT. Code I used: http://sanity-free.com/133/crc_16_ccitt_in_csharp.html.

    In order to properly calculate it:

    • Compress the picture bits using ZLIB (the picture must be PixelFormat.Format1bppIndexed, and the picture bits are best accessed with Bitmap.LockBits).
    • Encode the compressed data into Base64. No whitespace or line breaks allowed.
    • Convert the Base64 string to a byte array according to ASCII encoding (System.Text.Encoding.ASCII.GetBytes(base64string)).
    • Calculate the CRC over that byte array. The Initial CRC Value must be zero.
Iberian answered 13/12, 2019 at 10:0 Comment(1)
While this helps, it would be nice to see a code example of this using current C# .NET commands. An example of taking either a file or in memory bitmap and compressing it, encoding it, calculating the CRC, and what the final ZPL would look like using the ~DY command would help me. I've tried to figure it out from this comment but can't get it to work correctly.Wasp
D
0

These two pages contain interesting code in Java to write a converter encoding an image file to Z64 :

http://www.jcgonzalez.com/img-to-zpl-online

https://gist.github.com/trevarj/1255e5cbc08fb3f79c3f255e25989a18

I wrote a Python port of the Java class of the first link, taking the form of a Python3 class : https://github.com/ftheeten/python_zebra_adapter/blob/main/class_zebra.py

You can use afterwards the Python Pillow library to generate the images to be converted. This approach is very flexible as you can use any font having a TTF file.

E.G.

import socket
from PIL import ImageFont, ImageDraw, Image
import numpy as np
...

img=FUNCTION_GENERATING_A_PILLOW
zpl=Class_zpl()
zpl.set_compress(False)
zpl.set_blacklight_percentage(60)
str_label=zpl.process(img)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((PRINTER_IP, PORT_PRINTER))
s.sendall(str.encode(str_label))
#data = s.recv(1024)
s.close()
Deandra answered 15/2, 2022 at 19:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.