问题
If I have an Image file (png, bmp, or jpg), and I need to get the data for the ZPL GFA tag, how do I do that?
I would like some example in vb.net or C#
回答1:
please add this class to your project:
public class zplImageConverter : Component, IBindableComponent
{
private int blackLimit=125;
private int total;
private int widthBytes;
private bool compressHex= false;
public int BlacknessLimitPercentage
{
get
{
return (blackLimit * 100 / 255);
}
set
{
blackLimit = (value * 255 / 100);
}
}
public bool CompressHex
{
get { return compressHex; }
set { compressHex = value; }
}
private Image _image = null;
public Image Image
{
get { return _image; }
set
{
_image = value;
}
}
public string zplValue
{
get
{
return this._image != null ? FromImage( (Bitmap)this._image ) : "^A0,10,8^FDError!";
}
}
#region IBindableComponent Members
private BindingContext bindingContext;
private ControlBindingsCollection dataBindings;
[Browsable( false )]
public BindingContext BindingContext
{
get
{
if (bindingContext == null)
{
bindingContext = new BindingContext();
}
return bindingContext;
}
set
{
bindingContext = value;
}
}
[DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
public ControlBindingsCollection DataBindings
{
get
{
if (dataBindings == null)
{
dataBindings = new ControlBindingsCollection( this );
}
return dataBindings;
}
}
#endregion
private static Dictionary<int, string> mapCode= new Dictionary<int, string>() {
{ 1,"G"},
{ 2,"H"},
{ 3,"I"},
{ 4,"J"},
{ 5,"K"},
{ 6,"L"},
{ 7,"M"},
{ 8,"N"},
{ 9,"O"},
{ 10, "P"},
{ 11, "Q"},
{ 12, "R"},
{ 13, "S"},
{ 14, "T"},
{ 15, "U"},
{ 16, "V"},
{ 17, "W"},
{ 18, "X"},
{ 19, "Y"},
{ 20, "g"},
{ 40, "h"},
{ 60, "i"},
{ 80, "j"},
{ 100, "k"},
{ 120, "l"},
{ 140, "m"},
{ 160, "n"},
{ 180, "o"},
{ 200, "p"},
{ 220, "q"},
{ 240, "r"},
{ 260, "s"},
{ 280, "t"},
{ 300, "u"},
{ 320, "v"},
{ 340, "w"},
{ 360, "x"},
{ 380, "y"},
{ 400, "z"},
};
public string FromImage( Bitmap image )
{
string cuerpo= createBody(image);
if (compressHex) cuerpo = HexToAscii( cuerpo );
return headDoc() + cuerpo;
}
private string createBody( Bitmap orginalImage )
{
StringBuilder sb = new StringBuilder();
Graphics graphics = Graphics.FromImage( orginalImage);
graphics.DrawImage( orginalImage, 0, 0 );
int height = orginalImage.Height;
int width = orginalImage.Width;
int index = 0;
// int rgb;
int red;
int green;
int blue;
char[] auxBinaryChar = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' };
widthBytes = (width / 8);
if (((width % 8)
> 0))
{
widthBytes = (((int)((width / 8))) + 1);
}
else
{
widthBytes = (width / 8);
}
this.total = (widthBytes * height);
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width ; w++)
{
var rgb = orginalImage.GetPixel( w, h );
red = rgb.R;
green = rgb.G;
blue = rgb.B;
char auxChar = '1';
int totalColor = (red + green + blue)/3;
if ((totalColor > blackLimit || rgb.A<= blackLimit)) auxChar = '0';
auxBinaryChar[index] = auxChar;
index++;
if (((index == 8) || (w == (width - 1))))
{
sb.Append( ByteBinary( new string( auxBinaryChar ) ) );
auxBinaryChar = new char[] { '0', '0', '0', '0', '0', '0', '0', '0' };
index = 0;
}
}
sb.Append( "\n" );
}
return sb.ToString();
}
private string ByteBinary( string binary )
{
int dec = Convert.ToInt32(binary, 2);//int.Parse(binaryStr);//Integer.parseInt(binaryStr,2);
if (dec > 15)
{
return dec.ToString( "X" ).ToUpper();//int.toString( dec, 16 ).toUpperCase();
}
else
{
return "0" + dec.ToString( "X" ).ToUpper();//Integer.toString( dec, 16 ).toUpperCase();
}
}
private string HexToAscii( string code )
{
int maxlinea = widthBytes * 2;
StringBuilder sbCode = new StringBuilder();
StringBuilder sbLinea = new StringBuilder();
String previousLine = null;
int counter = 1;
char aux = code[0];
bool firstChar = false;
for (int i = 1; i < code.Length; i++)
{
var d=code[i];
if (firstChar)
{
aux = code[i];
firstChar = false;
continue;
}
if (code[i] == '\n')
{
if (counter >= maxlinea && aux == '0')
{
sbLinea.Append( "," );
}
else if (counter >= maxlinea && aux == 'F')
{
sbLinea.Append( "!" );
}
else if (counter > 20)
{
int multi20 = (counter/20)*20;
int resto20 = (counter%20);
sbLinea.Append( mapCode[multi20] );
if (resto20 != 0)
{
sbLinea.Append( mapCode[resto20] + aux );
}
else
{
sbLinea.Append( aux );
}
}
else
{
sbLinea.Append( mapCode[counter] + aux );
if (mapCode[counter] == null) { }
}
counter = 1;
firstChar = true;
if (sbLinea.ToString().Equals( previousLine ))
{
sbCode.Append( ":" );
}
else
{
sbCode.Append( sbLinea.ToString() );
}
previousLine = sbLinea.ToString();
sbLinea.Clear();//.setLength( 0 );
continue;
}
if (aux == code[i])
{
counter++;
}
else
{
if (counter > 20)
{
int multi20 = (counter/20)*20;
int resto20 = (counter%20);
sbLinea.Append( mapCode[multi20] );
if (resto20 != 0)
{
sbLinea.Append( mapCode[resto20] + aux );
}
else
{
sbLinea.Append( aux );
}
}
else
{
sbLinea.Append( mapCode[counter] + aux );
}
counter = 1;
aux = code[i];
}
}
return sbCode.ToString();
}
private String headDoc()
{
String str = "^GFA,"+ total + ","+ total + "," + widthBytes +", \n";
return str;
}
}
then to use:
zplImageConverter cvt= new zplImageConverter();
cvt.Image = Properties.Resources.images;//Set Image to convert
cvt.CompressHex = true;// Enable compresion HexString
cvt.BlacknessLimitPercentage = 50;// Configure the porcentaje for ilumination
Console.WriteLine( cvt.zplValue );// Print on Screen the zplCode (add ^XA before and ^XZ after and try on http://labelary.com/viewer.html)
labelary.com
来源:https://stackoverflow.com/questions/43346393/convert-image-data-to-format-needed-for-gfa