LP13
LP13

Reputation: 34149

reading barcode using Aspose

I have a PDF file that includes a page with the Barcode. The barcode was originally created using DecodeType.Code128
I am using ASPOSE.PDF and Aspose.Barcode to find the barcode and the page number.

        var barcodePageNumbers = new List<int>();
        var currentPageNumber = 0;

        using (PdfConverter converter = new PdfConverter())
        {
            converter.BindPdf(sourcefilePath);
            converter.RenderingOptions.BarcodeOptimization = true;
            converter.DoConvert();
            while (converter.HasNextImage())
            {
                currentPageNumber++;
                using (var ms = new MemoryStream())
                {
                    converter.GetNextImage(ms);
                    ms.Seek(0L, SeekOrigin.Begin);
                    BarCodeReader reader = new BarCodeReader(ms, DecodeType.Code128);
                    while (reader.Read())
                    {
                        var text = reader.GetCodeText();
                        if (text == _barcodeTextToMatch)
                        {
                            barcodePageNumbers.Add(currentPageNumber);
                        }
                    }
                }
            }
        }

Every now and then we get bad quality barcode pdf like the one attached herewith. (Note the attached PDF has 3 pages. I have created page 1 and 3 using tool. and the 2nd page which has barcode is from the original PDF.)

The code above works as long as the barcode quality is good. but if the quality is bad then it not able able to recognize its barcode.

Sample File

What other optimization technique can be used here?

Upvotes: 0

Views: 817

Answers (1)

Alifesoft
Alifesoft

Reputation: 1

At first, what the version of Aspose.PDF and Aspose.Barcode do you use? Because I tried the pdf file on the last Aspose.Barcode(develop version) it is recognized well.

string lFileName = @"d:\save\rec\merged.pdf";
var currentPageNumber = 0;
var barcodePageNumbers = new List<int>();

using (Aspose.Pdf.Facades.PdfConverter converter = new Aspose.Pdf.Facades.PdfConverter())
{
    converter.BindPdf(lFileName);
    converter.RenderingOptions.BarcodeOptimization = true;
    converter.DoConvert();
    while (converter.HasNextImage())
    {
        currentPageNumber++;
        using (var ms = new MemoryStream())
        {
            converter.GetNextImage(ms);
            ms.Seek(0L, SeekOrigin.Begin);
            BarCodeReader reader = new BarCodeReader(ms, DecodeType.Code128);
            while (reader.Read())
            {
                var text = reader.GetCodeText();
                barcodePageNumbers.Add(currentPageNumber);
            }
        }
    }
}

MessageBox.Show(string.Join(";", barcodePageNumbers.ToArray()));

Here are extracted images from pdf file: page_2.tiff - full image page2_cut.png - cut image. Both are recognized well.


Image distortion, which you can see on the image, is salt and paper issue. We solve this problem in most cases in the recognition module, but current detection module of barcode position could mark barcodes with so strong salt and paper distortion as nonbarcode area. At this time we don’t have optimization for this, but we are developing SVM(support vector machines) detector which detects barcode areas better and also will have options to optimize recognition with these issues. SVM barcode areas detector will be added to Aspose.Barcode in 2020Q1.


Current workaround for this: if you know possible barcode area you can set barcode recognition region with SetBarCodeImage(Bitmap value, Rectangle area) and this helps current Gradient detector to detect so noisy areas.

Upvotes: 0

Related Questions