ZXing how to count the number of similar barcodes on the same image

114 views Asked by At

I need to count the number of similar barcodes on a single image. For example, if the barcode "123" is pasted 6 times on a page, and the barcode "456" is pasted 2 times on this same page, I must be able to trace the number of times I read "123" and "456". I use ZXing.Net to achieve this goal. Unfortunately, I only get 2 results when I use BarcodeReader.DecodeMultiple, one per different barcode value, but not associated count.

How can I achieve this ?

Below my testing code :

// See https://aka.ms/new-console-template for more information
using PdfSharpCore.Pdf.IO;
using PdfSharpCore.Pdf;
using ZXing;
using ZXing.Client.Result;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;
using ZXing.Windows.Compatibility;
using ZXingTester;

string pdfPath = args[0];

BarcodeReader<System.Drawing.Bitmap> barcodeReader = new(null, source => new BitmapLuminanceSource(source), source => new HybridBinarizer(source))
{
    AutoRotate = true,
    Options = new DecodingOptions { TryHarder = true, TryInverted = true }
};

barcodeReader.ResultFound += result =>
{
    Console.WriteLine("================");
    Console.WriteLine("Barcode format: {0}", result.BarcodeFormat);

    if (result.ResultMetadata.ContainsKey(ResultMetadataType.UPC_EAN_EXTENSION))
    {
        Console.WriteLine("UPC/EAN Extension: {0}", result.ResultMetadata[ResultMetadataType.UPC_EAN_EXTENSION]);
    }
    var parsedResult = ResultParser.parseResult(result);

    if (parsedResult != null)
    {
        Console.WriteLine("nParsed result: {0}", parsedResult.DisplayResult);
    }

    Console.WriteLine("================");
};


PdfDocument document = PdfReader.Open(pdfPath);
int pageNumber = 1;

// Iterate pages
foreach (PdfPage page in document.Pages)
{
    Console.WriteLine("****************");
    Console.WriteLine("** PAGE {0}", pageNumber++);

    foreach (var bitmap in PdfSupport.GetBitmapsFromPdfPage(page))
    {
        _ = barcodeReader.DecodeMultiple(bitmap);
    }

    Console.WriteLine("****************");
}

Thanks

EDIT: I tried to change the code of ZXing.net as suggested by Michael. I commented code lines that create this "disitnct" behavior in GenericMultipleBarcodeReader :

private void doDecodeMultiple(BinaryBitmap image, IDictionary<DecodeHintType, object> hints, IList<Result> results, int xOffset, int yOffset, int currentDepth)
    {
        if (currentDepth > MAX_DEPTH)
        {
            return;
        }

        Result result = _delegate.decode(image, hints);
        if (result == null)
            return;

        //bool alreadyFound = false;
        //for (int i = 0; i < results.Count; i++)
        //{
        //    Result existingResult = (Result)results[i];
        //    if (existingResult.Text.Equals(result.Text))
        //    {
        //        alreadyFound = true;
        //        break;
        //    }
        //}
        //if (!alreadyFound)
        //{
        results.Add(translateResultPoints(result, xOffset, yOffset));
        //}`

It was then that I understood the need to leave this code: ZXing reads the same barcode several times. I think this behavior is related to the way it scans the image to detect barcodes, but it falls back the same many times hence the need to deduplicate.

No solution for me at the moment :(

0

There are 0 answers