Click here to Skip to main content
14,696,481 members
Articles » General Programming » Algorithms & Recipes » Algorithms
Posted 24 Jan 2006


123 bookmarked

Read Barcodes from an Image

Rate me:
Please Sign up or sign in to vote.
4.36/5 (25 votes)
31 Jan 2006
How to read two-width barcodes from an image in VB.NET.


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.


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

For Each result In scanner.Result

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



  • 01-24-06: Original article.


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


About the Author

Germany Germany
No Biography provided

Comments and Discussions

QuestionDetails of the code ... ? Pin
Member 1103734328-Aug-14 21:24
MemberMember 1103734328-Aug-14 21:24 
AnswerRe: Details of the code ... ? Pin
Member 1103734329-Aug-14 0:36
MemberMember 1103734329-Aug-14 0:36 
QuestionReading Barcode from an image Pin
ZeenY26-Feb-08 0:25
MemberZeenY26-Feb-08 0:25 
GeneralReading barcode type 39 Pin
ciapal19-Feb-08 0:52
Memberciapal19-Feb-08 0:52 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 21:30
Membercyos25-Apr-11 21:30 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 21:44
Membercyos25-Apr-11 21:44 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 21:46
Membercyos25-Apr-11 21:46 
QuestionThe Speed for barcode decoder Pin
jackjack8917-Apr-07 0:30
Memberjackjack8917-Apr-07 0:30 
QuestionChange BMP File Error! Pin
ZhouFuqiang20-Nov-06 17:57
MemberZhouFuqiang20-Nov-06 17:57 
AnswerRe: Change BMP File Error! Pin
mackenb25-Nov-06 5:52
Membermackenb25-Nov-06 5:52 
AnswerRe: Change BMP File Error! [modified] Pin
Robert_Zenz23-Mar-09 5:57
MemberRobert_Zenz23-Mar-09 5:57 
AnswerRe: Change BMP File Error! Pin
Todd Hang8-Oct-09 7:01
MemberTodd Hang8-Oct-09 7:01 
QuestionSome question? Pin
Trinh Tuan21-Sep-06 22:58
MemberTrinh Tuan21-Sep-06 22:58 
GeneralBarcode font Code 128 Pin
angelagke31-Jul-06 19:31
Memberangelagke31-Jul-06 19:31 
GeneralRe: Barcode font Code 128 Pin
mackenb2-Aug-06 2:07
Membermackenb2-Aug-06 2:07 
GeneralRe: Barcode font Code 128 Pin
angelagke2-Aug-06 6:24
Memberangelagke2-Aug-06 6:24 
Questionmailto: Pin
KatliwaMax30-May-06 21:29
MemberKatliwaMax30-May-06 21:29 
Generalhelp me, please! Pin
4aaaa9-Mar-06 17:43
Member4aaaa9-Mar-06 17:43 
GeneralRe: help me, please! [modified] Pin
Pandele Florin31-May-06 2:46
MemberPandele Florin31-May-06 2: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.