Click here to Skip to main content
Licence CPOL
First Posted 24 Mar 2004
Views 122,683
Downloads 2,605
Bookmarked 127 times

Reading Barcodes from an Image

By | 24 Mar 2004 | Article
An example of how to process an image for barcode strings

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Benjamin Liedblad

Software Developer (Senior)

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionBarcodeImaging DLL PingroupAkshay 0070:34 19 Mar '12  
GeneralMy vote of 5 Pinmembermanoj kumar choubey19:37 26 Feb '12  
GeneralHelp me for Barcode 39 Extension Pinmembernaorem19:47 24 Jan '10  
GeneralDone with Locate & Rotate Barcode [modified] Pinmembertechexpertgroup9:48 19 May '08  
GeneralRe: Done with Locate & Rotate Barcode PinmemberBerend Engelbrecht5:27 2 Oct '09  
QuestionBarcode 128 PinmemberIra8422:31 29 Nov '07  
Generalhistogram Pinmembersamis200023:45 7 Sep '06  
GeneralSuggestion If I may.. PinmemberPandele Florin3:38 5 Sep '06  
GeneralRe: Suggestion If I may.. PinmemberBerend Engelbrecht3:10 3 Oct '09  
Generalbarcode v2 Pinmemberwaelahmed4:29 9 Aug '06  
QuestionPossible bug? PinmemberxWayne12:05 25 May '05  
AnswerRe: Possible bug? PinmemberxWayne12:07 25 May '05  
GeneralRe: Possible bug? PinmemberBenjamin Liedblad7:38 27 May '05  
GeneralRe: Possible bug? PinmemberxWayne8:35 27 May '05  
GeneralRe: Possible bug? PinmemberReelix19:18 18 Nov '08  
GeneralRe: Possible bug? [modified: new CodeProject version published] PinmemberBerend Engelbrecht7:35 2 Oct '09  
GeneralRe: Possible bug? PinmemberReelix20:33 4 Oct '09  
GeneralInteresting PinmemberJoel Holdsworth6:25 25 Mar '04  
Generalvision algorithms PinsussHavoxx2:35 31 Mar '04  
GeneralRe: Interesting Pinmembergpj geoff17:07 31 Mar '04  
GeneralRe: Interesting PinmemberLucian_RT8:36 20 Jun '05  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 25 Mar 2004
Article Copyright 2004 by Benjamin Liedblad
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid