Click here to Skip to main content
6,291,124 members and growing! (15,760 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate

Reading Barcodes from an Image

By Benjamin Liedblad

An example of how to process an image for barcode strings
C#.NET 1.1, WinXPVS.NET2003, Dev
Posted:24 Mar 2004
Views:89,834
Bookmarked:83 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
20 votes for this article.
Popularity: 5.29 Rating: 4.07 out of 5
2 votes, 10.0%
1
1 vote, 5.0%
2
4 votes, 20.0%
3
4 votes, 20.0%
4
9 votes, 45.0%
5

Introduction

I recently ran across Neil Van Eps series of articles on how to draw barcodes and found myself wondering how easy it would be to process an image to read barcodes. Attached is a demo project that shows one method for reading Code39.

Background

I've been a Basic programmer since age 8 (love those GOTO's... Icky!), a VB4->VB6 programmer for ages (OK... Only since college), and a VB.NET programmer since it was released, but exclusively .NET for only about a year. Basically (sorry for the pun), I've been able to get by with very little knowledge of C or C++. I recently needed to do some image processing so I checked out The Code Project and found a series of articles titled Image Processing for Dummies with C# and GDI+ by Christian Graus. YES! I had finally found what I was looking for, an example on image processing. I started to translate the C# code to VB.NET (usually this is an easy task) and found it nearly impossible... Needless to say, I am now a C# developer as well. :)

Using the code

Although there are at least ten ways (that I can think of) to skin a cat, I selected a method for processing the image that was relatively simple just to prove the concept. First, I create an average of the pixels (histogram) down the image:

 
 
  for(int y=0;y<bmp.Height;++y)
  {
   for(int x=0; x < bmp.Width; ++x )
   {
  // Add up all the pixel values vertically (average the R,G,B channels) 

    vertSum[x] += ((p[0] + (p+1)[0] + (p+2)[0])/3);
    
    p+=3;
   }
   p += nOffset;
  }
 
Next run through the resulting array to find the narrow and wide bars:
 
 
  for(int i = 0; i < vertHist.histogram.Length; ++i)
  {
   ...
  }
 
Finally, lookup the narrow/wide bar pattern to match it to a character:
 
 
  dataString += parsePattern(new string(pattern));
  
 

TODO

The method I used to get the bar pattern isn't what I would call robust. I'd eventually like to figure out a better method for finding the barcode patterns (in any orientation) in addition to adding support for the various barcode types.

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

Benjamin Liedblad


Member

Occupation: Software Developer (Senior)
Location: United States United States

Other popular .NET Framework articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralDone with Locate & Rotate Barcode [modified] Pinmembertechexpertgroup10:48 19 May '08  
QuestionBarcode 128 PinmemberIra8423:31 29 Nov '07  
Generalhistogram Pinmembersamis20000:45 8 Sep '06  
GeneralSuggestion If I may.. PinmemberPandele Florin4:38 5 Sep '06  
Generalbarcode v2 Pinmemberwaelahmed5:29 9 Aug '06  
GeneralPossible bug? PinmemberxWayne13:05 25 May '05  
GeneralRe: Possible bug? PinmemberxWayne13:07 25 May '05  
GeneralRe: Possible bug? PinmemberBenjamin Liedblad8:38 27 May '05  
GeneralRe: Possible bug? PinmemberxWayne9:35 27 May '05  
GeneralRe: Possible bug? PinmemberReelix20:18 18 Nov '08  
GeneralInteresting PinmemberJoel Holdsworth7:25 25 Mar '04  
Generalvision algorithms PinsussHavoxx3:35 31 Mar '04  
GeneralRe: Interesting Pinmembergpj geoff18:07 31 Mar '04  
GeneralRe: Interesting PinmemberLucian_RT9:36 20 Jun '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 24 Mar 2004
Editor: Nishant Sivakumar
Copyright 2004 by Benjamin Liedblad
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project