Install Java Barcode Library
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
Javax Servlet
- /barcode-sdk/javax/OnBarcode.BarcodeGenerator.jar is for Javax Servlet
- Add barcode jar file to your Javax servlet application classpath
How to Generate Barcodes in Java Class?
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
You can create and print barcode in image file or in memory
- Image file path
- Java
BufferedImageobject - Java
Graphics2Dobject in specified rectangle area - Stream object
- Byte array object
Create barcode in image file
You can quickly create and print a barcode to an image file in Java application.
- Create a new barcode object. Here we create a
QRCodeobject. - Set QR Code encoding text through method
setData() - 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
Print barcode and draw barcode in specified area in Java Graphics2D object
Create barcode and paint it to a Stream object
Create and print barcode to byte array
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?
How to create and adjust barcode image size in Java?
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
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?
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
EAN13object, 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, 42)) - 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, 42)); 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
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.
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.
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?
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?
View details at: How to create, print barcode with customized colors and image settings using Java?
How to Generate Barcodes in html or JSP Pages with Tomcat?
- Under demo package, copy barcode folder and its contents to your tomcat webapps file
- Start tomcat, and navigate to http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128
- To create bar code images in html or jsp pages, you can insert an image tag (img) into your page.
- Demo tag: <img src="http://YourDomain:Port/barcode/barcode?DATA=0123456789&TYPE=CODE128" />
How to Create & Print Barcode Image in Java Servlet Class?
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)?
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?
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
Barcode for Java - Bar Code Types Generation
- 1D / Linear Barcodes:
- 2D / Matrix Barcodes: Data Matrix, PDF417, QR Code
