Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Read Barcodes from an Image

0.00/5 (No votes)
31 Jan 2006 1  
How to read two-width barcodes from an image in VB.NET.

Introduction

The following article describes an algorithm in VB.NET to detect 2 of 5 interleaved barcodes, which are widely used in industrial applications like document management (DMS). The algorithm can easily be extended to detect every other two-width barcode like 2 of 5 standard and Code 39.

Background

If you want to read barcodes from an image in professional applications, for example in document processing, you mainly face two problems:

  1. You have to use an expensive barcode tool, mainly because of runtime licenses.
  2. Your customer wants you to read barcode from documents which are of bad quality (fax, copies, photos, �).

The solution: Develop an algorithm on your own and tune it to the needs of your customer. This article explains how to develop a generic algorithm for two-width barcodes and a working example to read 2 of 5 interleaved.

Using the code

Scanning the barcodes is as simple as this:

Dim bmp As New Bitmap("d:\sample2of5.bmp") 
Dim scanner As New gmseNWScanner(_
        New gmseBarcodeDef2of5interleaved)
Dim result As gmseNWResult

Scanner.Scan(bmp)
For Each result In scanner.Result
    Debug.WriteLine(result.Text)
next

The main class is gmseNWScanner. It contains the generic scanning algorithm for two-width barcodes. The parameters are defined in the abstract class gmseNWBarcodeDef. The class gmseBarcodeDef2of5interleaved implements gmseNWBarcodeDef to decode 2 of 5 interleaved. The detected barcodes are saved in the following classes: gmseNWResult and gmseNWResultCollection.

Points of interest

Two-Width barcodes are represented with only two bar types: a narrow bar (n) and a wide bar (w). The tricky part is to get this representation from an image. Translating to text is straightforward. Scanning is done row by row (see chart). The array cBarWidth is computed. It contains the width of each bar in the current row. cBarWidth is scanned from left to right. The algorithm takes a subset of cBarWidth to find a candidate for a valid barcode. The subset is considered to be a candidate if the bars can be classified as narrow and wide bars. If a candidate is found, the algorithm extends the subset until a bar is found, this cannot be classified as a narrow or wide bar. The subset is then translated to the nw representation. The nw representation is then translated to the text with a Hashtable.

If you play with the algorithm you will see that sometimes regions in the image are identified as a barcode by accident. This is a common problem with barcode detection. Being restrictive in filtering out the unwanted regions will lead to an algorithm that does not detect barcodes on low quality images. So the best solution is to add a checksum. If possible, do some additional tests like using a fixed length of the coded text.

References

History

  • 01-24-06: Original article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here