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

PDF417 Barcode Decoder .NET Class Library and Two Demo Apps

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
2 May 2019CPOL7 min read 37.7K   3.9K   14   36
The PDF417 barcode decoder class library allows you to extract PDF417 barcode information from image files. The library is written in C# for the .NET framework. The two demo projects allows you to explore the library and read PDF417 barcodes.

Pdf417DecoderIcon

Introduction

The PDF417 barcode decoder class library allows you to extract PDF417 barcode information from image files. The library is written in C# for the .NET framework. It is the second article published by this author on encoding and decoding of PDF417 barcodes. The first article is PDF417 Barcode Encoder .NET Class Library and Demo App. You can use the encoder article to generate barcode image files to test the decoder. The decoder class reads an image file containing PDF417 single barcode or multiple barcodes and returns an array of results. The primary result is a byte array or multiple byte arrays. Methods are available to translate byte array to a text string based the ISO-8859 standard. Two demo/test projects are included to test the library. The Pdf417DecoderDemo reads barcode image files. The Pdf417VideoDecoder captures web camera pictures and decodes the PDF417 barcodes.

PDF417 is a two-dimensional barcode. The barcode documentation and specification can be found in the following websites. Wikipedia provides a good overview of the PDF417. Click here to access the page. The PDF417 standard can be purchased from the ISO organization at this website. An early version of the specifications is freely available to be downloaded at this website. I strongly recommend that you download this document if you want to fully understand the decoding process.

The software attached to this article is a PDF417 decoder library, and two demo/test programs. The software is written in C# for the .NET framework. This article explains the decoding process. How to integrate the decoder to your application. And how to use the demo/test programs.

PDF417 Barcode Decoding Process

An example of PDF417 barcode taken from the Wikipedia article referenced above is given below:

Pdf417BarcodeFormat

  • Read the image file and convert it into Boolean array of black and white pixels.
  • Scan the image horizontally line by line, left to right, and locate the start and stop patterns. If no start and stop signatures are found, scan the image right to left. The maximum tilt angle of the barcode relative to the image is about plus minus 30 degrees from the horizontal axis.
  • If the image contains more than one PDF417 barcode, the following steps will run for each barcode.
  • Scan the left row indicator column and the right row indicator column. The indicator columns codewords contain the number of data rows and columns, and the length of the error correction codewords.
  • Locates the top left indicator codeword, bottom left indicator codeword, top right indicator codeword and the bottom right indicator codeword.
  • Using the information from the previous two step, calculate the coefficients of projective mapping matrix. These coefficients will translate data row and column coordinates to pixels’ image coordinates. See Fundamentals of Texture Mapping and Image Warping (Page 19).
  • Extract the data codewords.
  • Run the error detection and correction algorithm.
  • Decompress the codewords into bytes.
  • If the result is successful save the result.
  • Repeat the last few steps for each barcode found.
  • Return to the calling application the number of successful barcodes detected.
  • If the returned barcode count is zero, no barcodes were detected, or none were successfully decoded. If the count is greater than zero, the results are returned in an array of barcode information, or in an array of arrays of binary bytes.
  • The returned binary data (bytes array) can be converted to text string using the global label identifier CharacterSet value.

PDF471 Decoder Project Source Code

The attached Visual Studio solution source Pdf417DecoderLibrary file includes three projects:

  • Pdf417DecoderLibrary: The decoder library consists of all the classes required to decode an image. The main class is Pdf417Decoder.
  • Pdf417DecoderDemo: The demo application allows you to test the decoder. It will decode an image file.
  • Pdf417VideoDecoder: The video decoder is a demo application that will use the first found web camera in your system to capture a PDF417 barcode image and decode it.

Integrating the PDF417 Decoder Library to Your Application

Include the Pdf417DecoderLibrary.dll class library with your project. Install the attached Pdf417EncoderLibrary.dll file in your development area. Start Visual Studio. Open your application. Go to the Solution Explorer. Right click on References and select Add Reference. Select the Browse tab and navigate your file system to the location of the installed Pdf417EncoderLibrary.dll. When your application is published, the file Pdf417EncoderLibrary.dll must be included and installed in the same folder as your executable (.exe) file.

The namespace of the library is Pdf417DercodeLibrary. Add a using statement to all your source files referring to this library.

Calling the PDF417 Decoder from Your Application

  • Create a Pdf417Decoder object. This object can be serially reused.
  • Call the Decode method with an image file name.
  • The return value is the number of detected barcodes count.
  • If the return value is zero, no barcode was found or was decoded successfully.
  • If the return value is greater than zero, iterate through all found barcodes.
  • There are three ways to access the results:
    • Read array of barcode binary data.
    • Read array of full barcode information.
    • Call BinaryDataToString method to return a text string for one binary data result.

Below, you will find an example of code sequence to be added to your application to read PDF417 barcodes.

C#
// create decoder object
Pdf417Decoder pdf417Decoder = new Pdf417Decoder();

// decode an image
int BarcodesCount = pdf417Decoder.Decode(Pdf417InputImage);

// process barcodes
for(int Count = 0; Count < BarcodesCount; Count++)
    {
    // binary result
    byte[] BinaryData = pdf417Decoder.BarcodesData[Count];

    // -or-
    // full information result
    BarcodeInfo barcodeInfo = pdf417Decoder.BarcodesInfo[Count];

    // -or-
    // text string result
    String TextData = pdf417Decoder.BinaryDataToString(Count);
    }

Full Barcode Information Result Class

The main result of decoding is the binary byte array. The library provides you with additional information about the barcode. The BarcodeInfo class is described below:

C#
/// <summary>
/// Barcode results extra information
/// </summary>
public class BarcodeInfo
{
/// <summary>
/// Barcode binary (byte array) data
/// </summary>
public byte[] BarcodeData { get; internal set; }

/// <summary>
/// Global Label Identifier character set (ISO-8859-n)
/// The n represent part number 1 to 9, 13 and 15
/// </summary>
public string CharacterSet { get; internal set; }

/// <summary>
/// Global Label Identifier character set number
/// This number is two more than the part number
/// </summary>
public int GliCharacterSetNo { get; internal set; }

/// <summary>
/// Global label identifier general purpose number
/// code word 926 value 900 to 810899
/// </summary>
public int GliGeneralPurpose { get; internal set; }

/// <summary>
/// Global label identifier user defined number
/// code word 925 value 810,900 to 811,799
/// </summary>
public int GliUserDefined { get; internal set; }

/// <summary>
/// Data columns
/// </summary>
public int DataColumns { get; internal set; }

/// <summary>
/// data rows
/// </summary>
public int DataRows { get; internal set; }

/// <summary>
/// Error correction length
/// </summary>
public int ErrorCorrectionLength { get; internal set; }

/// <summary>
/// Error correction count
/// </summary>
public int ErrorCorrectionCount { get; internal set;}
}

Binary Data to Text String

It is important to note that the barcode encodes an array of bytes not array of characters (text string). The decoding process recover the bytes from the barcode image. To encode text string, one must convert characters to bytes. The decoder must convert bytes to characters. As long as these two conversions are symmetrical encoding and decoding text will work. These conversions are not part of the PDF417 specification. The only support given to this process is the global label identifier character set. The encoder set this value as a function of ISO-8856-part. The decoder can read it and process the byte array properly.

In order for the two decoding methods given below to work, the encoder must encode text as follows:

C#
// this code if for a PDF417 encoder
// convert text string to byte array
// n is an ISO part number
Encoding ISO = Encoding.GetEncoding("ISO-8859-n");
byte[] UtfBytes = Encoding.UTF8.GetBytes(StringData);
byte[] IsoBytes = Encoding.Convert(Encoding.UTF8, ISO, UtfBytes);

The Pdf417Decoder class has two methods to convert binary data to text string.

A static method:

C#
/// <summary>
/// Convert binary data array to text string
/// </summary>
/// <param name="BarcodeBinaryData">Binary byte array</param>
/// <param name="IsoStandard">ISO standard "ISO-8859-part"</param>
/// <returns>Text string</returns>
public static string BinaryDataToString
    (
    byte[] BarcodeBinaryData,
    string IsoStandard = "ISO-8859-1"
    )
{
try
    {
    // convert byte array to string
    Encoding ISO = Encoding.GetEncoding(IsoStandard);
    byte[] Utf8Bytes = Encoding.Convert(ISO, Encoding.UTF8, BarcodeBinaryData);
    char[] Utf8Chars = Encoding.UTF8.GetChars(Utf8Bytes);
		
    // save result
    return new string(Utf8Chars);
    }

catch(Exception Ex)
    {
    return null;
    }
}

A method that convert the byte array to text string based on the global label ID character set:

C#
/// <summary>
/// Convert binary data to string for one result
/// </summary>
/// <param name="Index">Index to BarcodesInfo array</param>
/// <returns>Text string</returns>
public string BinaryDataToString
    (
    int Index
    )
{
return BinaryDataToString(BarcodesInfo[Index].BarcodeData,
    BarcodesInfo[Index].CharacterSet ?? "ISO-8859-1");
}

PDF417 Decoder Demo/Test Program

The Pdf417DecoderDemo project was designed to demonstrate and test all the features of the PDF417 Decoder Library. Click on the PDF417Image button. Select an image containing a PSD417 barcode. And watch the results. If you are running the program within the Visual Studio environment in debug mode, you can view the trace file Pdf417DecoderTrace.txt in a text editor. Scroll to the end of the file for the most recent results.

Pdf417DecoderDemo

Another example of decoding an image with two barcodes.

Pdf417DecoderDemo2Barcodes

PDF417 Video Decoder Demo/Test Program

The PDF417 Video Decoder Pdf417VideoDecoder is a test/demo application that will use the first found web camera in your system. A demo program combines PDF417 decoder and video camera image capture. The video camera software is based on the Direct Show Library.

This demo program is using some of the source modules of Camera .NET Project published at CodeProject.com and at Github project is based on DirectShowLib DirectShowLib.. Please note the DirectShowLib in this project is a modified subset of the original source modules.

Please note, I have only tested this application on my own video camera. My camera is Logitech HD Webcam C615. I am using frame size of 640 by 480 pixels.

The program sets the camera software to display the video stream in a preview area on the screen. The scanning is 5 frames per seconds. Each frame is captured and tested for a PDF417 barcode. Once a barcode is found, the result is displayed at the Decoded data text box. Decoding stops until the reset button is pressed. If the decoded data is a URI, the Go To URI button is enabled, and you can display this URI on your default web browser.

For video decoding to be successful, each barcode module must be represented by a few camera pixels. For example, 4 by 12 or more pixels. The barcode must be reasonably sharp, flat and parallel to the camera. The picture below illustrates the power of the software to transform the image to a square with the finder symbols at their correct positions.

Pdf417VideoDecoder

History

  • 2019/05/01: Version 1.0 - Original version

License

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


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThis is Awesome - Question on the Reed Solomon Pin
SpacemanScott5-Sep-23 7:59
SpacemanScott5-Sep-23 7:59 
BugNot scanning US DL barcode data scanned from scanner Pin
Rishi120-Oct-22 18:24
Rishi120-Oct-22 18:24 
QuestionIs this MIT licensed library? Pin
Member 1450390124-Jun-22 3:00
Member 1450390124-Jun-22 3:00 
QuestionIs there an easy to for this to decode PDF417 encoded strings? Pin
Member 1539147212-Oct-21 7:32
Member 1539147212-Oct-21 7:32 
AnswerRe: Is there an easy to for this to decode PDF417 encoded strings? Pin
Uzi Granot12-Oct-21 9:43
Uzi Granot12-Oct-21 9:43 
QuestionBarcodes that Fail Pin
Brett Allen26-Mar-21 12:06
Brett Allen26-Mar-21 12:06 
AnswerRe: Barcodes that Fail Pin
Brett Allen29-Mar-21 7:14
Brett Allen29-Mar-21 7:14 
I've found one more that fails to decode. This one also decodes fine in Cognex Phone App.

It has been pre-processed/cleaned up for reading; and we're attempting to use your library to read it.

Another Decode Failure - Album on Imgur[^]
QuestionHow do the Start/Stop Signatures Map? Pin
Brett Allen23-Mar-21 5:45
Brett Allen23-Mar-21 5:45 
AnswerRe: How do the Start/Stop Signatures Map? Pin
Uzi Granot23-Mar-21 7:37
Uzi Granot23-Mar-21 7:37 
GeneralRe: How do the Start/Stop Signatures Map? Pin
Brett Allen23-Mar-21 10:37
Brett Allen23-Mar-21 10:37 
QuestionHandling resolution? Pin
Brett Allen18-Mar-21 7:27
Brett Allen18-Mar-21 7:27 
AnswerRe: Handling resolution? Pin
Uzi Granot18-Mar-21 9:26
Uzi Granot18-Mar-21 9:26 
GeneralRe: Handling resolution? Pin
Brett Allen19-Mar-21 10:14
Brett Allen19-Mar-21 10:14 
GeneralMy vote of 5 Pin
Jacquers27-Dec-20 22:08
Jacquers27-Dec-20 22:08 
QuestionThis is such a neat little effort Pin
KhalidAli120-Dec-20 5:17
KhalidAli120-Dec-20 5:17 
QuestionPdf417 bug Pin
Member 110110687-Sep-20 4:43
Member 110110687-Sep-20 4:43 
AnswerRe: Pdf417 bug Pin
Uzi Granot10-Sep-20 2:25
Uzi Granot10-Sep-20 2:25 
QuestionRe: Pdf417 bug Pin
Member 1101106810-Sep-20 3:55
Member 1101106810-Sep-20 3:55 
AnswerRe: Pdf417 bug Pin
Uzi Granot10-Sep-20 4:20
Uzi Granot10-Sep-20 4:20 
QuestionBarcode error Pin
Member 110110686-Sep-20 11:18
Member 110110686-Sep-20 11:18 
AnswerRe: Barcode error Pin
Uzi Granot7-Sep-20 2:58
Uzi Granot7-Sep-20 2:58 
Question[^_^] Pin
Josedavidpc31012-Jun-20 0:32
Josedavidpc31012-Jun-20 0:32 
QuestionBetter detection of barcode? Pin
Robert W Moore10-Feb-20 17:31
Robert W Moore10-Feb-20 17:31 
AnswerRe: Better detection of barcode? Pin
Uzi Granot11-Feb-20 4:30
Uzi Granot11-Feb-20 4:30 
QuestionPdf417VideoDecoder.exe does not work Pin
Member 1193801421-Nov-19 9:07
Member 1193801421-Nov-19 9:07 

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.