Java Barcode Generator Tutorial

How to generate, create linear, 2d barcode images in Java Class, JSP Pages,Servlet, and Reports

Integration guide to generate linear, 2D barcodes using Java Barcode Generator Library

  • Easy to print, embed bulk barcode images in Jasper Report, Eclipse BIRT and iReport
  • Simple to redistribute Java barcode library with simply a jar file
  • Easy to use with no registration key or activation code required
  • Generating high quality bar code images in GIF, JPEG and Png image formats
  • Completely developed in Java, supporting JDK 1.4.2 and later versions
  • Support both Javax Servlet and Jakarta EE Servlet
  • Compatible with latest barcode symbology ISO/IEC Standards or GS1 specifications
  • Mature and reliable Barcode Generator Java Library


How to create, print 2d, 1d barcodes in Java web, desktop application?

  1. Download Java Barcode Generator library
  2. Install Java barcode generation library jar file into Java project
  3. Start to Create










Java Barcode is a Java barcode generation library which generates high quality 1D (linear) and 2D (matrix) barcodes.
OnBarcode provides complete Java barcode solutions, such as barcode generator in Java, barcode reader in Java, Java jar library for Java applications, and BIRT Barcode for Eclipse BIRT reports.


Install Java Barcode Library

Top
Java Barcode Generator is completely developed in Java programming, and it supports JDK 1.4.2 and later versions.

Compatibility

  • Java SE 25, 24, ... 6
  • J2SE 5.0
  • J2SE 1.4

Jakarta Servlet

  • /barcode-sdk/jakarta/OnBarcode.BarcodeGenerator.jar is for Jakarta Servlet.
  • Add barcode jar file to your Jakarta servlet application classpath
Using the above Java Barcode Generator library, you can create and print barcodes in a Servlet application using Jakarta Servlet (Jakarta EE).

Javax Servlet

  • /barcode-sdk/javax/OnBarcode.BarcodeGenerator.jar is for Javax Servlet
  • Add barcode jar file to your Javax servlet application classpath
Using the above barcode jar library, you can create and print barcodes in a Servlet application using Javax Servlet (Java EE).




How to Generate Barcodes in Java Class?

Top
Using OnBarcode Java Barcode Generator library, you can easily generate QR Code, Code 128, EAN/UPC and other 2d, 1d barcodes in few lines in your Java applications.

You can create and print barcode in image file or in memory
  • Image file path
  • Java BufferedImage object
  • Java Graphics2D object in specified rectangle area
  • Stream object
  • Byte array object


Create barcode image file in Java

You can quickly create and print a barcode to an image file in Java application.
  1. Create a new barcode object. Here we create a QRCode object.
  2. Set QR Code encoding text through method setData()
  3. Call method drawBarcode() to print QR Code to the target image file in Java code
QRCode barcode = new QRCode();
barcode.setData("https://www.onbarcode.com");
			
try {
	barcode.drawBarcode("c://Output//OnBarcode.com//java-qrcode-demo.png");
} catch (Exception e) {
	e.printStackTrace();
}



Create barcode in Java memory

You can also create a barcode to a Java object in memory and apply further processing on the barcode object in Java application.

Print barcode to Java BufferedImage object
java.awt.image.BufferedImage 	drawBarcode() 


Print barcode and draw barcode in specified area in Java Graphics2D object
void 	drawBarcode(java.awt.Graphics2D g, java.awt.geom.Rectangle2D rectangle) 


Create barcode and paint it to a Stream object
boolean  drawBarcode(java.io.OutputStream outputStream) 


Create and print barcode to byte array
byte[] 	drawBarcodeToBytes() 





Create barcode with complex text message encoded

You can create barcode with text message encoded through method setData(). If you need encode complex text message to barcode, such as non-printing ASCII characters, Unicode text, GS1 business text message, please view details here: How to generate barcode with ASCII text, Unicode text, GS1 data in Java application?

Unicode Text Encoding GS1 Text Encoding




How to create and adjust barcode image size in Java?

Top
When you create a barcode using Java barcode library, the barcode library will automatically calculate the barcode width and height based on the barcode option settings, such as encoding text length.

Resize 1d barcode in rectangle shape

You can also specify the printed barcode image width and height in the Java program.
Call method setBarcodeWidth() and setBarcodeHeight() to adjust the printed barcode image width and height.

Code128 barcode = new Code128();
barcode.setData("Code-128");

barcode.setBarcodeWidth(300);
barcode.setBarcodeHeight(200);
			
try {
	barcode.drawBarcode("c://Output//OnBarcode.com//java-barcode-resized.png");
} catch (Exception e) {
	e.printStackTrace();
}



Resize 2d barcode in sqaure shape

To adjust a square barcode width and height, such as QR Code, Data Matrix, you need specify the barcode image width value only, and the barcode library will draw the QR Code or Data Matrix in same width and height.

QRCode barcode = new QRCode();
barcode.setData("https://www.onbarcode.com");

barcode.setBarcodeWidth(300);
				
try {
	barcode.drawBarcode("c://Output//OnBarcode.com//java-qrcode-resized.png");
} catch (Exception e) {
	e.printStackTrace();
}





How to customize, print 1d barcode text label in Java?

Top

Adjust space between barcode and text label

You can increase the margin space between barcode modules and barcode text labe using method setTextMargin() in Java application.
Code128 barcode = new Code128();
barcode.setData("Code-128");
barcode.setTextMargin(50);
barcode.drawBarcode("c://Output//OnBarcode.com//java-code128-text-space.png");



Barcode text font style, size

Using Java barcode library, the barcode generator software will automatically calculate the barcode text font size according to the barcode width and height. If you need to customize the barcode text font style, size, you can try the following Java codes.
  • Create a EAN13 object, and set the EAN13 barcode data in 12-digit (without check digit) or 13-digit (with check digit)
  • Turn off the auto resize barcode text in method setAutoResizeBarcodeText(false)
  • Apply the new text font style in method setTextFont(new Font("ocr-b", Font.BOLD, 12))
  • Create and print the EAN13 barcode with text label customized to an image file.
EAN13 barcode = new EAN13();
barcode.setData("123456789012");
barcode.setAutoResizeBarcodeText(false);
barcode.setTextFont(new Font("ocr-b", Font.BOLD, 12));
barcode.drawBarcode("c://Output//OnBarcode.com//java-ean13-text-font.png");



Show, hide barcode text

You can hide linear barcode text label by calling method setShowText() using Java barcode library. Now you can display a 1d barcode in a smaller space.

Code128 barcode = new Code128();
barcode.setData("Code-128");
barcode.setShowText(false);
barcode.drawBarcode("c://Output//OnBarcode.com//java-code-128-text-hide.png");



Show, hide barcode check digit in text label

Some barcode formats (such as Code 39, Code 93), you can apply check digit without displaying it in the barcode text label.

Code 39 barcode check sum digit is optional. To apply it, you need call method setAddCheckSum() in Java code.
Code39 barcode = new Code39();
barcode.setData("CODE-39");
barcode.setAddCheckSum(true);
barcode.drawBarcode("c://Output//OnBarcode.com//java-code-39-text-check-digit-show.png");


The Java source code below, we will generate the same Code 39 barcode with check digit applied, but the check digit will not be displayed in the barcode text label.
Code39 barcode = new Code39();
barcode.setData("CODE-39");
barcode.setAddCheckSum(true);
barcode.setShowCheckSumChar(false);
barcode.drawBarcode("c://Output//OnBarcode.com//java-code-39-text-check-digit-hide.png");


You can compare the two Code 39 barcodes below. The two barcodes are including the exactly same bar and space modules, but the top barcode text label displays the check digit in the last character, and the bottom barcode hides the check digit.





How to print, config barcode color and image settings in Java?

Top
Using OnBarcode Java Barcode Generation library, you are able to apply barcode colors and image settings in Java application.
View details at: How to create, print barcode with customized colors and image settings using Java?

Barcode Colors Customization Barcode Image Options (EPS Output)




How to Generate Barcodes in html or JSP Pages with Tomcat?

Top
  1. Under demo package, copy barcode folder and its contents to your tomcat webapps file
  2. Start tomcat, and navigate to http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128
  3. To create bar code images in html or jsp pages, you can insert an image tag (img) into your page.
  4. Demo tag: <img src="http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128" />





How to Create & Print Barcode Image in Java Servlet Class?

Top
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 Barcode in Java reports (Jasper, iReport, BIRT reports)?

Top
This high-quality Java Barcode Generator also supports generating barcode images in Jasper Reports. For detailed generation tutorial, please view Complete Guide for Barcode Generation in Jasper Reports.

How to Generate Barcode Image in iReport, Jasper, BIRT reports?

Top
If you need to generate bulk barcode images in iReport, Jasper reports and BIRT reports using Java Barcode Generator, please view here:





Java Barcode Generator Supporting Barcode Symbology Types

Top

Barcode for Java - Bar Code Types Generation





































Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.