Our Customers
Contact Us Email: support@barcodelib.com
Home > Java Barcode Generator > Java Barcode Generation Guide > Java Code-128 Barcode Generator
Download Barcode for Java Trial

Java Code-128 Barcodes Generator Guide

Code-128 Bar Code Generation Guide in Java class, J2EE, Jasper Reports, iReport & Eclipse BIRT
  • Easily generate Code-128 barcodes in Java applications
  • Compatible the latest Code-128 ISO specification [ISO/IEC 15417 (Second edition 2007-06-01)]
  • Generate high quality Code-128 images in JPEG, GIF & PNG formats
  • 100% build in Java, compatible with JDK 1.4 and later version
  • Generate Code-128 barcodes on any platforms with a Java virtual machine
  • Generate Code-128 in Jasper Reports & iReport
  • Generate Code-128 in Eclipse BIRT
  • Mature Java Barcode Generator library since 2003
  • Royalty-free and perpetual developer license
Java Code-128 Introduction
Code-128 is also known as ANSI/AIM 128, ANSI/AIM Code 128, USS Code 128, Uniform Symbology Specification Code 128, Code 128 Code Set A, Code 128 Code Set B, Code 128 Code Set C, Code 128A, Code 128B, Code 128C.
Compatibility: Barcode for Java library is compatible with the latest Code-128 ISO specification [ISO/IEC 15417 (Second edition 2007-06-01)].
Code 128 is a very effective, high-density symbology which permits the encoding of alphanumeric data. The symbology includes a checksum digit for verification, and the bar code may also be verified character-by-character verifying the parity of each data byte. This symbology has been widely implemented in many applications where a relatively large amount of data must be encoded in a relatively small amount of space. It's specific structure also allows numeric data to be encoded at, effectively, double-density.
Code 128 Code Sets
  • Code Set A (or Chars Set A) includes all of the standard upper case U.S. alphanumeric keyboard characters and punctuation characters together with the control characters, (i.e. characters with ASCII values from 0 to 95 inclusive), and seven special characters.
  • Code Set B (or Chars Set B) includes all of the standard upper case alphanumeric keyboard characters and punctuation characters together with the lower case alphabetic characters (i.e. characters with ASCII values from 32 to 127 inclusive), and seven special characters.
  • Code Set C (or Chars Set C) includes the set of 100 digit pairs from 00 to 99 inclusive, as well as three special characters. This allows numeric data to be encoded as two data digits per symbol character, at effectively twice the density of standard data.
Code 128 Special characters
The last seven characters of Code Sets A and B (character values 96 - 102) and the last three characters of Code Set C (character values 100 - 102) are special non-data characters with no ASCII character equivalents, which have particular significance to the bar code reading device.

Sample of a Code 128 Barcode
Java Code-128 Encoding Data Scope
Code 128 Barcode for Java supports:
  • all 128 characters of ASCII.
  • values 128-255 in accordance with ISO 8859-1. There are referred to as extended ASCII.
How to Generate Code-128 Barcode in Java Class?
The following Java code illustrates how to create a Code-128 barcode, and output to various formats.
1     Linear linear = new Linear(); 
2 linear.setType(Linear.CODE128);
3 linear.setData("Code-128");
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 Code-128 (line 2)
  • Set Code-128 barcode data to encode (line 3)
  • Encode generated Code-128 barcode into gif image file (line 4)
  • Generate Code-128 and output to BufferedImage object (line 5)
  • Generate Code-128 in bitmap and output to byte[] (line 6)
  • Generate Code-128 and draw to defined area (Rectangle2D) on Graphics2D (line 7-9)
  • Generate Code-128 and output to OutputStream (line 10-11)
How to Generate Barcode Code-128 Image in Java Web Applications?
There are two methods to create Code-128 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:
    • 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=123456789&Type=CODE128" />
      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.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 Code 128 Image in Jasper Reports, iReport, Eclipse BIRT?
Java Code-128 Property Settings
  1. Set the type property to Linear.CODE128, Linear.CODE128A, or Linear.CODE128B, or Linear.CODE128C.
    Servlet URL Parameter: "Type". Value: 22 (Code128), 23 (Code128 A), 24 (Code128 B), 25 (Code128 C). Sample: &Type=22.
    • Linear.CODE128 (default):Barcode Library will automatically switch between code sets to encode the ASCII values.
    • Linear.CODE128A: Library will use the Char Set A which only supports ASCII values from 0 to 95.
    • Linear.CODE128B: Library will use the Char Set B which only supports ASCII values from 32 to 127.
    • Linear.CODE128C: Library will use the Char Set C which only supports pairs of digits.
  2. Set the data property with the value to encode.
    Type is String.
    Servlet URL Parameter: Data.
    • Valid Data Scope for Code 128 Auto (BarcodeType.CODE128):
      • all 128 characters of ASCII (ASCII values from 0 to 127)
    • Valid Data Scope for Code 128 Set A (BarcodeType.CODE128A):
      • supports ASCII values from 0 to 95
    • Valid Data Scope for Code 128 Set B (BarcodeType.CODE128B):
      • supports ASCII values from 32 to 127
    • Valid Data Scope for Code 128 Set C (BarcodeType.CODE128C):
      • supports pairs of digits
    • Sample: "123456789012".
  3. addCheckSum property is not applied here. Barcode Library will always add a check character between encoded data and stop character.
  4. Set the processTilde property to true, if you want use the tilde character "~" to specify special characters in the encoding data. Default is false.
    Servlet Parameter: "ProcessTilde". Values: "t" (true), "f" (false). Sample: &ProcessTilde=t.
    Format of the tilde:
    • ~NNN: is used to represent the ASCII character with the value of NNN. NNN is from 000 - 255.
  5. 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.
  6. 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
  7. 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 Barcode Code-128 Image Size?

All Java Barcode Types/Symbologies

Encode Linear Barcodes:
Encode Matrix Barcodes: