首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在实验室中优化BMP到ZPL中的ASCII HEX

如何在实验室中优化BMP到ZPL中的ASCII HEX
EN

Stack Overflow用户
提问于 2016-02-18 06:38:00
回答 1查看 4.2K关注 0票数 3

我想编写一段代码,将位图图像转换为zpl。我发现了以下代码:

代码语言:javascript
复制
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();

上面的代码工作得很好,但问题是它正在生成几乎4kb的zpl文件,假设相同的文件被标签转换为2kb。我想知道在GFA,a,b,c,数据中使用的标签是什么格式。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-03 11:52:19

经过3天的研究和努力,我终于发现了一件有趣的事情!我发现zpl中有一个压缩十六进制的概念。如果有七个F(FFFFFFF),那么我们可以用MF替换它,所以这个文本"FFFFFFF“将被替换为"MF”。这样我们就可以减少十六进制的大小。以下是定义的映射:

G=1H=2I=3J=4K=5L=6M=7N=8O P=10Q=11S=12T=14U=15V=16W=17X=18Y= 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。

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

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35474520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档