Click here to Skip to main content
6,295,667 members and growing! (14,772 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Graphics     Intermediate

Reading Barcodes from an Image - II

By qlipoth

An example of how to process an image for barcode strings.
C#.NET 1.1, WinXP, GDI+, VS.NET2003, Dev
Posted:20 Jun 2005
Views:74,353
Bookmarked:79 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
18 votes for this article.
Popularity: 4.28 Rating: 3.41 out of 5
3 votes, 16.7%
1

2

3
4 votes, 22.2%
4
11 votes, 61.1%
5

Sample Image

Introduction

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.

Background

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.

Changes:

  • 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++)
    {
        bmp.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
        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.

History

  • 5-15-05: Original.

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

About the Author

qlipoth


Member
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.
Occupation: Web Developer
Location: United States United States

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Really cool visual FX
    A set of classes for doing stunning visual effects, including water, plasma and fire.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
GeneralAny ideas on how to get this code to work in Windows Mobile? Pinmembers1dd9:01 29 May '09  
GeneralRe: Any ideas on how to get this code to work in Windows Mobile? Pinmembers1dd9:14 29 May '09  
GeneralAnyone to give me how to read code 128 barcode from an image ? PinmemberMember 45876799:12 8 Dec '08  
GeneralCan´t read codebars generated using Word... and other tools Pinmemberneptas7:50 13 Feb '08  
GeneralRe: Can´t read codebars generated using Word... and other tools PinmemberFeurich2:12 26 Mar '08  
GeneralUpdating to read code 128 Pinmembercameron molyneux18:53 22 Apr '07  
GeneralSome question. PinmemberPham Tuan22:23 21 Sep '06  
AnswerRe: Some question. Pinmemberqlipoth14:37 22 Sep '06  
GeneralRe: Some question. PinmemberPham Tuan16:43 22 Sep '06  
GeneralRe: Some question. Pinmembervbdotnetcoder20059:35 25 Sep '06  
GeneralReg:Generating BarCode.. PinmemberKumar Murugesan5:48 26 Jul '06  
GeneralRe: Reg:Generating BarCode.. Pinmemberqlipoth5:57 1 Aug '06  
GeneralRe: Reg:Generating BarCode.. Pinmemberkirankumarannigeri23:36 14 Nov '07  
GeneralImplementation Tips Pinmembervbdotnetcoder20056:47 2 Feb '06  
GeneralRe: Implementation Tips PinmemberSuntai3:32 5 May '06  
GeneralRe: Implementation Tips Pinmembervbdotnetcoder200511:24 10 May '06  
GeneralGreat topic PinmemberMichaelWells2:28 6 Jul '05  
GeneralRe: Great topic Pinmembervbdotnetcoder20059:41 16 Jan '06  
GeneralRe: Great topic PinmemberOllipolli3:31 20 Jan '06  
GeneralRe: Great topic Pinmembervbdotnetcoder200512:51 20 Jan '06  
GeneralRe: Great topic Pinmemberqlipoth17:03 5 Feb '06  
GeneralRe: Great topic Pinmemberqlipoth17:08 5 Feb '06  
GeneralRe: Great topic PinmemberRudy P1:59 11 Feb '06  
GeneralRe: Great topic Pinmemberqlipoth5:26 14 Feb '06  
GeneralRe: Great topic PinmemberStephanGreiwe0:45 21 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jun 2005
Editor: Smitha Vijayan
Copyright 2005 by qlipoth
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project