Click here to Skip to main content
14,173,097 members
Click here to Skip to main content
Add your own
alternative version

Stats

3.5K views
324 downloads
11 bookmarked
Posted 2 May 2019
Licenced CPOL

PDF417 Barcode Decoder .NET Class Library and Two Demo Apps

, 2 May 2019
Rate this:
Please Sign up or sign in to vote.
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.

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

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

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

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

/// <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)

Share

About the Author

Uzi Granot
Canada Canada
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionThanks for the library...but Pin
Keith Vinson3-May-19 12:57
memberKeith Vinson3-May-19 12:57 
AnswerRe: Thanks for the library...but Pin
Uzi Granot3-May-19 13:02
memberUzi Granot3-May-19 13:02 
GeneralRe: Thanks for the library...but Pin
Keith Vinson3-May-19 13:14
memberKeith Vinson3-May-19 13:14 
GeneralRe: Thanks for the library...but Pin
Uzi Granot3-May-19 13:19
memberUzi Granot3-May-19 13:19 
GeneralRe: Thanks for the library...but Pin
Keith Vinson3-May-19 13:22
memberKeith Vinson3-May-19 13:22 
GeneralRe: Thanks for the library...but Pin
Keith Vinson3-May-19 13:25
memberKeith Vinson3-May-19 13:25 
GeneralRe: Thanks for the library...but Pin
Uzi Granot3-May-19 13:32
memberUzi Granot3-May-19 13:32 
GeneralRe: Thanks for the library...but Pin
Uzi Granot3-May-19 13:57
memberUzi Granot3-May-19 13:57 
GeneralRe: Thanks for the library...but Pin
Uzi Granot3-May-19 14:28
memberUzi Granot3-May-19 14:28 
GeneralRe: Thanks for the library...but Pin
Uzi Granot3-May-19 14:35
memberUzi Granot3-May-19 14:35 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190524.3 | Last Updated 2 May 2019
Article Copyright 2019 by Uzi Granot
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid