如何优化 BMP 到 ZPL 的 ASCII 十六进制,就像在标签中使用一样

本文关键字:标签 一样 十六进制 BMP 优化 何优化 ZPL ASCII | 更新日期: 2023-09-27 18:35:05

我想编写一个将位图图像转换为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 文件,但相同的文件由大小为 2 kb 的标签转换。我想知道标签在 GFA,a,b,c,数据中使用什么格式。

如何优化 BMP 到 ZPL 的 ASCII 十六进制,就像在标签中使用一样

经过3天的研究和努力,我终于发现了一件有趣的事情!!我发现在 zpl 中有一个压缩十六进制的概念。如果有七个F(FFFFFFF),那么我们可以用MF替换它,所以这个文本"FFFFFFFF"将被替换为"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。

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