【问题标题】:ZXing barcode scanner for .NETZXing .NET 条码扫描器
【发布时间】:2023-08-26 15:55:02
【问题描述】:

我正在尝试使用这个库 https://zxingnet.codeplex.com/ 。 图片已在本站https://zxing.org/w/decode.jspx 上成功解码,但在我的代码中没有。

以下是我尝试过的两种方法:

BarcodeReader reader = new BarcodeReader { AutoRotate = true, TryHarder = true, TryInverted = true, PossibleFormats = fmts };
Result result = reader.Decode(new Bitmap(@"D:\\6.jpg"));

和:

public static byte[] ImageToByte(Bitmap img)
{
    ImageConverter converter = new ImageConverter();
    return (byte[])converter.ConvertTo(img, typeof(byte[]));
}

MultiFormatReader m_BarcodeReader = new MultiFormatReader();
var hints = new Dictionary<DecodeHintType, object>();
var fmts = new List<BarcodeFormat>();
fmts.Add(BarcodeFormat.EAN_13);
hints.Add(DecodeHintType.TRY_HARDER_WITHOUT_ROTATION, false);
hints.Add(DecodeHintType.POSSIBLE_FORMATS, fmts);

Result rawResult;
Bitmap image = new Bitmap(@"D:\\6.jpg");
RGBLuminanceSource r = new RGBLuminanceSource(ImageToByte(image), image.Width, image.Height);
GlobalHistogramBinarizer x = new
HybridBinarizer(r);
BinaryBitmap bitmap = new BinaryBitmap(x);
try
{
    rawResult = m_BarcodeReader.decode(bitmap, hints);

    if (rawResult != null)
    {
        return rawResult.Text;
    }
}
catch (ReaderException e)
{

}

在这两种情况下,解码结果都是null。我在这里做错了什么? 这是示例图片:

【问题讨论】:

  • 您尝试过简单的条形码图像吗?
  • 你能在windows窗体中显示图像吗?也许调整高度、宽度、左上角,这样只有条形码被传递给扫描仪。必要时旋转图像。
  • @VishnuPrasad,它在我无法检测到的非常特殊的情况下在这里和那里工作。大多数情况下它只是不起作用,而每张图片都在网站上解码zxing.org/w/decode.jspx
  • @jdweng,它应该自动取条码,无需干预。
  • 如果有人会搜索这个,我放弃了这个工作并与另一个 freebarcode.codeplex.com 一起工作。最后一个工作完美。

标签: c# barcode zxing barcode-scanner


【解决方案1】:

我终于完全重启了,因为它没有按预期工作。

我实现了以下算法:如果解码器没有读取条形码,则将图像拆分为 4 并重新启动。

之后效果很好,我认为这就是您提到的网站的工作方式。可惜它没有从头开始使用这种方法。

注意:这段代码远非完美,做了很多假设,如果你复制它并按原样使用它,如果你的图像格式不同,它可能会崩溃OP提供的那个

internal class Program
{
    private static readonly List<BarcodeFormat>  Fmts = new List<BarcodeFormat> { BarcodeFormat.All_1D };

    static void Main(string[] args)
    {
        Bitmap originalBitmap = new Bitmap(@"C:\Users\me\Desktop\6.jpg");
        Bitmap img = CropImage(originalBitmap, new Rectangle(0 , 0, originalBitmap.Width, originalBitmap.Height));
        int width = img.Width;
        int heigth = img.Height;
        int nbOfFrames = 1;
        bool found = false;
        while (!found && width > 10 && heigth > 10)
        {
            if (DecodeImg(img))
            {
                break;
            }
            nbOfFrames *= 4;
            width /= 2;
            heigth /= 2;
            var x = 0;
            var y = 0;

            for (int i = 0; i < nbOfFrames; i++)
            {
                img.Dispose();
                img = new Bitmap(CropImage(originalBitmap, new Rectangle(x, y, width, heigth)));

                if (DecodeImg(img))
                {
                    found = true;
                }
                x += width;
                if (x < originalBitmap.Width)
                {
                    continue;
                }
                x = 0;
                y += heigth;
                if (y >= originalBitmap.Height)
                {
                    y = 0;
                }
            }
        }


    }

    public static Bitmap CropImage(Image img, Rectangle cropArea)
    {
        Bitmap bmpImage = new Bitmap(img);
        return bmpImage.Clone(cropArea, PixelFormat.Format24bppRgb);
    }

    public static bool DecodeImg(Bitmap img)
    {
        BarcodeReader reader = new BarcodeReader
        {
            AutoRotate = true,
            TryInverted = true,
            Options =
            {
                PossibleFormats = Fmts,
                TryHarder = true,
                ReturnCodabarStartEnd = true,
                PureBarcode = false
            }
        };
        Result result = reader.Decode(img);

        if (result != null)
        {
            Console.WriteLine(result.BarcodeFormat);
            Console.WriteLine(result.Text);
            return true;
        }

        Console.Out.WriteLine("Raté");
        return false;
    }
}

【讨论】:

  • 嗯,它在我的机器上导致 null。你能把解决方案上传到某个地方吗?
  • @GiorgiNakeuri 我会的,给我一个小时
  • 可能是我使用了一些不正确的库版本?我安装了这个 nuget 包&lt;package id="ZXing.Net" version="0.14.0.1" targetFramework="net45" /&gt;。你能检查你的吗?
  • 如果有人会搜索这个,我放弃了这个工作并与另一个 freebarcode.codeplex.com 一起工作。最后一个工作完美。
  • @JaydeepKarena 然后您可以提出一个新问题并解释您的实施有什么问题。我很乐意看看它。