【问题标题】:Barcode not detected through barcode scanner after printing打印后无法通过条码扫描器检测到条码
【发布时间】:2018-10-27 03:36:09
【问题描述】:

我正在使用下面的代码打印在我的 C# WinForms 中生成的条形码,但条形码扫描仪没有检测到它,我尝试使用 code 128 字体和 code39 字体但没有成功,当我使用打印时它检测生成的条形码的调酒师软件,只是不会检测到我的

这里是代码

 private void txtPprice_TextChanged(object sender, EventArgs e)
        {
            string barcode = txtCode.Text;
            string price = txtPprice.Text;
            string pname = txtPname.Text;
            Bitmap bitm = new Bitmap(barcode.Length * 30, 90);

            using (Graphics graphic = Graphics.FromImage(bitm))
            {


                Font newfont = new Font("IDAutomationHC39M", 10);
                Font newfont2 = new Font("Arial Black", 8);
                PointF point = new PointF(10f, 10f);
                SolidBrush black = new SolidBrush(Color.Black);
                SolidBrush white = new SolidBrush(Color.White);
                graphic.FillRectangle(white, 0, 0, bitm.Width, bitm.Height);
                graphic.DrawString("*" + barcode + "*", newfont, black, point);
                PointF pointPrice = new PointF(45f, 55f);
                graphic.DrawString("" + pname +"", newfont2, black, pointPrice);
                PointF pointPname = new PointF(90f, 75f);
                graphic.DrawString("" + price + " L.E.", newfont2, black, pointPname);
                PointF pointBcode = new PointF(20f, 75f);
                graphic.DrawString("" + barcode + "", newfont2, black, pointBcode);

            }

            using (MemoryStream Mmst = new MemoryStream())
            {


                bitm.Save("ms", ImageFormat.Jpeg);
                pictureBox1.Image = bitm;
                pictureBox1.Width = bitm.Width;
                pictureBox1.Height = bitm.Height;


            }  
        }

我已尝试按照其他帖子中的建议添加和删除 *,还将字体大小从 8p 更改为 28px,但仍然没有运气 扫描仪和打印机在调酒师应用程序上工作正常

以下示例中使用的代码是 5094411

这是一张图片,以红色突出显示的文本框是字符串条形码的来源

您可以在图片中看到,在图片框中,它以 IDautomationHC39M 字体(条形码字体)显示 5094411,在 Arial 字体下方再次显示

【问题讨论】:

  • IDAutomationHC39M 是 Code39 字体,无法输出 Code128 条码。另请注意,如果您使用条形码字体,条形码具有必须预先计算的校验和。请包括示例barcode 字符串。
  • @DourHighArch 我不太确定你所说的预先计算是什么意思,我用来生成的字符串只是像 12345678 这样的数字我有一个随机数生成器到一个文本框,并使用这个值条形码字符串,所以它只是一个数字
  • 我认为这是我在几个月内第 4-5 次看到(几乎)完全相同的代码和完全相同的错误。这是来自 Barcode 字体生产商的示例代码吗?无论如何,您必须打印的位图的尺寸是多少,即。您是否必须在定义的空间打印它,或者它可以随意大?或者你只需​​要创建一个位图然后拉伸它以适应? “未检测到”是什么意思?条码扫描器不读取或输出错误数字?
  • @Jimi 我正在尝试使用 Xprinter 标签条码打印机,纸张尺寸为 2" x 1" .. 我的意思是它根本不读取它,没有哔哔声,什么都没有,它只检测我是否使用打印机附带的 Bartender 软件进行打印
  • 看看这个:Blurry and large image barcode。顺便说一句,Code39不需要校验位,可以输入原始数据,需要*起止符号。

标签: c# barcode barcode-scanner


【解决方案1】:

这是正确的代码,在 Jimi 的帮助下经过一些尝试后,我能够正确地完成这部分

string Barcode = "*"+txtCode.Text+"*";
            string price = txtPprice.Text;
            string pname = txtPname.Text;

            using (Bitmap bitmap = new Bitmap(350, 220))
            {
                bitmap.SetResolution(240, 240);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    Font font = new Font("IDAutomationHC39M", 10, FontStyle.Regular, GraphicsUnit.Point);

                    graphics.Clear(Color.White);
                    StringFormat stringformat = new StringFormat(StringFormatFlags.NoWrap);
                    graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
                    graphics.TextContrast = 10;
                    SolidBrush black = new SolidBrush(Color.Black);
                    SolidBrush white = new SolidBrush(Color.White);
                    PointF TextPosition = new PointF(45F, 10F);
                    SizeF TextSize = graphics.MeasureString(Barcode, font, TextPosition, stringformat);
                    PointF pointPrice = new PointF(90f, 125f);
                    Font newfont2 = new Font("Cambria", 8, FontStyle.Regular, GraphicsUnit.Point);
                    Font newfont3 = new Font("Arial Black", 10, FontStyle.Regular, GraphicsUnit.Point);
                    graphics.DrawString("" + pname + "", newfont3, black, pointPrice);
                    PointF pointPname = new PointF(200f, 170f);
                    graphics.DrawString("" + price + " L.E.", newfont3, black, pointPname);
                    PointF pointBcode = new PointF(35f, 170f);
                    graphics.DrawString("" + Barcode + "", newfont2, black, pointBcode);
                    if (TextSize.Width > bitmap.Width)
                    {
                        float ScaleFactor = (bitmap.Width - (TextPosition.X / 2)) / TextSize.Width;
                        graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        graphics.ScaleTransform(ScaleFactor, ScaleFactor);
                    }

                    graphics.DrawString(Barcode, font, new SolidBrush(Color.Black), TextPosition, StringFormat.GenericTypographic);

                    bitmap.Save(@"barcode.png", ImageFormat.Png);
                    this.pictureBox1.Image = (Bitmap)bitmap.Clone();
                    font.Dispose();
                }
            } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多