八月閑趣之報文
初
無意間得到一段報文,起了好奇心。檢索得知,這段報文屬于條碼打印機的控制指令ZPL,特點為以^或~開頭,(此類指令還有ZPL、EPL、CPCL,TSPL,ESC/POS,等等)。
按ZPL手冊,逐項對比,指定坐標放置對象,唯~DG(Download Graphics),涉及一個Z64特性。
舉例如下。
~DGR:LOGO.GRF,19600,70,:Z64:eJzt17sJACAUA8C3iftv5wbaWgmCf+7aQOokAgBYI5WurS0AAPzBxgQAAN7n2QAAANxn7GW1UZ7eAgAAAAAAAACcUQHgOgng:3EC5
Z64
Z64,格式為:id:encoded_data:crc。
先 CRC:檢查是否為 3EC5.

再 encoded_data:先base64,再解壓。
The second encoding, known as Z64, first compresses the data using the LZ77 algorithm to reduce
its size. (This algorithm is used by the PKWARE? compression program PKZIP? and is integral to
the PNG graphics format.) The compressed data is then encoded using the MIME Base64 scheme
as described above.

圖像
根據ZPL手冊,~DGd:o.x,t,w,data,及data解釋 (Each character represents a horizontal nibble of four dots),可知,這里存在一個圖像,總大小為19600 bytes,每行70 bytes,即,560×280。
Bitmap
將像素數據放入Bitmap中,可以得到圖像。

以 C# 處理,Bitmap 格式為 PixelFormat.Format1bppIndexed ,選擇 Color Palette,逐行復制,注意 Stride 需按 4 字節對齊。
圖像文件之 BMP

此處的 20160,即 280×72,每行72字節(70字節,以4對齊)。
圖像文件之 PNG

此處 IDAT 解壓后,得到 19880,即 280×71,每行71字節(70字節,額外字節 filter type byte)。
參考
- Programming Guide For ZPL II, https://support.zebra.com/cpws/docs/zpl/zpl_manual.pdf
- Types of Bitmaps - Windows , https://learn.microsoft.com/zh-cn/dotnet/desktop/winforms/advanced/types-of-bitmaps
- PNG Specification (Third Edition) - W3C, https://www.w3.org/TR/png-3/
更多
- ZLib Decompress Online - Code Beautify (需要注意 String, Blob 帶來的二進制文件問題)
- Hex to Base64 - Guru
- github.com/metafloor/zpl-image (可預覽)

浙公網安備 33010602011771號