如何优化用于Label的BMP至ZPL的ASCII十六进制 [英] How to optimize ASCII HEX for BMP to ZPL as using in Labelary

查看:47
本文介绍了如何优化用于Label的BMP至ZPL的ASCII十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  string bitmapFilePath = @"D:\ Demo.bmp";整数w,h;位图b =新的位图(bitmapFilePath);w = b.宽度;h = b.高度;byte [] bitmapFileData = System.IO.File.ReadAllBytes(bitmapFilePath);int fileSize = bitmapFileData.Length;//以下是有关test.bmp的信息.由开发人员决定//以确定除给定test.bmp之外的位图信息.int bitmapDataOffset = int.Parse(bitmapFileData [10] .ToString());;int width = w;//int.Parse(bitmapFileData [18] .ToString());;int高度= h;//int.Parse(bitmapFileData [22] .ToString());;int bitsPerPixel = int.Parse(bitmapFileData [28] .ToString());//需要单色图像!int bitmapDataLength = bitmapFileData.Length-bitmapDataOffset;double widthInBytes = Math.Ceiling(width/8.0);while(widthInBytes%4!= 0){widthInBytes ++;}//复制位图文件中的实际位图数据.//这表示不包含标题信息的位图数据.byte []位图=新的byte [bitmapDataLength];Buffer.BlockCopy(bitmapFileData,bitmapDataOffset,bitmap,0,bitmapDataLength);字符串valu2e = ASCIIEncoding.ASCII.GetString(bitmap);//byte [] ReverseBitmap =新的byte [bitmapDataLength];//反转位图颜色对于(int i = 0; i< bitmapDataLength; i ++){位图[i] ^ = 0xFF;}//创建十六进制位图数据的ASCII ZPL字符串字符串ZPLImageDataString = BitConverter.ToString(bitmap);ZPLImageDataString = ZPLImageDataString.Replace(-",string.Empty);//创建ZPL命令以打印图像字符串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,data中的标签使用什么格式.

解决方案

经过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.

自我回答的目的仅仅是帮助那些会遇到这种情况的人.谢谢 !!

I want to write a code that will convert bitmap image to zpl. I found following code for this:

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();

The above code is working perfect but the problem is that it is generating the zpl file of almost 4 kb let suppose but the same file is converted by labelary is of size 2 kb. I want to know that what format is the labelary using in GFA,a,b,c,data.

解决方案

After the research and effort of 3 days, I finally found an interesting thing !! I found that there is a concept of compressing Hexadecimal in zpl. If there are Seven F(FFFFFFF) then we can replace it with MF so this text "FFFFFFF" will be replaced with "MF". In this way we can reduce the size of hexadecimal. Following is the mapping defined:

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

Now suppose you have a figure 23 then you can create it by using g(20) and I(3) "gI", so the gI will denote the number 23.

The purpose of giving answer myself is just to help those who will get such sort of scenarios. Thank you !!

这篇关于如何优化用于Label的BMP至ZPL的ASCII十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆