How to optimize ASCII HEX for BMP to ZPL as using in Labelary
Asked Answered
L

1

3

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.

Lemming answered 18/2, 2016 at 6:38 Comment(1)
Thank you for this code. it works great for my scenario!Hyoscyamine
L
16

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 !!

Lemming answered 3/3, 2016 at 11:52 Comment(4)
I'd +1 if you can answer why there is a comma in e.g ( gK01F,gK07FC,gK0FFE,gJ03IF,gJ07IF8,gJ0JFC,gI01JFE,gI03KF,gI03KF8,gI07KFC,gI0LFE,gH01MF,gH03MF8,gH07MFC )Edgington
@Edgington From the ZPL2 programming manual: "A comma in the data pads the current line with 00 (white space), minimizing the data sent."Regimen
The exclamation mark and colon characters have special meaning too. The full scheme is documented in the section of the ZPL2 programming guide entitled 'Alternative Data Compression scheme for ~DG and ~DB Commands'.Longlived
Great! Where did you get this? Is this some standard? I checked the zpl-2 pdf. Nothing like this in it.Pegmatite

© 2022 - 2024 — McMap. All rights reserved.