Java Barcode Code 128 Generator Introduction
Java Barcode is a Java barcode generator Java library, which generates and prints standard Code 128 and 20+ linear and 2d barcodes in Java applications.
In free evaluation version of Java Barcode library, you will find compiled Java barcode library (single Jar file) to create, print Code 128 and other linear, 2d barcodes in your Java projects, detailed tutorial for generation barcodes in Java, and complete sample Java source code to generate Code 128 in Java class. Downloaded Java barcode generation software allows you to create and print QR Code, Data Matrix, UPC, GS1-128, Code 93, PDF417, Code 39, Intelligent Mail, and other barcodes in your Java products.
In free evaluation version of Java Barcode library, you will find compiled Java barcode library (single Jar file) to create, print Code 128 and other linear, 2d barcodes in your Java projects, detailed tutorial for generation barcodes in Java, and complete sample Java source code to generate Code 128 in Java class. Downloaded Java barcode generation software allows you to create and print QR Code, Data Matrix, UPC, GS1-128, Code 93, PDF417, Code 39, Intelligent Mail, and other barcodes in your Java products.
Code-128 Introduction
Code 128 is a very high-density barcode symbology. (A special application of Code 128 named GS1-128 is used extensively world wide in shipping and packaging industries.) It is used for alphanumeric or numeric-only barcodes.
Java Code 128 Generator - Valid Data Scope
Java Code 128 Generator encodes:
- 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 in Java Class?
Using Java Barcode Generator library, you can quickly create a Code 128 barcode image in Java using
com.onbarcode.barcode.Code128 class.
The following Java source code explains how to create, print a Code 128 barcode to an image file.
- Initiate a new
Code128object - Set the code 128 barcode encoding data using method
setData() - Call method
drawBarcode()to print Code 128 barcode to a PNG image file
Code128 barcode = new Code128(); barcode.setData("Code 128"); try { barcode.drawBarcode("c://Output//OnBarcode.com//java-code-128-demo.png"); } catch (Exception e) { e.printStackTrace(); }
Print Code 128 in memory
OnBarcode Java Barcode library provides several functions to encode and print Code 128 barcodes to various Java objects. You can process these barcode objects in your Java program directly.
Create Code 128 in memory, output to Java BufferedImage object
Create Code 128 and print barcode in specified area in Java Graphics2D object
Create Code 128 and print barcode to a Stream object
Create Code 128 and print barcode to byte array
- Image file path
- Java
BufferedImageobject - Java
Graphics2Dobject with specified rectangle area - Stream object
- Byte array object
Create Code 128 in memory, output to Java BufferedImage object
java.awt.image.BufferedImage drawBarcode()
Create Code 128 and print barcode in specified area in Java Graphics2D object
void drawBarcode(java.awt.Graphics2D g, java.awt.geom.Rectangle2D rectangle)
Create Code 128 and print barcode to a Stream object
boolean drawBarcode(java.io.OutputStream outputStream)
Create Code 128 and print barcode to byte array
byte[] drawBarcodeToBytes()
Code 128 Encoding Character Set
Code 128 character set supported
Code 128 barcode supports the following data characters
To encode text in Code 128, you can call method
- All 128 Full ASCII characters defined in ISO/IEC 646
- Extended ASCII characters are also supported with Function Character (FNC4) used. (By default are values 128 - 255 of ISO/IEC 8859-1, Latin Alphabet 1)
- Special characters: 4 non-data function characters, 4 code set selection characters, 3 Start chacters and 1 Stop character
To encode text in Code 128, you can call method
setData() in Java class using Java Barcode Generator library.
Code128 barcode = new Code128(); barcode.setData("Code-128");
Code 128 Code Sets
To encode data efficiently, Code 128 standard specifies three code sets to convert Code 128 data to codewords.
- Code Sets A: including all of the standard upper case alphanumeric characters and punctuation characters together with the control characters.
- Code Sets B: including all of the standard upper case alphanumeric characters and punctuation characters together with the lower case alphabetic characters and seven special characters.
- Code Sets C: including the set of 100 digit pairs from 00 to 99 inclusive, as well as three special characters.
Auto code set using Java barcode library.
barcode.setCodeSet(Code128.SET_AUTO);
How to print, customize Code 128 barcode text label in Java?
Customize Code 128 text label font style
- Set property
TextMarginto specify the space between bar modules and text label. - Disable property
AutoResizeBarcodeTextto false to customize the text label font size manually. - Set property
TextFontto specify the text label font family name, font style, and font size.
barcode.setTextMargin(5); barcode.setAutoResizeBarcodeText(false); barcode.setTextFont(new Font("ocr-b", Font.PLAIN, 14));
Hide Code 128 text label
In Java program, you can choose to hide Code 128 barcode text label to save barcode space.
- Set property
ShowTextto false to prevent printing the Code 128 barcode text labels.
barcode.setShowText(false);
How to render Code 128 barcode with specified colors in Java?
Here we will show how to customize, print Code 128 barcode with specified colors in Java application.
- Set property
ForeColorto specify the Code 128 bar module color - Set property
BackColorto specify the Code 128 space module and background color - Set property
TextColorto specify the Code 128 text label color
barcode.setForeColor(Color.red); barcode.setBackColor(Color.white); barcode.setTextColor(Color.red);
How to print rotated Code 128 barcode image in Java?
Use property
Rotate to rotate a Code 128 barcode image. You can rotate a Code 128 barcode in 0, 90, 180, 270 degrees in Java.
barcode.setRotate(IBarcode.ROTATE_90);
How to Create Code 128 image in Java JSP or Servlet web applications?
Code 128 in JSP pages
- Under demo package, copy barcode folder and its contents to your tomcat.
- Start tomcat, navigate to http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128
- To create bar code images in html or jsp pages, you can insert a image tag (img) into your page.
- For example, <img src="http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128"/>
Code 128 in Java Servlet
OnBarcode Java Barcode Generator library supports Code 128 barcode generation in both Jakarta Servlet and Javax Servlet.
import com.onbarcode.barcode.AbstractBarcode;
import com.onbarcode.barcode.Code128;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
public class BarcodeServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
{
try {
Code128 barcode = new Code128();
barcode.setData("0123456789");
ServletOutputStream servletoutputstream = response.getOutputStream();
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Generate Code-128 barcode & output to ServletOutputStream
barcode.drawBarcode(servletoutputstream);
} catch (Exception e) {
throw new ServletException(e);
}
}
}
How to Generate & Print Barcode Code 128 Image in Java report applications?
Using OnBarcode Java Barcode Generator library, you can create, print and insert Code 128 barcodes in reports in Java reporting applications.
The barcode library supports Code 128 barcodes generation
in iReport, Jasper Reports, Eclipse BIRT reports Java projects. View detailed developer guide below:
- How to generate barcode in Jasper reports in Java application?
- How to generate barcode in iReport reports in Java application?
- How to generate barcode in BIRT reports in Java application?
How to Set Generated Barcode Code 128 Image Width and Height?
- You can set barcode image width and height through properties barcodeWidth and barcodeHeight values.
- You can set X (bar module width) and Y (bar module height) values
Java Code 128 Generator - Barcode Property Settings
Java Code 128 Generator in Java Class: com.onbarcode.barcode.Code128
View the details here: complete Code 128 barcode property settings in Java class
View the details here: complete Code 128 barcode property settings in Java class
Common Asked Questions
What is the barcode Code 128 text code?
Code 128 barcode is a high-density 1d barcode symbology, which encodes digits, letters, control characters, and full ASCII 128 characters.
Using Java barcode generator library, you can easily encode, create, customize and print Code 128 barcode images in
com.onbarcode.barcode.Code128 class in
Java class, JSP, Servlet web and J2EE applications.
What is Code 128 barcode used for?
Code 128 barcode is a common linear barcode type used in:
- Food and non-food industries
- Shipping and packing industry
- Health, public sector and government bodies
What is the check digit for code 128?
Code 128 barcode contains a mandatory check digit (checksum) character, which is based on module 103 (mod 103).
Using Java barcode library API, the generator software will automatically calculate the check character and insert into the Code 128 barcode in Java projects.
What is Code 128 barcode maximum length?
The Code 128 ISO standard does not define the maximum number of characters in a single Code 128 barcode.
However some Code 128 standards will provide the limits. GS1-128 has limits the maximum length of Code 128 data to 48 characters.
Using Java Barcode library, you will encode and generate ISO standard Code 128 and application approved Code 128 barcodes, such as GS1-128 in Java applications.
However some Code 128 standards will provide the limits. GS1-128 has limits the maximum length of Code 128 data to 48 characters.
Using Java Barcode library, you will encode and generate ISO standard Code 128 and application approved Code 128 barcodes, such as GS1-128 in Java applications.
How to scan, read Code 128 barcodes?
You can download and install OnBarcode free Code 128 barcode scanner software, or use barcode scanner device, or 3rd party apps installed on phone to read and scan the Code 128 barcodes.
OnBarcode provides Java Barcode Reader library to scan, read Code 128 and other 2d, 1d barcodes in Java application.
Also you can use the OnBarcode free Code 128 barcode scanner software to decode
Code 128 barcode images.
Is Code 128 barcode better than Code 39?
Both Code 128 and Code 39 are popular linear barcode symbologies.
Code 128 is more compact than Code 39, supporting more data to be encoded in a smaller space.
Java Barcode library allows Java developers to encoding and printing both Code 128 and Code 39 in Java, JSP, Servlet class.