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

Reading Barcodes from an Image - III

, 19 Oct 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Detects Code39, EAN, and Code128 barcodes in an image.



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.


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.


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:

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.

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:
    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


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, and the barcode generator published by Inc., for testing.


  • 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.


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


About the Author

Berend Engelbrecht
Software Developer (Senior) Decos Software Engineering BV
Netherlands Netherlands
No Biography provided

Comments and Discussions

QuestionBarcodeImaging.FullScanPage PinmemberMember 1082270626-Feb-15 21:16 
QuestionDifferences between Me.Picture1.Image (vb) and this.Picture1.Image (c #) PinmemberNino10-Feb-15 11:54 
QuestionBarcode recognition algorithms PinmemberMember 1093923126-Aug-14 22:03 
QuestionGreat Scanning Methods PinmemberAdeleB24-Aug-14 17:43 
GeneralMy vote of 1 PinmemberMember 107885585-May-14 5:33 
QuestionVote of 5 PinmemberKlixx6-Mar-14 2:30 
GeneralNot Workin help Pinmemberheemanshubhalla9-Feb-14 0:17 
QuestionIMEI Sticker barcode is recognized..pls help Pinmembervignesh @ vickees25-Nov-13 21:44 
Answerbarcode reading Pinmemberhenlsiew11-Oct-13 19:03 
QuestionPerfect Pinmemberseanmir24-Sep-13 2:38 
QuestionRead barcode images on Pinmembervengance8519-Aug-13 22:15 
AnswerRe: Read barcode images on PinmemberAmit Pankajkumar Shah1-Dec-13 1:28 
Generalproblem PinmemberAnkurGupta OM18-Jun-13 20:59 
QuestionBarcode reading Pinmemberchristine sarsonas18-Jun-13 18:13 
QuestionHow to use PinmemberAlexander Weis31-May-13 11:12 
QuestionCode PDF417 PinmemberSamwill17-May-13 2:53 
Questionbarcode in .C# PinmemberBarcoding House20-Jan-13 18:27 
GeneralMy vote of 5 PinmemberJohn McKay10-Jan-13 7:39 
Questioncan i have the position of each barcode has been scaned? Pinmembermammmel16-Dec-12 17:57 
QuestionScannable QR Code must have quiet zone? PinmemberSusanna Moore4-Dec-12 21:21 
I'm using .NET barcode generator control for my project. But the created QR Code cannot be scanned by this free .net qr code barcode reader eval package sometimes. So, I wonder whether it's the quiet zone issue?
Can anybody help? any suggestions?
QuestionProblem with read some codes PinmemberMember 91534644-Dec-12 8:54 
AnswerRe: Problem with read some codes PinmemberMember 91534644-Dec-12 9:31 
AnswerGreat contribution, but also some distance away from the actual use Pinmembery_sir15-Oct-12 4:20 
GeneralMy vote of 3 PinmemberBrent Lamborn8-Aug-12 11:32 
QuestionImage dpi Pinmembertaher_elhossin24-Jul-12 11:17 
QuestionBug fixes PinmemberMatthias Böhnke6-Jul-12 1:36 
BugBug in ParseCode128Pattern - Check Digit PinmemberFurryPlatypus4-Jul-12 17:48 
QuestionMessage Automatically Removed PinmemberZacbr21-Apr-12 23:09 
QuestionRead Fax-tiffs PinmemberFysicus4-Apr-12 0:23 
GeneralMy vote of 5 Pinmembermanoj kumar choubey23-Feb-12 20:23 
QuestionAbout barcodes font height Pinmembersuperdelo14-Feb-12 23:31 
QuestionNew version but again failed to read barcodes PinmemberMember 388881626-Jan-12 6:39 
QuestionNot all barcode-types recognized PinmemberMember 151751024-Jan-12 6:13 
QuestionSome Fils fails to read barcode PinmemberMember 388881618-Jan-12 0:05 
QuestionCode 39 Pinmembersteve7g12-Jan-12 17:08 
GeneralMy vote of 5 Pinmemberchenghuiwu16-Oct-11 23:35 
QuestionGreat code !!!!! Thanks Pinmembermpino13-Sep-11 23:43 
QuestionNot every barcode is recognized PinmemberJecka8-Aug-11 2:45 
AnswerRe: Not every barcode is recognized Pinmemberdongyin2-Jan-12 23:14 
QuestionPLEASE SAVE ME FROM THIS BARCODES PinmemberMember 79328884-Jul-11 22:30 
QuestionIt doesn't work PinmemberMember 799840211-Jun-11 9:34 
GeneralMy vote of 5 PinmemberNasenbaaer26-May-11 4:49 
GeneralRe: My vote of 5 Pinmemberoscarlcs30-Jul-12 10:19 
GeneralMy vote of 5 Pinmvpthatraja21-May-11 19:06 
GeneralPerformance boost PinmemberPaul Hatcher18-Mar-11 17:41 
GeneralNice work!! Pinmemberandredani27-Feb-11 23:55 
GeneralWindows Phone 7 Version Pinmemberzhi-lee.com26-Oct-10 12:30 
GeneralRe: Windows Phone 7 Version PinmemberMember 740954123-Dec-10 5:18 
GeneralRe: Windows Phone 7 Version Pinmemberzhi-lee.com16-Jan-11 1:25 
GeneralRe: Windows Phone 7 Version PinmemberBerend Engelbrecht16-Jan-11 1:58 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150327.1 | Last Updated 19 Oct 2009
Article Copyright 2009 by Berend Engelbrecht
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid