12,241,814 members (24,612 online)
Add your own
alternative version

157.8K views
8.8K downloads
139 bookmarked
Posted

# Reading Barcodes from an Image

, 24 Mar 2004 CPOL
 Rate this:
Please Sign up or sign in to vote.
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

 Software Developer (Senior) United States
No Biography provided

## Comments and Discussions

 First Prev Next
 PDF document Member 1139256621-Jan-15 11:41 Member 11392566 21-Jan-15 11:41
 error in reading barcode san.1234531-Aug-12 3:14 san.12345 31-Aug-12 3:14
 BarcodeImaging DLL Akshay 00719-Mar-12 1:34 Akshay 007 19-Mar-12 1:34
 My vote of 5 manoj kumar choubey26-Feb-12 20:37 manoj kumar choubey 26-Feb-12 20:37
 Help me for Barcode 39 Extension naorem24-Jan-10 20:47 naorem 24-Jan-10 20:47
 Done with Locate & Rotate Barcode [modified] techexpertgroup19-May-08 10:48 techexpertgroup 19-May-08 10:48
 Re: Done with Locate & Rotate Barcode Berend Engelbrecht2-Oct-09 6:27 Berend Engelbrecht 2-Oct-09 6:27
 Barcode 128 Ira8429-Nov-07 23:31 Ira84 29-Nov-07 23:31
 histogram samis20008-Sep-06 0:45 samis2000 8-Sep-06 0:45
 Suggestion If I may.. Pandele Florin5-Sep-06 4:38 Pandele Florin 5-Sep-06 4:38
 Re: Suggestion If I may.. Berend Engelbrecht3-Oct-09 4:10 Berend Engelbrecht 3-Oct-09 4:10
 barcode v2 waelahmed9-Aug-06 5:29 waelahmed 9-Aug-06 5:29
 Possible bug? xWayne25-May-05 13:05 xWayne 25-May-05 13:05
 Re: Possible bug? xWayne25-May-05 13:07 xWayne 25-May-05 13:07
 Re: Possible bug? Benjamin Liedblad27-May-05 8:38 Benjamin Liedblad 27-May-05 8:38
 Re: Possible bug? xWayne27-May-05 9:35 xWayne 27-May-05 9:35
 Re: Possible bug? Reelix18-Nov-08 20:18 Reelix 18-Nov-08 20:18
 Re: Possible bug? [modified: new CodeProject version published] Berend Engelbrecht2-Oct-09 8:35 Berend Engelbrecht 2-Oct-09 8:35
 Re: Possible bug? Reelix4-Oct-09 21:33 Reelix 4-Oct-09 21:33
 Interesting Joel Holdsworth25-Mar-04 7:25 Joel Holdsworth 25-Mar-04 7:25
 vision algorithms Havoxx31-Mar-04 3:35 Havoxx 31-Mar-04 3:35
 Re: Interesting gpj geoff31-Mar-04 18:07 gpj geoff 31-Mar-04 18:07
 Re: Interesting Lucian_RT20-Jun-05 9:36 Lucian_RT 20-Jun-05 9:36
 Last Visit: 31-Dec-99 19:00     Last Update: 29-Apr-16 9:17 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    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
Web02 | 2.8.160426.1 | Last Updated 25 Mar 2004
Article Copyright 2004 by Benjamin Liedblad
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid