Click here to Skip to main content
Click here to Skip to main content

Reading Barcodes from an Image

By , 24 Mar 2004
 

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
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Bugerror in reading barcodemembersan.1234531 Aug '12 - 2:14 
QuestionBarcodeImaging DLLgroupAkshay 00719 Mar '12 - 0:34 
GeneralMy vote of 5membermanoj kumar choubey26 Feb '12 - 19:37 
GeneralHelp me for Barcode 39 Extensionmembernaorem24 Jan '10 - 19:47 
GeneralDone with Locate & Rotate Barcode [modified]membertechexpertgroup19 May '08 - 9:48 
GeneralRe: Done with Locate & Rotate BarcodememberBerend Engelbrecht2 Oct '09 - 5:27 
QuestionBarcode 128memberIra8429 Nov '07 - 22:31 
Generalhistogrammembersamis20007 Sep '06 - 23:45 
GeneralSuggestion If I may..memberPandele Florin5 Sep '06 - 3:38 
GeneralRe: Suggestion If I may..memberBerend Engelbrecht3 Oct '09 - 3:10 
Generalbarcode v2memberwaelahmed9 Aug '06 - 4:29 
QuestionPossible bug?memberxWayne25 May '05 - 12:05 
AnswerRe: Possible bug?memberxWayne25 May '05 - 12:07 
GeneralRe: Possible bug?memberBenjamin Liedblad27 May '05 - 7:38 
GeneralRe: Possible bug?memberxWayne27 May '05 - 8:35 
GeneralRe: Possible bug?memberReelix18 Nov '08 - 19:18 
GeneralRe: Possible bug? [modified: new CodeProject version published]memberBerend Engelbrecht2 Oct '09 - 7:35 
GeneralRe: Possible bug?memberReelix4 Oct '09 - 20:33 
GeneralInterestingmemberJoel Holdsworth25 Mar '04 - 6:25 
Generalvision algorithmssussHavoxx31 Mar '04 - 2:35 
GeneralRe: Interestingmembergpj geoff31 Mar '04 - 17:07 
GeneralRe: InterestingmemberLucian_RT20 Jun '05 - 8:36 

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

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