Click here to Skip to main content
11,712,820 members (80,411 online)
Click here to Skip to main content

Reading Barcodes from an Image - II

, 21 Aug 2010 CPOL 232.8K 20.9K 202
Rate this:
Please Sign up or sign in to vote.
An example of how to process an image for barcode strings
Sample Image


I recently ran across Benjamin Liedblad's article on reading barcode strings from images. The code was good, but not very useful in the real world. I thought my additions had a place here.

Berend Engelbrecht has vastly improved this project. His new version can be found at BarcodeImaging3.aspx.


Liedblad's barcode reader has two significant flaws:

  • It only reads right-side up barcodes (as he mentioned in his "todo").
  • It assumes that there is nothing other than the barcode in the image.

Frequently, we need to read a barcode from a page full of other text. This is what I set about to do.

Using the Code

First of all, you should check out the original article to see how the barcode reading works. Much of the code is left unchanged.


  • Since the barcode we are looking for may not be at the beginning of the text line that we scan, we can't just scan by blocks of 9 'characters'. Instead, we walk along the scanned pattern one 'bar' at a time, testing it and the 8 bars after it to see if they form a character. If we find a real character, then we skip over the character pattern and begin again.
  • Since there are text and other distractions on many pages, we need to cut the page up into sections and look for barcodes in each section. I've found that 50 is a good number of sections. While this seems like a lot, it's enough to ensure that we get the barcode we're looking for. Your mileage may vary. To this end, I added a startheight and endheight to ReadCode39(). Then we just need to calculate where each section begins and ends. This is accomplished easily in a for loop:
    for (int i=0; i < numscans; i++)
        read = ReadCode39(bmp,i * (bmp.Height / numscans), 
               (i * (bmp.Height / numscans))+ (bmp.Height / numscans));
  • We need to check all four page orientations, which is handled easily with .NET's Bitmap control:
    for (int i=0; i < 4; i++)
        VScanPageCode39(ref CodesRead, bmp,numscans);

    You can, of course not use this if you know that all of your pages are the right way up.

  • Finally, since we scan so many times and receive so many barcodes (including the backwards version of what we are looking for), we need to store all of the barcodes that we found. For this, I decided to use an ArrayList. The code currently returns all of the barcodes, but since most people supply some sort of pattern to their code (usually beginning and ending with astericks "*"), it's easy enough to pick out the right one.

Running the Demo

When you run the demo and load "SamplePage.jpg", first perform "Scan Barcode". This is essentially identical to Liedblad's original code. You will get "*JR7". Not very helpful. Then perform "Scan Page". Now you have a list of barcodes, one of which is the one you want (*surprise*)... and one is its mirror image (P4 9V*VK P). As you can see, it's pretty easy to pick out what you need.


  • 5-15-05: Original
  • 8-20-10: Article updated


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior) InGage Networks
United States United States
James is currently a Software developer after a detour from that profession into IT because he 'didn't want to sit in a cubicle and wanted to deal with people.' He has since learned that people are stupid, and enjoys his cubicle greatly.

You may also be interested in...

Comments and Discussions

GeneralRe: Implementation Tips Pin
Suntai5-May-06 2:32
memberSuntai5-May-06 2:32 
GeneralRe: Implementation Tips Pin
vbdotnetcoder200510-May-06 10:24
membervbdotnetcoder200510-May-06 10:24 
GeneralGreat topic Pin
MichaelWells6-Jul-05 1:28
memberMichaelWells6-Jul-05 1:28 
GeneralRe: Great topic Pin
vbdotnetcoder200516-Jan-06 8:41
membervbdotnetcoder200516-Jan-06 8:41 
GeneralRe: Great topic Pin
Ollipolli20-Jan-06 2:31
memberOllipolli20-Jan-06 2:31 
GeneralRe: Great topic Pin
vbdotnetcoder200520-Jan-06 11:51
membervbdotnetcoder200520-Jan-06 11:51 
GeneralRe: Great topic Pin
qlipoth5-Feb-06 16:03
memberqlipoth5-Feb-06 16:03 
GeneralRe: Great topic Pin
qlipoth5-Feb-06 16:08
memberqlipoth5-Feb-06 16:08 
Yeah, this code is pretty much completely intolerant of angles, as it's meant for scanned documents... and if someone's scanning documents diagonally, they should be beaten severely.

I think that an angled reader would be a great deal more difficult to build, however if the number of scans were increased dramaticly (well above 50) then at least one of the scans should read a barcode as long as the angle isn't severe enough that no scan gets across the whole barcode.

Barcode scanners in Supermarkets have dozens of scan lines at differant angles to accomplish this.
GeneralRe: Great topic Pin
Rudy P11-Feb-06 0:59
memberRudy P11-Feb-06 0:59 
GeneralRe: Great topic Pin
qlipoth14-Feb-06 4:26
memberqlipoth14-Feb-06 4:26 
GeneralRe: Great topic Pin
StephanGreiwe20-Oct-06 23:45
memberStephanGreiwe20-Oct-06 23:45 
GeneralRe: Great topic Pin
Section8NZ22-May-07 20:20
memberSection8NZ22-May-07 20:20 
GeneralRe: Great topic Pin
BruceN8-Jul-07 17:18
memberBruceN8-Jul-07 17:18 
GeneralRe: Great topic Pin
Section8NZ8-Jul-07 22:12
memberSection8NZ8-Jul-07 22:12 
GeneralRe: Great topic Pin
Kerem Kat8-Apr-08 13:14
memberKerem Kat8-Apr-08 13:14 

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 | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 21 Aug 2010
Article Copyright 2005 by qlipoth
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid