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:
- 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.
Pdf417Decoder pdf417Decoder = new Pdf417Decoder();
int BarcodesCount = pdf417Decoder.Decode(Pdf417InputImage);
for(int Count = 0; Count < BarcodesCount; Count++)
{
byte[] BinaryData = pdf417Decoder.BarcodesData[Count];
BarcodeInfo barcodeInfo = pdf417Decoder.BarcodesInfo[Count];
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:
public class BarcodeInfo
{
public byte[] BarcodeData { get; internal set; }
public string CharacterSet { get; internal set; }
public int GliCharacterSetNo { get; internal set; }
public int GliGeneralPurpose { get; internal set; }
public int GliUserDefined { get; internal set; }
public int DataColumns { get; internal set; }
public int DataRows { get; internal set; }
public int ErrorCorrectionLength { get; internal set; }
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:
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:
public static string BinaryDataToString
(
byte[] BarcodeBinaryData,
string IsoStandard = "ISO-8859-1"
)
{
try
{
Encoding ISO = Encoding.GetEncoding(IsoStandard);
byte[] Utf8Bytes = Encoding.Convert(ISO, Encoding.UTF8, BarcodeBinaryData);
char[] Utf8Chars = Encoding.UTF8.GetChars(Utf8Bytes);
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:
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.
Another example of decoding an image with two barcodes.
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.
History
- 2019/05/01: Version 1.0 - Original version