Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / Visual Basic
Article

Read Barcodes from an Image

Rate me:
Please Sign up or sign in to vote.
4.36/5 (25 votes)
31 Jan 20062 min read 204.5K   8.9K   125   19
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:

VB
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.

Image 1

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


Written By
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerRe: Change BMP File Error! [modified] Pin
Robert_Zenz23-Mar-09 4:57
Robert_Zenz23-Mar-09 4:57 
AnswerRe: Change BMP File Error! Pin
Todd Hang8-Oct-09 6:01
Todd Hang8-Oct-09 6:01 
QuestionSome question? Pin
Trinh Tuan21-Sep-06 21:58
Trinh Tuan21-Sep-06 21:58 
GeneralBarcode font Code 128 Pin
angelagke31-Jul-06 18:31
angelagke31-Jul-06 18:31 
GeneralRe: Barcode font Code 128 Pin
mackenb2-Aug-06 1:07
mackenb2-Aug-06 1:07 
GeneralRe: Barcode font Code 128 Pin
angelagke2-Aug-06 5:24
angelagke2-Aug-06 5:24 
Questionmailto: Pin
KatliwaMax30-May-06 20:29
KatliwaMax30-May-06 20:29 
Generalhelp me, please! Pin
4aaaa9-Mar-06 16:43
4aaaa9-Mar-06 16:43 
GeneralRe: help me, please! [modified] Pin
Pandele Florin31-May-06 1:46
Pandele Florin31-May-06 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.