如何在Labelary中使用BMP到ZPL的ASCII HEX优化

Has*_*lam 2 .net c# zpl

我想编写一个将位图图像转换为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();
Run Code Online (Sandbox Code Playgroud)

上面的代码是完美的,但问题是它生成了几乎4 kb的zpl文件,假设但是相同的文件由labelary转换,大小为2 kb.我想知道在GFA,a,b,c,数据中使用什么格式的文章.

Has*_*lam 9

经过3天的研究和努力,我终于找到了一件有趣的事情!我发现在zpl中有一个压缩十六进制的概念.如果有七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.

自己给出答案的目的只是为了帮助那些会得到这种情景的人.谢谢 !!

  • 感叹号和冒号字符也有特殊含义。完整的方案记录在 ZPL2 编程指南的标题为“~DG 和~DB 命令的替代数据压缩方案”的部分中。 (2认同)