Java Data Matrix Generator Tutorial

Generate, create 2d barcode Data Matrix images in Java Class, JSP Pages, Servlet, Jasper Report

Download Java Barcode Generator Free Evaluation
Purchase Java Barcode Generator SDK License

Generate, Create, Print, Draw Data Matrix barcode in Java projects

  • Easy to Generate Data Matrix in Java class, Java Bean, Applet, Swing applications
  • Create Data Matrix in Java Servlet, JSP, EJB, Tomcat, JBoss server side projects
  • Print, draw Data Matrix barcode images in Jasper Reports, iReport & Eclipse BIRT Java reporting software
  • Encoding, generating high quality Data Matrix BarCode images in GIF, JPEG, PNG, & EPS formats
  • Compatible with ISO / IEC 16022 (2nd edition 2006-09-15)
  • 100% written in Java for JDK 1.4.2 and later versions
  • Simple to redistribute Java Data Matrix barcode library. No registration key. No activation code
  • Mature Data Matrix barcode generation Java library

Java Data Matrix Generator Introduction
Java Barcode Generator library includes a set of Java API which supports generating Data Matrix, and other 20+ linear, 2d barcode symoboglies in your Java applications.

You can download the trial package of Java Barcode library to generate Data Matrix in your Java projects. The download the package includes compiled Java barcode generate SDK (a single Jar file), detailed tutorial for creating barcodes in Java, and complete sample Java source code to generate Data Matrix in Java class. Using downloaded Java barcode generation SDK, you can also easily create and print QR Code, PDF417, Code 39, Code 128, UPC, ITF14, and other 1d and 2d barcodes.

Barcode Data Matrix Introduction

Data Matrix code is a two-dimensional matrix barcode consisting of black and white "cells" or modules arranged in either a square or rectangular pattern. The information to be encoded can be text or raw data. Usual data size is from a few bytes up to 2 kilobytes. The length of the encoded data depends on the symbol dimension used. Error correction codes are added to increase symbol strength: even if they are partially damaged, they can still be read. A Data Matrix symbol can store up to 2,335 alphanumeric characters.

Java Data Matrix Generator - Valid Data Scope

Java Data Matrix Generator encodes:
  • All 128 characters of ASCII
  • Values 128-255 in accordance with ISO 8859-1. These are referred to as extended ASCII.
How to Generate Data Matrix in Java Class?
Provided here is the Java sample codings for generating Data Matrix 2D barcode images in Java class. For detailed barcode setting demo, please navigate to complete Java Data Matrix Generator Demo Source Code
// Create Java Data Matrix object 
DataMatrix barcode = new DataMatrix();

// Set Data Matrix data text to encode

// Generate Data Matrix barcode & print into Graphics2D object
barcode.drawBarcode("Java Graphics2D object");

// Generate Data Matrix barcode & encode into GIF format

// Generate Data Matrix barcode & encode into JPEG format

// Generate Data Matrix barcode & encode into EPS
How to Create Data Matrix image in HTML or JSP pages?
  1. Under demo package, copy barcode folder and its contents to your tomcat.
  2. Start tomcat, navigate to http://YourDomain:Port/barcode/barcode?DATA=DataMatrixEncodingData&TYPE=datamatrix
  3. To create bar code images in html or jsp pages, you can insert a image tag (img) into your page.
  4. For example, <img src="http://YourDomain:Port/barcode/barcode?DATA=DataMatrixEncodingData&TYPE=datamatrix"/>
How to Generate Data Matrix Image in Java Servlet Class?
import com.onbarcode.barcode.AbstractBarcode; 
import com.onbarcode.barcode.DataMatrix;
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 {
DataMatrix barcode = new DataMatrix();
barcode.setData("Data Matrix");

ServletOutputStream servletoutputstream = response.getOutputStream();

response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);

// Generate DataMatrix barcode & output to ServletOutputStream

} catch (Exception e) {
throw new ServletException(e);
How to Generate GS1 Compatible Data Matrix?
  1. You need set property fnc1Mode value to IBarcode.FNC1_ENABLE (1), to indicate that the generated data matrix is GS1 compatible.
  2. You can use 2 methods to input GS1 AI elements
    • Parenthesis: to use parenthesis to indicate an AI code. Eg. (01)12345
    • Escape Tilde: to use ~ai* to indicate an AI while * is digit number of the AI (from 2 ~ 7). Eg. ~ai20112345 same as (01)12345
    • Note 1: To use escape tilde, you need set processTilde property to true.
    • Note 2: You can not use parenthesis character in the data message, except, user enables ProcessTilde flag and use ~040 and ~041 to replace '(' and ')' Parenthesis must in pair, otherwise, the output would be incorrect.
Data Matrix Reader Programming and Macro 05, Macro 06
To encode Data Matrix reader programming, macro 05 and macro 06, you need set procesTilde property to true, and add one of the following message to the beginning of the encoding Data Matrix data
  1. ~rp: Reader Programming (for ASCII mode and Auto mode only)
  2. This should be located at the beginning of the encoding data, e.g. data = "~rpABCD1234".
  3. ~m5: 05 Macro (for ASCII mode and Auto mode only)
  4. This should be located at the beginning of the encoding data, e.g. data = "~m5ABCD1234".
  5. ~m6: 06 Macro (for ASCII mode and Auto mode only)
  6. This should be located at the beginning of the encoding data, e.g. data = "~m6ABCD1234".
How to Generate & Print Barcode Data Matrix Image in Jasper Reports?
OnBarcode Java Barcode Generator can easily add barcoding features into Jasper Reports through Jasper Report JRRenderable Implementation. Here is the Complete Guide for Barcode Generation in Jasper Reports
How to Generate & Print Barcode Data Matrix Image in iReport?
To generate Data Matrix barcodes in iReport through Jasper Reports JRRenderable Implementation, please view the Complete Guide for Barcode Generation in iReport.
How to Generate & Print Barcode Data Matrix Image in Eclipse BIRT?
How to Set Generated Barcode Data Matrix Image Width and Height?
  1. You can set barcode image width and height through properties barcodeWidth and barcodeHeight values.
  2. Or you can set X (bar module width) and Y (bar module height) values
Java Data Matrix Generator - Property Settings
Java Data Matrix Generator in Java Class: com.onbarcode.barcode.DataMatrix

Category Properties Value Comments
Basic Property: data
Type: String
Default: ""
Barcode value to encode

UPC-A Valid Data Char Set:
  • 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits)

UPC-A Valid data length:
  • 11 digits only, excluding the last checksum digit.

Property: addCheckSum
Type: boolean
Default: false
addCheckSum property is not applied here. Java Barcode will always add a check character in the last digit (modulo 10).
UPC-A Related Property: supData
Type: String
Default: ""
Set the supplement data to encode. Valid values are 2 or 5 digits
Property: supHeight
Type: float
Default: 0.8f
This is a multiplicator of the height of the bar module; The default is 0.8 (80% of Y - bar module height).
Property: supSpace
iReport & URL: SUP-SPACE
Type: float
Default: 15
The separation between the barcode and the supplement. Default is 15 pixel.
Property: autoResize
Type: boolean
Default: false
Auto resize the generated barcode image
Property: barAlignment
Type: int
Default: 1 (center)
Barcode horizontal alignment inside the image. 0: left, 1: center, 2: right.
Property: uom
iReport & URL: UOM
Type: int
Default: 0 (pixel)
Unit of meature for all size related settings in the library.
Valid values: 0: pixel; 1: inch; 2: cm.
Property: X
iReport & URL: X
Type: float
Default: 1
Width of barcode bar module (narrow bar), default is 1 pixel
Property: Y
iReport & URL: Y
Type: float
Default: 30
Height of barcode bar module, default is 30 pixel
Property: barcodeWidth
Type: float
Default: 0
Barcode image width.

If barcodeWidth setting is smaller than the barcode required minimum width, the library will automatically reset to barcode minimum width.
Property: barcodeHeight
Type: float
Default: 0
Barcode image height.

If barcodeHeight setting is smaller than the barcode required minimum height, the library will automatically reset to barcode minimum height.
Property: leftMargin
Type: float
Default: 0
Barcode image left margin size.
Property: rightMargin
Type: float
Default: 0
Barcode image right margin size.
Property: topMargin
Type: float
Default: 0
Barcode image top margin size.
Property: bottomMargin
Type: float
Default: 0
Barcode image bottom margin size.
Property: resolution
Type: int
Default: 72
Barcode image resolution in DPI (Dots per inch).
Property: rotate
iReport & URL: ROTATE
Type: int
Default: 0 (IBarcode.ROTATE_0)
Valid values:

  • 0 (IBarcode.ROTATE_0)
  • 1 (IBarcode.ROTATE_90)
  • 2 (IBarcode.ROTATE_180)
  • 3 (IBarcode.ROTATE_270)
Property: backColor
Type: Color
Default: white
Barcode image background color
Property: foreColor
Type: Color
Default: black
Barcode image foreground color
Property: showText
iReport & URL: SHOW-TEXT
Type: boolean
Default: true
If true, display barcode data text under the barcode, otherwise do not display.
Property: textFont
iReport & URL: TEXT-FONT
Type: Font
new Font("Arial", Font.PLAIN, 11)
Barcode text font style.

In Java Servlet web streaming, using the url paramter in the following format:
Property: textMargin
Type: float
Default: 6
Space between barcode and barcode data text, default is 6 pixel
Property: textColor
Type: Color
Default: black
Barcode text color
// generate barcode and output to OutputStream object
public boolean drawBarcode(OutputStream outputStream) throws Exception

// generate barcode into a new BufferedImage object
public BufferedImage drawBarcode() throws Exception

    Use this method to generate barcode, and save into gif or jpeg files
    1. to save into gif file, filename ends with ".gif", like "c:\\barcode.gif"
    2. to save into jpeg file, filename ends with ".jpg", like "c:\\barcode.jpg"
    3. to save into png file, filename ends with ".png", like "c:\\barcode.png"
public byte[] drawBarcodeToBytes() throws Exception

public boolean drawBarcode(String imageFile) throws Exception

// Generate barcode and save into EPS file, the filename must ends with ".eps"
public void drawBarcode2EPS(String filename) throws Exception

// Generate barcode on Graphics2D object within certain area
public void drawBarcode(Graphics2D g, Rectangle2D rectangle) throws Exception
Java Barcode Generator Supporting Barcode Symbology Types
Barcode for Java - Bar Code Types Geneation
OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © . All rights reserved.