Our Customers
Contact Us Email: support@barcodelib.com
Home > Java Barcode Generator > Java Barcode Generation Guide > Java USPS OneCode 4-State Customer Barcode Generator
Download Barcode for Java Trial

Java OneCode Barcodes Generator Guide

USPS Intelligent Mail (OneCode) Bar Code Generation Guide in Java class, J2EE, Jasper Reports, iReport & Eclipse BIRT
  • Easily generate OneCode barcodes in Java applications
  • Generate high quality OneCode images in JPEG, GIF & PNG formats
  • 100% build in Java, compatible with JDK 1.4 and later version
  • Generate OneCode barcodes on any applications and platforms with Java virtual machine installed
  • Generate OneCode in Jasper Reports & iReport
  • Generate OneCode in Eclipse BIRT
  • Professional Java Barcode Generator library which enjoys customer appreciation for more than 10 years
  • Royalty-free developer license, one time purchase for lifetime use
OneCode Barcode Introduction
OneCode is also known as OneCode 4CB, USPS 4CB, 4-CB, 4-State Customer Barcode, USPS OneCode Solution Barcode.
OneCode 4-State Customer Barcode (4-CB) is the next generation in the evolution of USPS barcode technology. It does not replace existing barcodes - it offers a more effective alternative. Partnering with the Mailing Industry, the Postal Service developed the 4-CB which increases the amount of information carried on letter and flats mail pieces and allows for expanded tracking capability, creating greater visibility into the mailstream.
4-CB is a height-modulated barcode using four distinct, vertical bar types. It encodes a 31-digit string into 65 vertical bars each representing one of four possible states: full bar, ascender, tracker, and descender.

4-State Barcode Bars
4-CB carries a payload 31 digits comprised of the following elements.

Type Field Digits
Tracking Code Barcode Identifier 2 (2nd digit must be 0-4)
Service Type Identifier 3
Mailer Identifier 6 or 9
Serial Number 9 or 6
If Mailer Identifier is 6, Serial Number must be 9.
If Mailer Identifier is 9, Serial Number must be 6.
Routing Code Delivery Point ZIP Code 0, 5, 9, or 11
Table 1 - USPS OneCode SOLUTION Barcode Data Fields
For each of the fields in Table 1, except the Routing Code, leading or trailing zeros shall be provided to achieve the correct size. Spaces and nulls are not acceptable characters for padding. For ZIP Codes, which have one of four lengths, encoding shall provide padding beyond the digits to be encoded. No error detection and correction, such as a checksum with POSTNET or PLANET, shall be included as part of the data fields. The OneCode Barcode Data Fields are described as follows:
  • The Barcode Identifier is assigned by USPS for the type of tracking service. This is two digits, with the second digit in the range of 0-4.
  • The Service Type Identifier is assigned by USPS for any special services on the mailpiece. The allowable encoding range is 000-999.
  • The Mailer Identifier is assigned by USPS as a unique identifier for each OneCode SOLUTION customer. The allowable encoding range is 000000-99999.
  • The Serial Number is assigned by the mailer for tracking mailpieces. The allowable encoding range is 000000000-999999999.
  • The Delivery Point ZIP Code is assigned by the mailer for routing the mailpiece. This replaces POSTNET for routing the mailpiece to its final delivery point. The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges are no ZIP Code, 00000-99999, 000000000-999999999, and 00000000000-99999999999.
OneCode Encoding Data Scope
Onecode Barcode for Java supports:
  • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
How to Generate USPS OneCode Barcode in Java Class?
The following Java code illustrates how to create a OneCode barcode, and output to various formats.
1     Linear linear = new Linear(); 
2 linear.setType(Linear.ONECODE);
3 linear.setData("34160265194042788110");
4 linear.renderBarcode("c:/barcode.gif");

5 BufferedImage bufferedImage = linear.renderBarcode();

6 byte[] barcodeBytes = linear.renderBarcodeToBytes();

7 Graphics2D g = ...
8 Rectangle2D rectangle = ...
9 linear.renderBarcode(g, rectangle);

10 OutputStream outputStream = ...
11 linear.renderBarcode(outputStream);
  • Create a Linear barcode object (line 1)
  • Set Linear object barcode type to OneCode (line 2)
  • Set OneCode barcode data to encode (line 3)
  • Encode generated OneCode barcode into gif image file (line 4)
  • Generate OneCode and output to BufferedImage object (line 5)
  • Generate OneCode in bitmap and output to byte[] (line 6)
  • Generate OneCode and draw to defined area (Rectangle2D) on Graphics2D (line 7-9)
  • Generate OneCode and output to OutputStream (line 10-11)
How to Generate OneCode Barcode Image in Java Web Applications?
There are two methods to create OneCode barcode images in your Java web applications.
  1. The simplest way is to stream barcode image using our buildin barcode servlet application.
    • Download the Barcode for Java Trial Package, and unzip it.
    • Under java_barcode_trial package, copy barcode folder to your java servlet container like tomcat.
    • Restart tomcat. Now you have installed successfully.
    • To test your installation, open your web browser and navigate to:
      http://YourDomain:Port/barcode/linear?Data=34160265194042788110&Type=ONECODE
    • To create barcode image in your JSP or html page, you need pass the url to IMG tag src value. For example,
      <img src="http://YourDomain:Port/barcode/linear?Data=34160265194042788110&Type=ONECODE" />
      Using this method, it will not generate any barcode images in your server side.
  2. The second method is to generate barcode images in your server side.
    • Create a barcode image in server side, like
      Linear linear = new Linear();
      linear.setData("34160265194042788110");
      linear.setType(Linear.ONECODE);
      linear.renderBarcode("C:/Tools/Tomcat 5.5/webapps/YourWebApp/barcode-images/barcode.gif");
    • In your JSP or HTML page, you need use IMG tag to display generated image, like
      <img src="http://YourDomain:Port/YourWebApp/barcode-images/barcode.gif" />
How to Generate Barcode USPS OneCode Image in Jasper Reports, iReport, Eclipse BIRT?
OneCode Barcode Property Settings
  1. Set the type property to Linear.ONECODE
    Servlet URL Parameter: "Type". Value: 36. Sample: &Type=36.
  2. Set the data property with the value to encode. Type is String.
    Servlet URL Parameter: Data.
    • Valid Data Scope:
      • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    • Valid Data Format:
      • The valid data length should be 20, 25, 29, or 31 digits.
    • Sample: "0123456709498765432101234567891".
  3. addCheckSum property is not applied.
  4. Barcode image size settings.
    • Set property uom (Unit of Measure) for properties X, Y, leftMargin, rightMargin, topMargin and bottomMargin.
      Default is Linear.UOM_PIXEL (0). Valid values are Linear.UOM_PIXEL (0), Linear.UOM_CM (1), Linear.UOM_Inch (2).
      Servlet URL Parameter: "UOM". Value: 0 (pixel), 1 (cm), 2 (inch). Sample: &UOM=0.
    • Set the X (for barcode module width) and Y (for barcode module height) properties.
      Both types are float. Default X is 3. Y is 75.
      Servlet URL Parameter: "X", "Y". Sample: &X=3&Y=75.
    • Set the leftMargin, rightMargin, topMargin and bottomMargin properties, and types are all float.
      Default values are 0 for all 4 margin settings.
      Servlet URL Parameter: "LeftMargin", "RightMargin", "TopMargin", "BottomMargin". Sample: &LeftMargin=0.
    • Set the resolution property (Value is expressed in DPI - Dots per inch).
      Default is 72 dpi.
      Servlet URL Parameter: "Resolution". Sample: &Resolution=72.
  5. Setting up text style in barcode image.
    • Set the showText properties. If this value is true, barcode data will be displayed with the barcode.
      Default is true.
      Servlet URL Parameter: "ShowText". Value: "t" (true), "f" (false). Sample: &ShowText=t.
    • Set the textFont property. The font used to display text in barcode image.
      Default is new Font("Arial", Font.PLAIN, 11).
      Servlet URL Parameter: "TextFont". Value format: [font name]|[font style]|[font size]. Sample Values: &TextFont=Arial|Bold|12
  6. With rotate property, you can display barcode horizontally or vertically.
    Default value is 0.
    Servlet URL Parameter: "Rotate". Sample: &Rotate=0.
    Value can be:
    • 0 (Linear.ANGLE_0),
    • 1 (Linear.ANGLE_90),
    • 2 (Linear.ANGLE_180),
    • 3 (Linear.ANGLE_270).
How to Set Generated OneCode Barcode Image Size?

All Java Barcode Types/Symbologies

Encode Linear Barcodes:
Encode Matrix Barcodes: