Click here to Skip to main content
Click here to Skip to main content
Go to top

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.

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

Share

About the Author

mackenb

Germany Germany
No Biography provided

Comments and Discussions

 
QuestionDetails of the code ... ? [modified] PinmemberMember 1103734328-Aug-14 20:24 
AnswerRe: Details of the code ... ? PinmemberMember 1103734328-Aug-14 23:36 
QuestionReading Barcode from an image PinmemberZeenY25-Feb-08 23:25 
AnswerRe: Reading Barcode from an image Pinmembersteve7g10-Jan-12 16:31 
GeneralReading barcode type 39 Pinmemberciapal18-Feb-08 23:52 
GeneralRe: Reading barcode type 39 Pinmembercyos25-Apr-11 20:30 
GeneralRe: Reading barcode type 39 Pinmembercyos25-Apr-11 20:44 
GeneralRe: Reading barcode type 39 Pinmembercyos25-Apr-11 20:46 
QuestionThe Speed for barcode decoder Pinmemberjackjack8916-Apr-07 23:30 
Hi,
i found that the performance of "System.Runtime.InteropServices.Marshal.ReadByte"
is show... anyone can give me a suggestion what method can replace this function so the performance can be improved?
 
thanks
 
Jack
QuestionChange BMP File Error! Pinmemberzfq30820-Nov-06 16:57 
AnswerRe: Change BMP File Error! Pinmembermackenb25-Nov-06 4:52 
AnswerRe: Change BMP File Error! [modified] PinmemberRobert_Zenz23-Mar-09 4:57 
AnswerRe: Change BMP File Error! Pinmembertqhang9998-Oct-09 6:01 
QuestionSome question? PinmemberPham Tuan21-Sep-06 21:58 
GeneralBarcode font Code 128 Pinmemberangelagke31-Jul-06 18:31 
GeneralRe: Barcode font Code 128 Pinmembermackenb2-Aug-06 1:07 
GeneralRe: Barcode font Code 128 Pinmemberangelagke2-Aug-06 5:24 
Questionmailto: PinmemberKatliwaMax30-May-06 20:29 
Generalhelp me, please! Pinmember4aaaa9-Mar-06 16:43 
GeneralRe: help me, please! [modified] PinmemberPandele Florin31-May-06 1:46 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 31 Jan 2006
Article Copyright 2006 by mackenb
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid