Optimization recognition

We experience some problems with the Barcode recognition. We tested 2 methods for reading the barcodes: via GetAllPossibleBarcodes and via GetCodeText.

Apart from the very poor performance which both methods have, we get some strange results.
These are the testscenarios:

  1. Read with enhanced extractor.Resolution (300 instead of 150) AND using MedianSmoothing. Results are:

Test 1, found barcode: 8122

Test 1, found barcode: 7646

Test 1, found barcode: 51

Test 1, found barcode: 57

Test 1, found barcode: 46

Test 2, found barcode: 376

Test 2, found barcode: 376

  1. Read with enhanced extractor.Resolution (300 instead of 150). Results are:

Test 1, found barcode: 8122

Test 1, found barcode: 7646

Test 1, found barcode: 51

Test 1, found barcode: 57

Test 1, found barcode: 46

Test 2, found barcode: 51

Question 1: why does method 2 (via GetCodeText) return different results?

Question 2: We have some PDFs in which the quality of the barcodes is quite poor. What can we do for optimizing those PDFs/images? We experimented with Resolution and MedianSmoothing…

This is the code we use:

PdfExtractor extractor = new PdfExtractor();
extractor.BindPdf(SourcePath);
extractor.StartPage = 1;

extractor.EndPage = 1;
extractor.Resolution = 300;
extractor.ExtractImage();

int imageCounter = 0;

List result = new List();

while (extractor.HasNextImage())
{
imageCounter++;

MemoryStream stream = new MemoryStream();
extractor.GetNextImage(stream);
Bitmap bmp = new Bitmap(stream);
stream.Position = 0;

// TESTING VIA GetAllPossibleBarCodes
using (var reader = new BarCodeReader(new Bitmap(stream), BarCodeReadType.AllSupportedTypes))
{
reader.Read();

//RecognitionHints.ImageBinarization imageBinarization = RecognitionHints.ImageBinarization.MedianSmoothing;
//reader.ImageBinarizationHints = imageBinarization;
 BarCodeReader.PossibleBarCode[] pb1 = reader.GetAllPossibleBarCodes();
reader.Close();

foreach (BarCodeReader.PossibleBarCode pb1s in pb1)
{
Debug.WriteLine(string.Format("Test 1, found barcode: {0}", pb1s.Codetext));
}
}

// TESTING VIA GetCodeText
using (var reader = new BarCodeReader(new Bitmap(stream), BarCodeReadType.AllSupportedTypes))
{

//RecognitionHints.ImageBinarization imageBinarization = RecognitionHints.ImageBinarization.MedianSmoothing;
//reader.ImageBinarizationHints = imageBinarization;

while (reader.Read())
{
if (!result.Contains(reader.GetCodeText()))
result.Add(reader.GetCodeText());
}
}

reader.Close();

foreach (string pb2s in result)
{
Debug.WriteLine(string.Format("Test 2, found barcode: {0}", pb2s));}
}
}
}

Hi Ben,


Thank you for contacting support.
BenV:
Question 1: why does method 2 (via GetCodeText) return different results?
We’ve added GetAllPossibleBarCodes method recently. It works for only 1D barcodes. Second, it returns all possible codes so far results can vary with GetCodeText method. It will be so nice of you if you may please share the problematic PDF document in this forum thread. We shall take a closer look and guide you accordingly.
BenV:
Question 2: We have some PDFs in which the quality of the barcodes is quite poor. What can we do for optimizing those PDFs/images? We experimented with Resolution and MedianSmoothing.
We’ve added a couple of new graphic filters. Please take a look over this helping article: Better and Faster Image Processing for Barcode Recognition

2D barcode symbologies with error correction will have better recognition rate. This is decided by 2D barcode’s error correction level during encode. If you think that the barcodes will be degraded and unreadable, then you can increase error correction value to the max. Please go through helping example topics: 2D Barcodes Basic Features

We’re looking forward to helping you.

Thanks for the fast reply.

I attatched the PDF from question 1.

Setting the ImageBinarizationHints to GrayScale helped a little in performance.
But is is still a long time. ±10 seconds.

Is there any possibility to make it faster?


Thank you for looking in to this

Hi Ben,

Thank you for sharing sample PDF document. I have tested your sample PDF file against the latest builds of Aspose.BarCode 6.0.0 and Aspose.PDF 8.9.1.0. It does not take a long time on my side. Please take a look over the sample code and the results:

// bind the pdf document

PdfExtractor pdfExtractor = new PdfExtractor();

pdfExtractor.BindPdf(@"ASposeExample.pdf");

Console.WriteLine("Extracting images.....");

pdfExtractor.ExtractImage();

int i = 0;

// save images to stream in a loop

while (pdfExtractor.HasNextImage())

{

Console.WriteLine("Getting next image....");

i++;

// save image to stream

MemoryStream imageStream = new MemoryStream();

pdfExtractor.GetNextImage(imageStream, ImageFormat.Png);

Stopwatch stopwatch = new Stopwatch();

stopwatch.Start();

using (BarCodeReader barcodeReader = new BarCodeReader(imageStream, BarCodeReadType.Code128 | BarCodeReadType.DataMatrix))

{

while (barcodeReader.Read())

{

Console.WriteLine(i + " Codetext found: " + barcodeReader.GetCodeText() + ", Symbology: " + barcodeReader.GetReadType().ToString());

}

stopwatch.Stop();

Console.WriteLine("Total time " + stopwatch.Elapsed.Seconds + " seconds");

}

}

Result ( in debug mode):

Extracting images…

Getting next image…

1 Codetext found: 144250100002201, Symbology: Code128

1 Codetext found: 000000000000000206420, Symbology: DataMatrix

Total time 2 seconds

Getting next image…

2 Codetext found: 002560100000000206420, Symbology: DataMatrix

Total time 1 seconds

Getting next image…

3 Codetext found: 144250100002209, Symbology: Code128

Total time 1 seconds

Getting next image…

Total time 0 seconds

Result ( in exe mode):

Extracting images…

Getting next image…

1 Codetext found: 144250100002201, Symbology: Code128

1 Codetext found: 000000000000000206420, Symbology: DataMatrix

Total time 1 seconds

Getting next image…

2 Codetext found: 002560100000000206420, Symbology: DataMatrix

Total time 0 seconds

Getting next image…

3 Codetext found: 144250100002209, Symbology: Code128

Total time 0 seconds

Getting next image…

Total time 0 seconds

By the way, you can cut the part where bar code resides, if some placement rules are present for bar code i.e. top right corner. This can significantly speed up the processing as we will have to process only a small portion of the image.

Please refer to the following documentation: Read Barcode from Specific Region of Image

I hope, this helps. Please do let us know in case of further assistance or questions.