Generate Data Matrix Code in Java.

Data matrix code is an omnidirectional 2D barcode defined by an L-shaped pattern at one side of the code along with solid border at two adjacent sides. In specific use cases, you may need to create a Data Matrix code with a variety of encoding types. This article explains how to generate a Data Matrix Code programmatically in Java.

Java API Installation to Generate Data Matrix Code

Aspose.BarCode for Java provides a robust API for creating and scanning a wide range of barcodes, including Data Matrix. Download the required JAR and Javadoc files and add the repository and dependency entries below to your Maven pom.xml to start using the library.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-barcode</artifactId>
    <version>22.8</version>
</dependency>

Generate Data Matrix Code in Java

Follow these steps to create a basic Data Matrix barcode:

  1. Instantiate a BarcodeGenerator object and set EncodeTypes.DATA_MATRIX as the symbology.
  2. Choose an appropriate EncodeMode (e.g., EncodeMode.AUTO) to let the API select the optimal encoding.
  3. Call the save method to write the barcode image to a file (PNG, JPEG, SVG, etc.).

The example below demonstrates the complete process:

Generate Data Matrix ECC200 Code in Java

ECC200 is the most widely used Data Matrix variant, offering high error‑correction capability. To generate an ECC200 barcode:

  1. Create a BarcodeGenerator instance and specify EncodeTypes.DATA_MATRIX.
  2. Set the EncodeMode to EncodeMode.ECC200 and define the desired image size or pixel dimensions.
  3. Save the barcode using the save method to export it in your preferred format.

The code snippet below provides a ready‑to‑run example:

Get a Free License

Request a free temporary license to evaluate all Aspose.BarCode features without usage limits. This allows you to test encoding options, error correction levels, and output formats in your development environment.

Conclusion

This tutorial demonstrated how to programmatically generate Data Matrix barcodes in Java, covering both basic generation and ECC200‑specific creation. Data Matrix barcodes provide strong error correction, tolerating up to 30 % damage, which makes them ideal for aerospace, automotive, and other high‑reliability applications. For additional details, explore the documentation or ask questions in the forum.

See Also