Problem reading Code-128 barcodes created using Aspose.Barcode

Hello,


I am attempting to create a series of various sized code-128 bar codes using Aspose.Barcode, then read those various sized bar codes using Aspose.Barcode. Unfortunately, I am having little success.

The various sized, programmatically generated bar codes are printed out, then scanned via a Xerox WorkCenter 5945 (400DPI) to a network folder. Once scanned, a software application utilizing Aspose.Barcode attempts to read the various bar codes and record the data contained within the bar codes. As stated, I am having very little success getting a read of high enough quality to be useful. I have attached a PDF that contains all of the code-128 bar codes, of which only the second one could be read to any level of success.

The code used to create the bar codes and read the bar codes was taken directly from your documentation, but I will provide it to you anyway:

Creation:

BarCodeBuilder bb = new BarCodeBuilder();
Aspose.BarCode.License bcLicense = new Aspose.BarCode.License();
System.IO.Stream bcstream = System.IO.File.OpenRead(Server.MapPath("~") + @"\bin\Aspose.BarCode.lic");
bcLicense.SetLicense(bcstream);
bcstream.Close();

bb.CodeText = “some test value”;
bb.CodeLocation = CodeLocation.None;
bb.SymbologyType = Symbology.Code128;
bb.BackColor = System.Drawing.Color.White;
bb.ImageQuality = ImageQualityMode.Default;
bb.AutoSize = false;
bb.ImageHeight = 8.75f;
bb.ImageWidth = 86.25f;
bb.GraphicsUnit = System.Drawing.GraphicsUnit.Millimeter;
bb.Save(“test.png”, System.Drawing.Imaging.ImageFormat.Png);


Obviously the ImageHeight and ImageWidth differ throughout the provided PDF. 8.75f and 86.25f are what generated the first bar code on the attached document.

Reading:

Aspose.Pdf.Facades.PdfExtractor pdfExtractor = new Aspose.Pdf.Facades.PdfExtractor();
pdfExtractor.BindPdf(“path to document”);

Document pdfDocument = new Document(document.FullName);

pdfExtractor.StartPage = 1;
pdfExtractor.EndPage = 1;
pdfExtractor.ExtractImageMode = ExtractImageMode.ActuallyUsed;
pdfExtractor.ExtractImage();

while (pdfExtractor.HasNextImage())
{
MemoryStream imageStream = new MemoryStream();
pdfExtractor.GetNextImage(imageStream);
imageStream.Position = 0;

using (Aspose.BarCodeRecognition.BarCodeReader barcodeReader = new Aspose.BarCodeRecognition.BarCodeReader(imageStream, BarCodeReadType.Code128))
{
barcodeReader.ImageBinarizationHints RecognitionHints.ImageBinarization.Grayscale;
barcodeReader.ImageBinarizationHints = RecognitionHints.ImageBinarization.MedianSmoothing;
barcodeReader.ImageBinarizationHints = RecognitionHints.ImageBinarization.ComplexBackground;

string barcodeCodeText = barcodeReader.GetCodeText();
string barcodeReadType = barcodeReader.GetReadType().ToString();
float barcodeQuality = barcodeReader.GetRecognitionQuality();

System.Console.WriteLine("Codetext found: " + barcodeCodeText + ", Symbology: " + barcodeReadType + ", Quality: " + barcodeQuality.ToString());

barcodeReader.Close();
}
}


Thank you for your help.

Hi Joshua,


Thank you for contacting support. We’re working over your query and will get back to you soon.

Hi Joshua,


Thank you for being patient. We have tested your sample PDF file against the latest builds of Aspose.BarCode 6.9.0 and Aspose.Pdf 10.3.0. We can recognize all Code128 codes by using PdfConverter class. Please use this sample code below:

[.NET, C#]
int pageIndex = 0;

// bind the pdf
PdfConverter converter = new PdfConverter();
converter.BindPdf(@“C:\temp\XEF440217_DOC004.pdf”);
converter.RenderingOptions.BarcodeOptimization = true;
converter.DoConvert();

while (converter.HasNextImage())
{
pageIndex++;
// Console.WriteLine(“Getting next Page…”);
MemoryStream pageStream = new MemoryStream();
converter.GetNextImage(pageStream, ImageFormat.Png);
using (BarCodeReader reader = new BarCodeReader(pageStream, BarCodeReadType.Code128))
{
while (reader.Read())
{
Console.WriteLine(“Page#” + pageIndex + " Code Text: " + reader.GetCodeText() + " Type: " + reader.GetReadType());
}
}
}
Results:
Page#1 Code Text: 131.732945.138 Type: Code128
Page#1 Code Text: 131.732945.138 Type: Code128
Page#1 Code Text: 131.732945.138 Type: Code128
Page#1 Code Text: 131.732945.138 Type: Code128
Page#1 Code Text: 131.732945.138 Type: Code128
Page#1 Code Text: 131.732945.138 Type: Code128

We hope, this helps. Please feel free to reply us in case of any confusion or questions.

Hello Imran,


Thank you so much for your prompt reply. Your response has helped me tremendously!

I do have a few questions for you:

First, what class libraries are you using, Aspose.BarCode or Aspose.BarCodeRecognition? The reason I ask is that I no longer have access to methods like GetRecognitionQuality().

Second, another important part of this is that I need the ability to determine if a bar code can even be recognized as I iterate through each page (“live” documents will have differently valued bar codes on each page). To achieve this, I was utilizing the following code. If the Read() was false, I knew that no bar code could be found or detected:

if (barcodeReader.Read().Equals(false))
{
}

Thirdly, if I can read a bar code, I need to check the quality of that bar code. I have found that if the read quality is below 85%, I cannot really trust the data that the read is pulling. Previously, I was utilizing method GetRecognitionQuality() to determine this.

Next, do you recommend using RecognitionHints.ImageBinarization, or is this not needed with this new method?

Finally, I have noticed that PDFs with logos or other images on them do not read well. Is this a known issue? Is there a way around this? Many of my documents will have a letterhead and logo, as well as a bar code.

Once again, thank you for your continued support and for such a great product!

Joshua
Hi Joshua,
Thank you for your inquiries.

Joshua_W:
First, what class libraries are you using, Aspose.BarCode or Aspose.BarCodeRecognition? The reason I ask is that I no longer have access to methods like GetRecognitionQuality().
Please note, we had combined generation and recognition modules from Aspose.BarCode version 6.1.0. So there is now a single library Aspose.BarCode.dll and it has both generation and recognition namespaces. We have tested your sample PDF against the latest build of Aspose.BarCode 6.9.0.
Joshua_W:
Second, another important part of this is that I need the ability to determine if a bar code can even be recognized as I iterate through each page ("live" documents will have differently valued bar codes on each page). To achieve this, I was utilizing the following code. If the Read() was false, I knew that no bar code could be found or detected:

if (barcodeReader.Read().Equals(false))
{
}
Well, you can use while loop to get all barcodes as narrated in our earlier post. There, you can notice a nested loop, upper loop iterate through the PDF pages and export each PDF page to an image, whereas the inner loop iterate through the available barcodes on each PDF image page. In case, the image has three barcodes and Aspose API can only read two barcodes, then Read method call will return true values two times. First time call of the Read method allows to get first barcode credentials and second time call allows to get second barcode credentials. After this, it always returns false value.

Joshua_W:
Thirdly, if I can read a bar code, I need to check the quality of that bar code. I have found that if the read quality is below 85%, I cannot really trust the data that the read is pulling. Previously, I was utilizing method GetRecognitionQuality() to determine this.
Please download and use the latest version of Aspose.BarCode 6.9.0. It has support of GetRecognitionQuality method and you can call it by the BarCodeReader class object.
Joshua_W:
Next, do you recommend using RecognitionHints.ImageBinarization, or is this not needed with this new method?
It offers various filters to apply to the image before scanning process. It's all depend on the image quality. You can apply checks in the source code.
Joshua_W:
Finally, I have noticed that PDFs with logos or other images on them do not read well. Is this a known issue? Is there a way around this? Many of my documents will have a letterhead and logo, as well as a bar code.
Our recommendations for barcode image generation are as follows:

  • Resolution: ideal resolution is 300 dpi to 600 dpi
  • Orientation: The best barcode orientation is horizontal and the worst orientation is diagonal 45 or 225 degree.
  • Color: the best is black and white or near this.
  • Pictures having only barcodes: It's better if we have only barcodes on the picture, because other items can mess gray scaling process.
  • General: if barcode is more clear, recognition will be better.

In case, you are facing issues while recognizing codes from the PDF file, then please provide us these problematic files for testing purposes. We'll take a closer look and reply you accordingly.