【问题标题】:How to optimize ASCII HEX for BMP to ZPL as using in Labelary如何优化 BMP 到 ZPL 的 ASCII HEX,如在 Labelary 中使用
【发布时间】:2016-05-30 05:01:00
【问题描述】:

我想编写一个将位图图像转换为 zpl 的代码。我为此找到了以下代码:

string bitmapFilePath = @"D:\Demo.bmp";
int w, h;
Bitmap b = new Bitmap(bitmapFilePath);
w = b.Width; h = b.Height;
byte[] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);
int fileSize = bitmapFileData.Length;

// The following is known about test.bmp.  It is up to the developer
// to determine this information for bitmaps besides the given test.bmp.
int bitmapDataOffset = int.Parse(bitmapFileData[10].ToString()); ;
int width = w; // int.Parse(bitmapFileData[18].ToString()); ;
int height = h; // int.Parse(bitmapFileData[22].ToString()); ;
int bitsPerPixel = int.Parse(bitmapFileData[28].ToString()); // Monochrome image required!
int bitmapDataLength = bitmapFileData.Length - bitmapDataOffset;
double widthInBytes =  Math.Ceiling(width / 8.0);

while(widthInBytes%4 != 0){                               
    widthInBytes++;
}
// Copy over the actual bitmap data from the bitmap file.
// This represents the bitmap data without the header information.
byte[] bitmap = new byte[bitmapDataLength];
Buffer.BlockCopy(bitmapFileData, bitmapDataOffset, bitmap, 0, bitmapDataLength);

string valu2e = ASCIIEncoding.ASCII.GetString(bitmap);

//byte[] ReverseBitmap = new byte[bitmapDataLength];

// Invert bitmap colors
for (int i = 0; i < bitmapDataLength; i++)
{
    bitmap[i] ^= 0xFF;
}
// Create ASCII ZPL string of hexadecimal bitmap data
string ZPLImageDataString = BitConverter.ToString(bitmap);
ZPLImageDataString = ZPLImageDataString.Replace("-", string.Empty);      

// Create ZPL command to print image
string ZPLCommand = string.Empty;

ZPLCommand += "^XA";
ZPLCommand += "^PMY^FO20,20";
ZPLCommand +=
"^GFA, " +
bitmapDataLength.ToString() + "," +
bitmapDataLength.ToString() + "," +
widthInBytes.ToString() + "," +
ZPLImageDataString;

ZPLCommand += "^XZ";

System.IO.StreamWriter sr = new StreamWriter(@"D:\logoImage.zpl", false, System.Text.Encoding.Default); 

sr.Write(ZPLCommand);
sr.Close();

上面的代码运行完美,但问题是它正在生成几乎 4 kb 的 zpl 文件,假设但相同的文件由 labelary 转换为 2 kb 大小。我想知道 GFA、a、b、c、data 中使用的标签是什么格式。

【问题讨论】:

    标签: c# .net zpl


    【解决方案1】:

    经过3天的研究和努力,我终于找到了一个有趣的东西!!发现zpl中有压缩Hexadecimal的概念。如果有 7 个 F(FFFFFFF),那么我们可以将其替换为 MF,因此此文本“FFFFFFF”将替换为“MF”。这样我们可以减小十六进制的大小。以下是定义的映射:

    G = 1 H = 2 I = 3 J = 4 K = 5 L = 6 M = 7 N = 8 O = 9 P = 10 Q = 11 R = 12 S = 13 T = 14 U = 15 V = 16 W = 17 X = 18 Y = 19

    g = 20 h = 40 i = 60 j = 80 k = 100 l = 120 m = 140 n = 160 o = 180 p = 200 q = 220 r = 240 s = 260 t = 280 u = 300 v = 320 w = 340 x = 360 y = 380 z = 400

    现在假设你有一个数字 23,那么你可以使用 g(20) 和 I(3) "gI" 创建它,所以 gI 将表示数字 23。

    我自己给出答案的目的只是为了帮助那些会遇到这种情况的人。谢谢 !!

    【讨论】:

    • 如果你能回答为什么在例如(gK01F,gK07FC,gK0FFE,gJ03IF,gJ07IF8,gJ0JFC,gI01JFE,gI03KF,gI03KF8,gI07KFC,gI0LFE,gH01MF,gH03MF8)中有逗号,我会 +1 ,gH07MFC)
    • @vincent 来自 ZPL2 编程手册:“数据中的逗号用 00(空白)填充当前行,最小化发送的数据。”
    • 感叹号和冒号也有特殊含义。完整的方案记录在 ZPL2 编程指南标题为“~DG 和 ~DB 命令的替代数据压缩方案”的部分中。
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多