Click here to Skip to main content
15,867,851 members
Articles / Programming Languages / C#

Scan and Read the Barcode from PDF File

Rate me:
Please Sign up or sign in to vote.
4.89/5 (30 votes)
5 Aug 2010CPOL4 min read 342.4K   30.1K   149   63
Scan the PDF file and recognize if it contains the Barcode or not

Acknowledgment

The project is about Barcode recognition. The project contains decode / encode the EAN-13, EAN-8, and QR code barcode. I have also added the functionality to scan the PDF file and the software is smart enough to recognize whether PDF contains any barcode or not.

Detect EAN -13, EAN – 8 and QR code from the PDF file.

Image 1

Introduction

We need a functionality that can read the PDF file and recognize the EAN-13 and QR-Code from the file. On top of that, I needed decoding and encoding algorithm of EAN-13 and QR code. I saw some solution on the internet about Barcode Recognition, but they were running somehow a little bit slowly, so I decided to optimize them.

EAN-13

EAN-13, based upon the UPC-A standard, was implemented by the International Article Numbering Association (EAN) in <place w:st="on">Europe.

A typical EAN-13 bar code looks something like this:

Image 2

Computing the Checksum Digit

The steps for calculating the check digit are as follows:

  1. Consider the right-most digit of the message to be in an "odd" position, and assign odd/even to each character moving from right to left.
  2. Sum the digits in all odd positions, and multiply the result by 3.
  3. Sum the digits in all even positions.
  4. Sum the totals calculated in steps 2 and 3.
  5. The check digit is the number which, when added to the totals calculated in step 4, result in a number evenly divisible by 10.
  6. If the sum calculated in step 4 is evenly divisible by 10, the check digit is "0" (not 10).

QR Code

A QR Code is a matrix barcode (or two-dimensional code), readable by QR scanners, mobile phones with camera, and smart phones. The code consists of black modules arranged in a square pattern on white background. The information encoded can be text, URL or other data.

Understand the components of a QR code

Image 3

Usage of QR Code

QR codes can store quite complex information in a small matrix. As awareness grows about how useful they are, we can expect to see them in more public venues. Commercial packaging will display codes with detailed nutritional information or links to web­sites where users can play the latest product-associated game or register for updates or coupons. In academic uses, QR codes on student tests could help ensure anonymity in grading. Posted next to artwork or in musical or theatrical programs, QR codes might lead students to open forums where they could join in community discussions about what they've heard or seen. In scientific endeavours, QR codes could take the place of printed labels; attached to lab work, samples, or medication options, they could preserve confidentiality of participant names.

Using the Code

To open the solution, you'll need Visual Studio 2008.

  1. EAN -13

    Image 4

    To generate the image of EAN-13 barcode, you got to click on the “Encode” button. The attached solution contain “BarcodeImaging” project which has the function to draw an image of the entered barcode number.

    C#
    //===== Encoding performed here =====
    
    barcode.Image = b.Encode(type, this.txtData.Text.Trim(), W, H);
    
    //===================================

    To decode the EAN -13, go to “EAN-13 Decode” tab where you can select the image file and once you click on Decode button, the software will return the barcode number if image contains a valid barcode.

    C#
    //===== Scanning of image performed here =====
    
    BarcodeImaging.FullScanPage(ref barcodes, (Bitmap)this.pictureBox1.Image, iScans);
    
    //===================================
  2. QR Code

    Image 5

    Enter a data that you want to store into QR code. You can set the encoding, correction level, version and size of the QR Code. Once you click on the Encode button image will be generated.

    The attached solution contains the “QRCodelib” which has a function of encoding and decoding of the QR Code.

    C#
    //===== Encoding performed here =====
    
    String data = txtEncodeData.Text;
    
    image = qrCodeEncoder.Encode(data);
    
    //===================================

    To decode the QR Code:

    C#
    //===== Decoding performed here =====
    
    String decodedString = decoder.decode
    	(new QRCodeBitmapImage(new Bitmap(picDecode.Image)));
    
    //===================================
  3. PDF Scan

    The most interesting part of this article is to scan the PDF and recognize the barcode. I used “PdfToImage” ghost script to generate the images of all the PDF pages. You got to select the PDF file and click on the Scan button, the application will scan the PDF.

    Image 6

Points of Interest

A person who needs barcode recognition from files can use this project. Apart from existing implemented functionality, I would like some enhancements in the code which are mentioned below:

  • Support more barcode symbology
  • QR code decoder is running a little bit slow, need optimization on algorithm
  • Need more file type support, currently project only supports PDF file
  • Need to create EAN-13 code on the basis of country, manufacturing, and product code

References

History

This is the first version of my article, I will be posting the second version with enhancements soon.

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)
United States United States
Piyush has valuable experience in requirements gathering, designing, implementing, and maintaining data-driven, object-oriented and service based enterprise systems. In addition to his technical expertise, Piyush also published paper on knowledge management and he has excellent communication skills to cooperate with the clients.

He holds a Masters Degree in Computer Science from the University Of Michigan, USA

Comments and Discussions

 
GeneralRe: gsdll32.dll error Pin
Piyush Patell27-Aug-10 2:25
Piyush Patell27-Aug-10 2:25 
GeneralRe: gsdll32.dll error Pin
eyale2-Sep-10 5:01
eyale2-Sep-10 5:01 
GeneralRe: gsdll32.dll error Pin
Piyush Patell3-Oct-10 23:53
Piyush Patell3-Oct-10 23:53 
GeneralRe: gsdll32.dll error Pin
priya rishi17-Feb-11 18:28
priya rishi17-Feb-11 18:28 
GeneralRe: gsdll32.dll error Pin
Piyush Patell17-Feb-11 18:44
Piyush Patell17-Feb-11 18:44 
GeneralRe: gsdll32.dll error Pin
GPUToaster™13-Apr-11 23:53
GPUToaster™13-Apr-11 23:53 
GeneralRe: gsdll32.dll error Pin
Onofrio Carmine22-Nov-21 3:57
Onofrio Carmine22-Nov-21 3:57 
GeneralJust a minor "correction" Pin
Kenneth Nilsen10-Aug-10 3:42
Kenneth Nilsen10-Aug-10 3:42 
Hi withpiyush and thanks for a nice article.

Just a minor "correction" of the break-down on the EAN13-code that could become important if one has a larger database of codes in it:

The 2 first digits are representing a country code.
The next 4 are the producer.
The next 6 are the product code of that producer.
and as you describe, the last digit is the checksum of the 12 first digits.

Best wishes.
--
Kenneth Chr. Nilsen
Optisoft.no

GeneralRe: Just a minor "correction" Pin
Piyush Patell11-Aug-10 4:17
Piyush Patell11-Aug-10 4:17 
Generalneed to generate only images that contains Barcode Pin
Piyush Patell5-Aug-10 4:32
Piyush Patell5-Aug-10 4:32 

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.