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.
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.
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.
How to Generate Data Matrix in Java Class?
Here we will show you a sample Java source code to generate Data Matrix 2D barcode images in Java class using Java Barcode library.
- Create a
DataMatrixobject - Set the data matrix barcode encoding text through method
setData() - Draw and print the Data Matrix barcode into a PNG image file using methdo
drawBarcode()
DataMatrix barcode = new DataMatrix(); barcode.setData("Data Matrix"); try { barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-demo.png"); } catch (Exception e) { e.printStackTrace(); }
Print Data Matrix in Java memory
If you need to create and print Data Matrix barcode in Java memory and futher process the Data Matrix barcode object, Java barcode generator library provides several methods for you.
You can find more details about How to create and print barcode object in Java memory using Java Barcode library?
OutputStream barcodeInStream = new ByteArrayOutputStream(); boolean success = barcode.drawBarcode(barcodeInStream);
You can find more details about How to create and print barcode object in Java memory using Java Barcode library?
Data Matrix Data Encoding in Java
Data Matrix barcode supports encoding:
- All 128 characters of ASCII
- Values 128-255 in accordance with ISO 8859-1. These are referred to as extended ASCII.
- Unicode text
- Binary data
Convert ASCII string to Data Matrix
Encode ASCII text (plain text) to Data Matrix is an easy job. Input the ASCII text string to method
setData("ASCII text here").
DataMatrix barcode = new DataMatrix(); barcode.setData("Abc-123"); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-ascii-string.png");
Encode ASCII non-printing chars
Full ASCII table includes 128 characters. 33 of them are control characters (non-printing chars), such as character 'carriage return' or
Using Java Barcode Generator library, you can use non-printing char's ASCII value to encode into Data Matrix in Java class.
[CR] in short. You can not directly key-in these non-printing characters in your Java IDE code editor.
Using Java Barcode Generator library, you can use non-printing char's ASCII value to encode into Data Matrix in Java class.
- Enable property 'process tilde' using method
setProcessTilde(true) - Convert each non-printing character's ASCII code value to 3-digit format, add
~in front. For example, to encode[CR](it's ASCII value is 13), input~013in Data Matrix barcode data.
DataMatrix barcode = new DataMatrix(); barcode.setProcessTilde(true); barcode.setData("Abc~013123"); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-ascii-non-printing.png");
Convert Unicode text to Data Matrix
Data Matrix supports Unicode text encoding. You can quickly encode and create Data Matrix with Unicode text string encoded in Java program.
- Create a
DataMatrixobject - Set the data matrix barcode encoding Unicode text using method
setData(). In the following sample Java code, we will convert and encode東京(Tokyo in Japanese) to Data Matrix. - Enable Unicode text encoding in method
setEncodeUnicodeText(true) - Print the Data Matrix with Unicode text into an image file
DataMatrix barcode = new DataMatrix(); barcode.setData("東京"); barcode.setEncodeUnicodeText(true); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-unicode-text.png");
Encode GS1 data message
Data Matrix supports GS1 standard. You can create and encode GS1 data text into Data Matrix barcode using Java Data Matrix Generator library.
- Enable
FNC1(Function 1) in methodsetFnc1Mode(IBarcode.FNC1_ENABLE)to support GS1 barcode generation - Input GS1 AI code and data in method
setData() - Print GS1 Data Matrix barcode in Java
DataMatrix barcode = new DataMatrix(); barcode.setFnc1Mode(IBarcode.FNC1_ENABLE); barcode.setData("(17)050101(10)ABC123"); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-gs1.png");
How to create and adjust Data Matrix dimension size in Java?
Using OnBarcode Java Barcode library API, you can easily create and customize the Data Matrix with your required image width and height in Java application.
Data Matrix in square shape
Most of Data Matrix barcodes are in square shape. When you are generating Data Matrix in square using Java Barcode library, you need set the barcode width only. The barcode library will
create a square Data Matrix with required barcode width and height in Java application.
- Set Data Matrix image width in pixel in method
setBarcodeWidth(200)
DataMatrix barcode = new DataMatrix(); barcode.setData("Data Matrix"); barcode.setBarcodeWidth(200); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-size.png");
Data Matrix in rectangle shape
Data Matrix (ECC200) standard has specified 6 rectangular Data Matrix symbols with the following rows and columns.
In the following Java sample code, we will draw a rectangle Data Matrix with 16 rows and 48 columns.
- 8 rows x 18 columns
- 8 rows x 32 columns
- 12 rows x 26 columns
- 12 rows x 36 columns
- 16 rows x 36 columns
- 16 rows x 48 columns
In the following Java sample code, we will draw a rectangle Data Matrix with 16 rows and 48 columns.
- Use method
setFormatMode(DataMatrix.F_16X48)to create a Data Matrix with 16 rows and 48 columns - Set the Data Matrix barcode width in 300 pixels, and height in 100 pixels.
DataMatrix barcode = new DataMatrix(); barcode.setData("Data Matrix"); barcode.setFormatMode(DataMatrix.F_16X48); barcode.setBarcodeWidth(300); barcode.setBarcodeHeight(100); barcode.drawBarcode("c://Output//OnBarcode.com//java-data-matrix-size-rectangle.png");
Create Data Matrix with advanced options using Java
Data Matrix Format Mode
Data Matrix ISO standard specifies 24 square symbols and 6 rectangular symbols available in ECC 200.
Using Java Data Matrix Barcode Generator library, you can create and customize Data Matrix barcode with specified size format in Java class.
Using Java Data Matrix Barcode Generator library, you can create and customize Data Matrix barcode with specified size format in Java class.
- Set property
FormatModeto specify the generated Data Matrix size format mode.
DataMatrix barcode = new DataMatrix(); barcode.FormatMode = DataMatrixFormatMode.Format_48X48;
Data Matrix Structured Append mode
Using Java Data Matrix Generation SDK, you can easily split a large data message and store to multiple Data Matrix barcodes in Java application.
The following Java source codes show how to store one message into 3 three Data Matrix barcode images using Java Barcode Generator library.
The following Java source codes show how to store one message into 3 three Data Matrix barcode images using Java Barcode Generator library.
- Set property
StructuredAppendto true to enable Data Matrix Structured Append mode - Set property
SymbolCountto specify the total number of Data Matrix barcodes to store the same data message - Set property
SymbolIndexto specify the order of the current data matrix. The first symbol index should be starting with0 - Set property
FileIdwith a random unique integer. All data matrix barcodes to store the same data message should have the sameFileIdvalue.
DataMatrix barcode = new DataMatrix(); barcode.Data = "Data Matrix"; barcode.StructuredAppend = true; barcode.SymbolCount = 3; // The whole data message will be stored in three Data Matrix barcodes barcode.SymbolIndex = 0; // This Data Matrix is the first one barcode.FileId = 999; barcode.drawBarcode("C://Output//csharp-datamatrix-structured-append-demo.png");
Data Matrix FNC1 in the first position
When you need create Data Matrix with GS1 data encoded in Java program, you need make sure the FNC1 appears in the first Data Matrix symbol character position.
To enable this mode using Java Data Matrix barcode generator library, you need apply the following property in the Java web and desktop application.
To enable this mode using Java Data Matrix barcode generator library, you need apply the following property in the Java web and desktop application.
- Set property
FNC1with valueFNC1.FNC1_1ST_POS
DataMatrix barcode = new DataMatrix(); barcode.FNC1 = FNC1.FNC1_1ST_POS;
How to Create Data Matrix image in HTML or JSP pages?
- Under demo package, copy barcode folder and its contents to your tomcat.
- Start tomcat, navigate to http://YourDomain:Port/barcode/barcode?DATA=DataMatrixEncodingData&TYPE=datamatrix
- 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=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.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// Generate DataMatrix barcode & output to ServletOutputStream
barcode.drawBarcode(servletoutputstream);
} catch (Exception e) {
throw new ServletException(e);
}
}
}
How to generate & print Data Matrix in Java reports application?
OnBarcode Java Barcode Generator can easily add barcoding features Java reports application (including Jasper Reports, iReport, Eclipse BIRT Reports).
Java Data Matrix Generator - Property Settings
Java Data Matrix Generator in Java Class: com.onbarcode.barcode.DataMatrix
View the details here: complete Data Matrix barcode property settings in Java class
View the details here: complete Data Matrix barcode property settings in Java class
Common Asked Questions
What is a data matrix barcode?
A data matrix barcode is a 2d or matrix barcode symbology that is made of black (bar) and white (space) modules that are usually arranged in a square pattern.
Using OnBarcode Java Barcode Generator library, you can easily create, encode, customize and print Data Matrix barcode images in Java classes.
What are the different types of Data Matrix?
According to Data Matrix latest ISO standard (ISO/IEC 16022), there are two versions of Data Matrix
- ECC 000-140
- ECC 200
What is the ISO standard for Data Matrix barcode?
Data Matrix ISO specification is ISO/IEC 16022, and the latest standard version is ISO/IEC 16022:2024.
Java Barcode Generator library fully supports ISO compatible Data Matrix generations in Java, JSP, Servlet, Spring web applications.
How much data can a Data Matrix barcode store?
The maximum data storage of Data Matrix is depending on its encoding data formats.
- Alphanumeric data: up to 2,335 characters
- Byte data (8-bit): 1,555 characters
- Numeric data: 3,116 digits
What is the minimum and maximum size of Data Matrix barcode?
The minimum dimension size of Data Matrix is 10 x 10 modules. And the maximum dimension size of Data Matrix is 144 x 144 modules. Each modules are squares.
OnBarcode Java Barcode library supports all formats defined in Data Matrix ISO standard. You can generate and customize the Data Matrix format through
DataMatrix.setFormatMode() method in Java class.
What is the difference between Data Matrix and QR Code?
Data Matrix and QR Code have lot of same features. They are both 2 dimensional (matrix) barcodes, and enable to encode ASCII text, Unicode text, binary data.
Both of them are encoding GS1 business data messages.
Java Barcode SDK fully supports both Data Matrix and QR Code 2d barcodes generation in Java class, JSP, Servlet web and J2EE application.
Can a phone scan a Data Matrix barcode?
Yes. You can scan and read a Data Matrix barcode using your smartphone (iPhone or Android phone) with your barcode scanner app installed.