ZXing条码扫描器适用于.NET。

3

我正在尝试使用这个库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。我做错了什么? 这是示例图片:

enter image description here


你试过使用简单的条形码图像吗? - Vishnu Prasad V
你能在Windows窗体中显示图像吗?也许可以调整高度、宽度、左上角,以便只传递条形码给扫描器。必要时旋转图像。 - jdweng
@VishnuPrasad,它在某些特定情况下可以正常工作,但我无法检测出来。大多数情况下它都不起作用,而网站https://zxing.org/w/decode.jspx上的每张图片都被解码了。 - Giorgi Nakeuri
@jdweng,它应该自动获取条形码,无需干预。 - Giorgi Nakeuri
如果有人在寻找这个问题的解决方案,我已经放弃了,转而使用另一个:https://freebarcode.codeplex.com/。后者完美地解决了我的问题。 - Giorgi Nakeuri
1个回答

3
我最终进行了完全的重启,因为它没有按预期工作。
我实现了以下算法:如果解码器无法读取条形码,则将图像分成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;
    }
}

嗯,它在我的机器上结果为空。你能否请上传解决方案到某个地方? - Giorgi Nakeuri
@GiorgiNakeuri 我会的,给我一个小时。 - Thomas Ayoub
也许我使用的库版本不正确? 我已经安装了这个NuGet包 <package id="ZXing.Net" version="0.14.0.1" targetFramework="net45" />。 你能检查一下你的吗? - Giorgi Nakeuri
如果有人搜索这个问题,我放弃了让它工作并转而使用另一个 https://freebarcode.codeplex.com/ 。最后一个完美地运作。 - Giorgi Nakeuri
1
@JaydeepKarena,如果你有新的问题并且能解释一下你的实现有什么问题,那么你可以提出新的问题。我很乐意帮你看一下。 - Thomas Ayoub
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接