Click here to Skip to main content
13,453,774 members (42,328 online)
Click here to Skip to main content
Add your own
alternative version


123 bookmarked
Posted 24 Jan 2006

Read Barcodes from an Image

, 31 Jan 2006
Rate this:
Please Sign up or sign in to vote.
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.



  • 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

You may also be interested in...


Comments and Discussions

QuestionDetails of the code ... ? Pin
Member 1103734328-Aug-14 20:24
memberMember 1103734328-Aug-14 20:24 
AnswerRe: Details of the code ... ? Pin
Member 1103734328-Aug-14 23:36
memberMember 1103734328-Aug-14 23:36 
QuestionReading Barcode from an image Pin
ZeenY25-Feb-08 23:25
memberZeenY25-Feb-08 23:25 
AnswerRe: Reading Barcode from an image Pin
steve7g10-Jan-12 16:31
membersteve7g10-Jan-12 16:31 
GeneralReading barcode type 39 Pin
ciapal18-Feb-08 23:52
memberciapal18-Feb-08 23:52 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 20:30
membercyos25-Apr-11 20:30 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 20:44
membercyos25-Apr-11 20:44 
GeneralRe: Reading barcode type 39 Pin
cyos25-Apr-11 20:46
membercyos25-Apr-11 20:46 
QuestionThe Speed for barcode decoder Pin
jackjack8916-Apr-07 23:30
memberjackjack8916-Apr-07 23:30 
QuestionChange BMP File Error! Pin
zfq30820-Nov-06 16:57
memberzfq30820-Nov-06 16:57 
AnswerRe: Change BMP File Error! Pin
mackenb25-Nov-06 4:52
membermackenb25-Nov-06 4:52 
AnswerRe: Change BMP File Error! [modified] Pin
Robert_Zenz23-Mar-09 4:57
memberRobert_Zenz23-Mar-09 4:57 
AnswerRe: Change BMP File Error! Pin
tqhang9998-Oct-09 6:01
membertqhang9998-Oct-09 6:01 
QuestionSome question? Pin
Pham Tuan21-Sep-06 21:58
memberPham Tuan21-Sep-06 21:58 
GeneralBarcode font Code 128 Pin
angelagke31-Jul-06 18:31
memberangelagke31-Jul-06 18:31 
GeneralRe: Barcode font Code 128 Pin
mackenb2-Aug-06 1:07
membermackenb2-Aug-06 1:07 
GeneralRe: Barcode font Code 128 Pin
angelagke2-Aug-06 5:24
memberangelagke2-Aug-06 5:24 
Questionmailto: Pin
KatliwaMax30-May-06 20:29
memberKatliwaMax30-May-06 20:29 
Generalhelp me, please! Pin
4aaaa9-Mar-06 16:43
member4aaaa9-Mar-06 16:43 
GeneralRe: help me, please! [modified] Pin
Pandele Florin31-May-06 1:46
memberPandele 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180318.3 | Last Updated 31 Jan 2006
Article Copyright 2006 by mackenb
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid