Click here to Skip to main content
15,890,345 members
Articles / Multimedia / GDI+

Reading Barcodes from an Image - II

Rate me:
Please Sign up or sign in to vote.
4.74/5 (35 votes)
21 Aug 2010CPOL2 min read 337K   24.8K   205   51
An example of how to process an image for barcode strings
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.

Berend Engelbrecht has vastly improved this project. His new version can be found at BarcodeImaging3.aspx.

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:
    C#
    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:
    C#
    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
  • 8-20-10: Article updated

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) InGage Networks
United States United States
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.

Comments and Discussions

 
GeneralThanks Pin
mr_squall26-Aug-09 21:54
mr_squall26-Aug-09 21:54 
QuestionAny ideas on how to get this code to work in Windows Mobile? Pin
s1dd29-May-09 8:01
s1dd29-May-09 8:01 
AnswerRe: Any ideas on how to get this code to work in Windows Mobile? Pin
s1dd29-May-09 8:14
s1dd29-May-09 8:14 
GeneralRe: Any ideas on how to get this code to work in Windows Mobile? Pin
Berend Engelbrecht3-Oct-09 5:24
Berend Engelbrecht3-Oct-09 5:24 
QuestionAnyone to give me how to read code 128 barcode from an image ? Pin
Narsimha098-Dec-08 8:12
Narsimha098-Dec-08 8:12 
GeneralCan´t read codebars generated using Word... and other tools Pin
neptas13-Feb-08 6:50
neptas13-Feb-08 6:50 
GeneralRe: Can´t read codebars generated using Word... and other tools Pin
Feurich26-Mar-08 1:12
Feurich26-Mar-08 1:12 
GeneralRe: Can´t read codebars generated using Word... and other tools Pin
Berend Engelbrecht3-Oct-09 4:58
Berend Engelbrecht3-Oct-09 4:58 
I had the same problem and looked into the cause. Please consider this enlargement from the leading edge of a barcode, that I generated using a Code39 TrueType font in MS Word. I used the MS Document Image Writer printer to create a tiff image directly from Word, then enlarged and measured it in Photoshop:

Click to view image in new window

The first dark bar encountered has a width of 4 pixels, being a "narrow" bar. The software assumes that the first bar encountered will always be a dark "narrow" bar and uses that to determine if other bars are "narrow" or "wide". A barcode is seen as a pattern of bars of alternating width in light and dark color.

Here is one example of the code that does that determination:
if ((i-nBarStart) > (nNarrowBarWidth))
{
  // The light bar was wider than the narrow bar width, it's a wide bar
As you can see, the comparison will go wrong because the "narrow" bars following the first one are not 4 pixels, but 5, 6 or even 7 pixels wide. All bars > 5 pixels will be regarded as "wide" bars by the software, and the barcode is thus not correctly recognized. Word consistently makes the white gaps wider than the black bars in a barcode font, hence the problem is so prominent when Word is used to create barcode bitmaps.

To solve the problem, I have applied a small change:
private const float WIDEFACTOR = 2.0f; // minimum width of wide bar compared to narrow bar

...

  int nBarWidth = i - nBarStart;
  if (nBarWidth > nNarrowBarWidth * WIDEFACTOR)
  {
    // The light bar was wider than the narrow bar width, it's a wide bar
Now with this version of the code, a bar will not be considered to be "wide" unless it is at least twice as wide as the measured narrow bar width. In the example, only bars >= 9 pixels will be considered to be wide. With this change the barcode created by Word is correctly recognized. I have re-run the software against all my test images and I believe that the change does not negatively affect the detection rate.
GeneralUpdating to read code 128 Pin
cameron molyneux22-Apr-07 17:53
cameron molyneux22-Apr-07 17:53 
GeneralSome question. Pin
Trinh Tuan21-Sep-06 21:23
Trinh Tuan21-Sep-06 21:23 
AnswerRe: Some question. Pin
qlipoth22-Sep-06 13:37
qlipoth22-Sep-06 13:37 
GeneralRe: Some question. Pin
Trinh Tuan22-Sep-06 15:43
Trinh Tuan22-Sep-06 15:43 
GeneralRe: Some question. Pin
vbdotnetcoder200525-Sep-06 8:35
vbdotnetcoder200525-Sep-06 8:35 
GeneralReg:Generating BarCode.. Pin
Kumar Murugesan26-Jul-06 4:48
Kumar Murugesan26-Jul-06 4:48 
GeneralRe: Reg:Generating BarCode.. Pin
qlipoth1-Aug-06 4:57
qlipoth1-Aug-06 4:57 
GeneralRe: Reg:Generating BarCode.. Pin
kirankumarannigeri14-Nov-07 22:36
kirankumarannigeri14-Nov-07 22:36 
GeneralImplementation Tips Pin
vbdotnetcoder20052-Feb-06 5:47
vbdotnetcoder20052-Feb-06 5:47 
GeneralRe: Implementation Tips Pin
Suntai5-May-06 2:32
Suntai5-May-06 2:32 
GeneralRe: Implementation Tips Pin
vbdotnetcoder200510-May-06 10:24
vbdotnetcoder200510-May-06 10:24 
GeneralGreat topic Pin
Memetican6-Jul-05 1:28
Memetican6-Jul-05 1:28 
GeneralRe: Great topic Pin
vbdotnetcoder200516-Jan-06 8:41
vbdotnetcoder200516-Jan-06 8:41 
GeneralRe: Great topic Pin
Ollipolli20-Jan-06 2:31
Ollipolli20-Jan-06 2:31 
GeneralRe: Great topic Pin
vbdotnetcoder200520-Jan-06 11:51
vbdotnetcoder200520-Jan-06 11:51 
GeneralRe: Great topic Pin
qlipoth5-Feb-06 16:03
qlipoth5-Feb-06 16:03 
GeneralRe: Great topic Pin
qlipoth5-Feb-06 16:08
qlipoth5-Feb-06 16:08 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.