Click here to Skip to main content
6,595,444 members and growing! (21,749 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Barcodes     Intermediate License: The Code Project Open License (CPOL)

Drawing a Postnet barcode

By Iven Xu

Drawing a Postnet barcode based on Neil Van Eps' and Rainman_63's articles
C++, Windows, Visual Studio, MFC, Dev
Posted:8 Feb 2006
Views:21,891
Bookmarked:12 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
3 votes for this article.
Popularity: 1.99 Rating: 4.17 out of 5

1

2

3
1 vote, 33.3%
4
2 votes, 66.7%
5
Sample Image - Postnet.gif

Introduction

This article is based on Neil Van Eps' series of articles that show how to draw barcodes. In this article, the code will draw the 1D barcode of Postnet.

Postnet Basics

PostNet was developed by the United States Postal Service (USPS) to allow faster sorting and routing of mail. Postnet is the familiar, funny-looking barcode often printed on envelopes and business return mail.

Encode Table

This table indicates how to encode each digit of a PostNet barcode. Note that the encoding is expressed as "0" (half bar) or "1" (full bar).

ASCII CHARACTER BARCODE ENCODING
0 11000
1 00011
2 00101
3 00110
4 01001
5 01010
6 01100
7 10001
8 10010
9 10100

Structure of Postnet Barcode

A PostNet barcode has the following structure:

  1. Frame bar, encoded as a single 1
  2. 5, 9, or 11 data characters properly encoded (see encoding table below)
  3. Check digit, encoded using encoding table below
  4. Final frame bar, encoded as a single 1

Computing the Checksum Digit

PostNet barcodes always include a modulo 10 check digit.

Before a PostNet symbol may be encoded, a checksum digit must be calculated to be subsequently appended to the end of the barcode. The checksum digit is a simple modulo 10 calculation of the sum of all the digits that are being coded.

The steps for calculating the check digit are as follows:

  1. Sum the total of all individual digits being encoded. For example, 80122-1905 would be calculated as 8 + 0 + 1 + 2 + 2 + 1 + 9 + 0 + 5 = 28.
  2. Determine the value that, when added to the sum, produces a value evenly divisible by 10. For example, 28 + 2 = 30, so the value is 2.
  3. The check digit is the value calculated in step 2. In this case the check digit is 2.

CEAN13 Class

My CEAN13 is based on the CBarcode class, that has been introduced in Neil Van Eps' series of articles.

Here is the CPostnet class declaration. It's implemented to draw a Postnetbarcode.

class CRationalCodabar : public CBarcode  
{
public:
 void DrawBitmap();
 void BitmapToClipboard();
 CRationalCodabar();
 virtual ~CRationalCodabar();

private:
 CString RetrievePattern(char c);
 void DrawPattern(CString csPattern);
};

CPostnet::DrawBitmap() Details

void CPostnet::DrawPattern( CString csPattern )
{
 int   i,nXPixel,nYPixel;
 CDC   oDC;

 // attach to the device context
 oDC.Attach(m_hDC);

 // initialize X pixel value
 nXPixel = m_nStartingXPixel;
 
 for (i=0;i<csPattern.GetLength();i++)
 {  // X value for loop
  for (nXPixel=m_nStartingXPixel;nXPixel<m_nStartingXPixel+m_nNarrowBarPixelWidth;
       nXPixel++)
  {   
    // if this is a bar
   if (csPattern.GetAt(i)=='b')
   {
    for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)
    {
     oDC.SetPixelV(nXPixel,nYPixel,COLORBLACK);
    }
   }
   else
   {
    for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight/2;nYPixel++)
    {
     oDC.SetPixelV(nXPixel,nYPixel,COLORBLACK);
    }
   }

   for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)
   {
    oDC.SetPixelV(nXPixel+m_nNarrowBarPixelWidth,nYPixel,COLORWHITE);
   }
  }

  // advance the starting position
  m_nStartingXPixel+= 2*m_nNarrowBarPixelWidth;
 }

 // detach from the device context
 oDC.Detach();
 
 return;
}

Other Resources

Here are some resources about barcodes:

History

  • 9th February, 2006: Initial post

License

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

About the Author

Iven Xu


Member
My home page: http://www.xtrafinal.com
Occupation: Web Developer
Location: China China

Other popular General Graphics articles:

  • A flexible charting library for .NET
    Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
  • CxImage
    CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
  • 3D Pie Chart
    A class library for drawing 3D pie charts.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • ImageStone
    An article on a library for image manipulation.
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
General[Message Deleted] Pinmembercabbage_kongxin20:17 10 Feb '06  
GeneralRe: Oh my god PinmemberIven Xu2:07 11 Feb '06  
GeneralRe: Oh my god PinmemberRavi Bhavnani7:53 11 Feb '06  
QuestionDo you happen to know... PinmemberPandele Florin23:07 8 Feb '06  
AnswerRe: Do you happen to know... PinmemberIven Xu2:28 11 Feb '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 Feb 2006
Editor: Deeksha Shenoy
Copyright 2006 by Iven Xu
Everything else Copyright © CodeProject, 1999-2009
Web15 | Advertise on the Code Project