ZPL打印二维码、汉字

ZPL打印二维码、汉字

^XA
^PW3500
^JMA
^PR2 --打印速度
^~SD2 --打印浓度
^LH40,0 
^CI26    --解决中文字符解析不出来  
^FO200,10{1}^XGR:imgName.GRF,1,1^FS  --打印图片
^FO30,800^BY2,2,65^BCN,N,N,N^FD0123ABCD^FS  --打印一维码
^FO1050,700^BQN,2,4^FDLA,0123ABCD^FS  --打印二维码(QR)
^XZ

_0A_0D 此字符加入到字符中间可以达到换行效果 
校验等级:H Q M L
字符模式:N数字,A字符,B字节

–汉字生成图片

public string getNamey(string datas, string bz)
        {
            #region
            string printContext = datas;

            Bitmap img;
            if (bz == "微软雅黑32")
            {
                img = CreateImage(printContext, new Font("微软雅黑", 32));
            }
            else if (bz == "微软雅黑28")
            {
                img = CreateImage(printContext, new Font("微软雅黑", 28));
            }
            else
            {
                img = CreateImage(printContext, new Font("宋体", 9));
            }
            string imgCode = ConvertImageToCode(img);

            var t = ((img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)) * img.Size.Height).ToString();
            var w = (img.Size.Width / 8 + ((img.Size.Width % 8 == 0) ? 0 : 1)).ToString();
            string zpl = string.Format("~DGR:imgName.GRF,{0},{1},{2}", t, w, imgCode);
            #endregion

            return zpl;
        }
        protected Bitmap CreateImage(string data, Font f)
        {
            if (string.IsNullOrEmpty(data))
                return null;
            var txt = new TextBox();
            txt.Text = data;
            txt.Font = f;
            //txt.PreferredSize.Height只能取到一行的高度(连边距) 
            //所以需要乘以行数, 但是必须先减掉边距, 乘了以后,再把边距加上. 
            //5是目测的边距 
            var image = new Bitmap(txt.PreferredSize.Width, (txt.PreferredSize.Height - 5) * txt.Lines.Length + 5);
            var g = Graphics.FromImage(image);
            var b = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Black, Color.Black, 1.2f, true);
            g.Clear(System.Drawing.Color.White);
            g.DrawString(data, f, b, 1, 1);
            return image;
        }
        protected string ConvertImageToCode(Bitmap img)
        {
            var sb = new StringBuilder();
            long clr = 0, n = 0;
            int b = 0;
            for (int i = 0; i < img.Size.Height; i++)
            {
                for (int j = 0; j < img.Size.Width; j++)
                {
                    b = b * 2;
                    clr = img.GetPixel(j, i).ToArgb();
                    string s = clr.ToString("X");

                    if (s.Substring(s.Length - 6, 6).CompareTo("BBBBBB") < 0)
                    {
                        b++;
                    }
                    n++;
                    if (j == (img.Size.Width - 1))
                    {
                        if (n < 8)
                        {
                            b = b * (2 ^ (8 - (int)n));

                            sb.Append(b.ToString("X").PadLeft(2, '0'));
                            b = 0;
                            n = 0;
                        }
                    }
                    if (n >= 8)
                    {
                        sb.Append(b.ToString("X").PadLeft(2, '0'));
                        b = 0;
                        n = 0;
                    }
                }
                sb.Append(System.Environment.NewLine);
            }
            return sb.ToString();

        }
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页