斑马打印机 - 不打印PNG流*提供我自己的答案*

时间:2015-10-27 12:01:25

标签: printing crc zebra-printers

我认为我非常接近打印。但它仍然不是。抛出没有异常,它似乎确实击中了斑马打印机,但没有。这是一个很长的镜头,因为我认为大多数人都处于相同的位置并对此知之甚少。任何人都可以给予任何帮助,无论多么小的欢迎,我都失去了生存的意愿

using (var response = request.GetResponse())
            {
                using (var responseStream = response.GetResponseStream())
                {
                    using (var stream = new MemoryStream())
                    {
                        if (responseStream == null)
                        {
                            return;
                        }

                        responseStream.CopyTo(stream);
                        stream.Position = 0;

                        using (var zipout = ZipFile.Read(stream))
                        {
                            using (var ms = new MemoryStream())
                            {
                                foreach (var e in zipout.Where(e => e.FileName.Contains(".png")))
                                {
                                    e.Extract(ms);
                                }

                                if (ms.Length <= 0)
                                {
                                    return;
                                }

                                var binaryData = ms.ToArray();

                                byte[] compressedFileData;

                                //  Compress the data using the LZ77 algorithm.
                                using (var outStream = new MemoryStream())
                                {
                                    using (var compress = new DeflateStream(outStream, CompressionMode.Compress, true))
                                    {
                                        compress.Write(binaryData, 0, binaryData.Length);
                                        compress.Flush();
                                        compress.Close();
                                    }
                                    compressedFileData = outStream.ToArray();
                                }

                                //  Encode the compressed data using the MIME Base64 algorithm.
                                var base64 = Convert.ToBase64String(compressedFileData);

                                //  Calculate a CRC across the encoded data.
                                var crc = Calc(Convert.FromBase64String(base64));

                                //  Add a unique header to differentiate the new format from the existing ASCII hexadecimal encoding.
                                var finalData = string.Format(":Z64:{0}:{1}", base64, crc);

                                var zplToSend = "~DYR:LOGO,P,P," + finalData.Length + ",," + finalData;
                                const string PrintImage = "^XA^FO0,0^IMR:LOGO.PNG^FS^XZ";

                                try
                                {
                                    var client = new System.Net.Sockets.TcpClient();
                                    client.Connect(IpAddress, Port);

                                    var writer = new StreamWriter(client.GetStream(), Encoding.UTF8);
                                    writer.Write(zplToSend);
                                    writer.Flush();
                                    writer.Write(PrintImage);
                                    writer.Close();
                                    client.Close();
                                }
                                catch (Exception ex)
                                {
                                    // Catch Exception
                                }
                            }
                        }
                    }
                }
            }

    private static ushort Calc(byte[] data)
    {
        ushort wCrc = 0;

        for (var i = 0; i < data.Length; i++)
        {
            wCrc ^= (ushort)(data[i] << 8);

            for (var j = 0; j < 8; j++)
            {
                if ((wCrc & 0x8000) != 0)
                {
                    wCrc = (ushort)((wCrc << 1) ^ 0x1021);
                }
                else
                {
                    wCrc <<= 1;
                }
            }
        }

        return wCrc;
    }

1 个答案:

答案 0 :(得分:0)

一旦您知道如何即可轻松进行。 base64中的Zebra ZPL徽标示例

Python3

import crcmod
import base64
crc16 = crcmod.predefined.mkCrcFun('xmodem')
s = hex(crc16(ZPL_LOGO.encode()))[2:]
print (f"crc16: {s}")

记录不佳,我至少可以说