Click here to Skip to main content
15,860,861 members
Articles / Desktop Programming / Win32

Reading Barcodes from an Image - III

Rate me:
Please Sign up or sign in to vote.
4.88/5 (122 votes)
19 Oct 2009CPOL4 min read 1.2M   95.3K   414   158
Detects Code39, EAN, and Code128 barcodes in an image.

Screenshot

Acknowledgement

This project is based on the CodeProject article: Reading Barcodes from an Image - II by qlipoth. I asked and got permission from qlipoth to publish an enhanced version of his code.

Introduction

My company has a need for reading barcodes from images that also contain text on the same page as the barcode(s). When searching for a solution in C#, I found qlipoth's CodeProject article: Reading Barcodes from an Image - II. His code came a long way towards what I wanted, but I needed a few improvements. In the end, my changes amounted to a significant rewrite that justifies a new CodeProject publication.

Background

This code has several improvements compared to the original code written in 2005:

  1. Validation is added for Code39 barcodes. The code has much less chance of detecting a "barcode" that is not actually present.
  2. Detection speed is improved. The main problem in the original project was that it used Bitmap.RotateFlip, and that function is quite slow.
  3. Added EAN/UPC and Code128 barcode support. The original project could only detect Code39 barcodes.
  4. Fixed some bugs that were reported for qlipoth's project and for the related project Reading Barcodes from an Image published by Benjamin Liedblad in 2004.

Using the code

To open the solution, you'll need Visual Studio 2008. However, I have not used any constructs that are not .NET 1.1 compatible, so the actual code should also be compilable in Visual Studio 2003 or 2005.

To use the code, call FullScanPage or ScanPage with the Windows Bitmap object containing your image. The included TestApp (written in VB.NET) demonstrates the basic use:

VB
Dim barcodes As New System.Collections.ArrayList
BarcodeImaging.FullScanPage(barcodes, Me.PictureBox1.Image, 100)
If barcodes.Count > 0 Then
  ' Found one or more barcodes ...

The numscans parameter in both functions indicates how many bands of pixels should be scanned across or down the image. With a higher number, the code finds more barcodes, but of course, will also run slightly slower. Usually 50 - 100 scans will be OK to find all barcodes on a full page image.

Difference between FullScanPage and ScanPage

FullScanPage always scans horizontally and vertically, and attempts to detect all supported barcode types. If you know that your input material is always scanned in the same orientation, or if you do not need detection of all barcode types, you can make your program run faster by using ScanPage.

C#
public static void ScanPage(ref System.Collections.ArrayList CodesRead, 
       Bitmap bmp, int numscans, ScanDirection direction, BarcodeType types)

With the additional parameters direction and types, you can fine-tune barcode detection:

  • direction: can be ScanDirection.Vertical or ScanDirection.Horizontal. Use Vertical for detecting barcodes with vertical bars, or Horizontal if you expect barcodes that are rotated 90 degrees.
  • types: Pass BarcodeType.All to detect all supported barcode types, or you can specify one or more specific barcode types, like this:
    C#
    ScanPage(ref CodesRead, bmp, numscans, ScanDirection.Vertical,
             BarcodeType.Code39 | BarcodeType.Code128);

    Supported types are Code39, EAN, and Code128. The EAN reader will also detect UPC codes.

Points of interest

  • The EAN reader also reads the 2 or 5 digits supplemental barcodes often found on books and periodicals. As shown in the screenshot, a supplemental barcode will be returned as a separate barcode string starting with "S".
  • The Code128 reader has mixed code page support. The serial number in the sample image Code128test-CodeC.png starts as Code C, but the last digit is added as Code B.
  • The code uses a more advanced approach for measuring the "narrow bar width" than qlipoth's and Liedblad's versions. This should allow for better detection rates when reading EAN and Code128. In Code39, we only need to distinguish between narrow and wide bars, but the other barcode types use four different bar widths.
  • I have added the concept of "barcode zones", to allow detection of differently scaled barcodes on the same scan line. In Code128test-CodeC.png, this enables the software to read both the serial number and the UPC code that are printed side-by-side.

    barcode zones

References

I used the following references while writing this project:

  1. For implementing EAN detection: Wikipedia articles on the European Article Number, EAN-2, and EAN-5.
  2. For implementing Code 128: the character table and checksum calculation as published on code128barcodes.com, and the barcode generator published by IDAutomation.com Inc., for testing.

History

  • October 5, 2009: Original.
  • October 19, 2009: Improved Code39 detection, bugfixes. Added VB.NET translation of the barcode detection class and the COM interface provided by Alessandro Gubbiotti.

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) Decos Software Engineering BV
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGood code but doesn't work for some images Pin
Member 1104091131-Oct-22 8:45
Member 1104091131-Oct-22 8:45 
BugIn rare situations a B&W (Format1bppIndexed) image will not be scanned properly Pin
Tony Khorouzan18-Oct-22 5:35
Tony Khorouzan18-Oct-22 5:35 
BugDetection can be impacted by overflow in rare situations Pin
Tony Khorouzan18-Oct-22 5:07
Tony Khorouzan18-Oct-22 5:07 
QuestionParseEAN does not support patterns ending with 3 characters Pin
Tony Khorouzan12-Sep-22 12:02
Tony Khorouzan12-Sep-22 12:02 
QuestionFindBarcodeZones - ending pixel of first bar code can be off Pin
Tony Khorouzan12-Sep-22 11:26
Tony Khorouzan12-Sep-22 11:26 
QuestionDon't works Pin
Telerin16-Feb-20 10:01
Telerin16-Feb-20 10:01 
QuestionRead interleaved 2 of 5 ? Pin
Member 1449671613-Jun-19 8:41
Member 1449671613-Jun-19 8:41 
QuestionHow to read barcode value from pdf file using c# ?? Pin
Member 1185984522-Apr-19 20:40
Member 1185984522-Apr-19 20:40 
Questionuse dll with autoit Pin
Member 141444118-Feb-19 8:14
Member 141444118-Feb-19 8:14 
QuestionUnable to read the barcode Pin
RaviMehrotra30-Nov-17 17:21
RaviMehrotra30-Nov-17 17:21 
The solution unable to read the barcodes from 99 percent images Frown | :(
We are using barcode39.
Share your support ID to provide you some sample images.
QuestionQR Support Pin
maher khalil26-Jul-17 5:20
maher khalil26-Jul-17 5:20 
BugBug fixes Pin
mrodby14-Jun-17 19:42
mrodby14-Jun-17 19:42 
Praiseawesome.. Pin
Hannes_Malan3-Mar-17 0:53
professionalHannes_Malan3-Mar-17 0:53 
Question"BarcodeDetection" Pin
Member 1272378029-Sep-16 7:40
Member 1272378029-Sep-16 7:40 
QuestionDint work for me Pin
aniljain508-Sep-16 0:28
aniljain508-Sep-16 0:28 
BugBug with Code128 in case checksum {99, 100, 101} Pin
Михаил Шабалкин1-Sep-16 18:49
Михаил Шабалкин1-Sep-16 18:49 
GeneralRe: Bug with Code128 in case checksum {99, 100, 101} Pin
gneotel27-Sep-16 2:50
gneotel27-Sep-16 2:50 
Questioninterleaved 25 Pin
AndersonDeA2-Jun-16 5:07
AndersonDeA2-Jun-16 5:07 
QuestionHow to get coordinate of detected barcodes Pin
cemes27-Jan-16 14:27
cemes27-Jan-16 14:27 
QuestionWhat about EAN8? Pin
Member 1040122530-Nov-15 3:18
Member 1040122530-Nov-15 3:18 
QuestionHow to read Barcode images in asp.net application Pin
MChandraKanth25-Nov-15 18:31
MChandraKanth25-Nov-15 18:31 
GeneralNot Working Pin
polattt1-Oct-15 3:58
polattt1-Oct-15 3:58 
QuestionHello Pin
Member 1178984522-Jul-15 6:56
Member 1178984522-Jul-15 6:56 
GeneralMy vote of 5 Pin
Antonio Petricca20-Jul-15 23:21
Antonio Petricca20-Jul-15 23:21 
Questionbarcode scanning Pin
willie_li9-Jun-15 16:55
willie_li9-Jun-15 16:55 

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.