Java Barcode Generator, .NET Barcode Generator for C#, ASP.NET, VB.NET
HOME BARCODE FOR JAVA PURCHASE


Developer Guide > Data Matrix Generation Guide

Data Matrix Barcode Sample Code & Barcode Property Settings



1. Generate Data Matrix in Java Class

The following Java code illustrates how to generate a Data Matrix barcode in a Java class

	DataMatrix barcode = new DataMatrix();
        barcode.setDataMode(DataMatrix.MODE_AUTO);

        barcode.setFormatMode(9);
        barcode.setProcessTilde(true);
        barcode.setData("This is a testing string. It contains a digit stream" +
                "12345678901234567890 and special characters ^&^*&^#@*&$^#@&*.");

        barcode.setUOM(uom);
        barcode.setModuleSize(moduleSize);
        barcode.setLeftMargin(leftMargin);
        barcode.setRightMargin(rightMargin);
        barcode.setTopMargin(topMargin);
        barcode.setBottomMargin(bottomMargin);
        barcode.setResolution(resolution);
        barcode.setRotate(rotate);

        barcode.renderBarcode("c://datamatrix.gif");
        
        // generate barcode to BufferedImage object
        BufferedImage bufferedImage = linear.renderBarcode();

	// generate barcode to byte[] object
	byte[] barcodeBytes = linear.renderBarcodeToBytes();

	// render barcode on Graphics2D
	Graphics2D g = ...
	Rectangle2D rectangle = ... 
	linear.renderBarcode(g, rectangle);
	
	// generate barcode and output to OutputStream object
	OutputStream outputStream = ...
	linear.renderBarcode(outputStream);



2. Java Data Matrix Property Settings

  • Set the data property with the value to encode. Type is String.
    Servlet URL Parameter: "Data".
  • Set the dataMode property.
    Valid value is DataMatrix.MODE_AUTO (default), DataMatrix.MODE_ASCII, DataMatrix.MODE_C40,
    DataMatrix.MODE_TEXT, DataMatrix.MODE_X12, DataMatrix.MODE_EDIFACT, DataMatrix.MODE_BASE256.
    • Auto (0): Barcode library will decide the best data mode for you.
    • ASCII (1): it is used to encode data that mainly contains ASCII characters (0-127). This is the default encoding format by Barcode Library.
    • C40 (2): it is used to encode data that mainly contains numeric and upper case characters.
    • Text (3): it is used to encode data that mainly contains numeric and lower case characters.
    • X12 (4):it is used to encode the standard ANSI X12 electronic data interchange characters.
    • EDIFACT (5): it is used to encode 63 ASCII values (values from 32 to 94) plus an Unlatch character (binary 011111).
    • Base256 (6): it is used to encode 8 bit values.

    Servlet URL Parameter: "DataMode". Sample: &DataMode=1
  • Set the format property. Type is int. Default is DataMatrix.FORMAT_10X10. Specifies the Data Matrix Format to use on that symbology.
    Valid values see class DataMatrix.FORMAT_*x*;
    Servlet URL Parameter: "Format". Sample: &Format=0
  • Set the processTilde property to true, if you want use the tilde character "~" to specify special characters in the encoding data. Default is false.
    Format of the tilde:
    • ~NNN: is used to represent the ASCII character with the value of NNN. NNN is from 000 - 255.
    • ~6NNNNN: is used to represent the Unicode. NNNNN is from 00000 - 65535.
    • ~rp: is used only at the very beginning of the symbol for the reader programming purpose.
    • ~m5: is used only at the very beginning of the symbol, the header [)> + ASCII 30 + ASCII 05 + ASCII 29 will be transmitted by the barcode reader before the data in the message and the trailer ASCII 30 + ASCII 4 will be transmitted afterwards.
    • ~m6: is used only at the very beginning of the symbol, the header [)> + ASCII 30 + ASCII 06 + ASCII 29 will be transmitted by the barcode reader before the data in the message and the trailer ASCII 30 + ASCII 4 will be transmitted afterwards.
    • ~7NNNNNN: is used to specify the Extended Channel Interpretations and NNNNNN is a value between 000000 and 999999.

    Servlet URL Parameter: "ProcessTilde". Value: "t" (true), "f" (false). Sample: &ProcessTilde=t
  • Data Matrix can be divided into multiple data areas. Conversely, information stored in multiple Data Matrix symbols can be reconstructed as single data symbols. One data symbol can be divided into up to 16 symbols.
    • Set isStructuredAppend property to true, then Structured Append is enabled.
      Servlet Parameter: "IsStructuredAppend".
    • Set symbolCount property to the number of total symbols which make the sequence.
      Servlet Parameter: "SymbolCount".
    • Set symbolIndex property to the position of current symbol in the secuence (Start with 0).
      Servlet Parameter: "SymbolIndex".
    • Set fileID property to be identified to the same file.
      Servlet Parameter: "FileID".
  • Set the fnc1Mode property.
    • 0 (none)
    • 1 (enabled)

    Servlet URL Parameter: "FNC1Mode". Sample: &FNC1Mode=0
  • Barcode image size settings: How to control barcode size?
    • Set property uom (Unit of Measure) for properties X, Y, leftMargin, rightMargin, topMargin and bottomMargin.
      Default is Linear.UOM_PIXEL (0). Valid values are Linear.UOM_PIXEL (0), Linear.UOM_CM (1), Linear.UOM_Inch (2).
      Servlet URL Parameter: "UOM". Value: 0 (pixel), 1 (cm), 2 (inch). Sample: &UOM=0
    • Set the moduleSize (for barcode module width & height) property.
      Type is float. Default is 5.
      Servlet URL Parameter: "ModuleSize". Sample: &ModuleSize=5
    • Set the leftMargin, rightMargin, topMargin and bottomMargin properties, and types are all float.
      Default values are 0 for all 4 margin settings.
      Servlet URL Parameter: "LeftMargin", "RightMargin", "TopMargin", "BottomMargin". Sample: &LeftMargin=0
    • Set the resolution property (Value is expressed in DPI - Dots per inch).
      Default is 72 dpi.
      Servlet URL Parameter: "Resolution". Sample: &Resolution=72
  • With rotate property, you can display barcode horizontally or vertically.
    Value can be
    • 0 (Linear.ANGLE_0),
    • 1 (Linear.ANGLE_90),
    • 2 (Linear.ANGLE_180),
    • 3 (Linear.ANGLE_270)

    Default value is 0.
    Servlet URL Parameter: "Rotate". Sample: &Rotate=0


3. Encoding GS1 Compatible Data Matrix

  • Set property fnc1Mode value to 1, which indicates that you are encoding GS1 compatible data matrix
  • You can use (dddd) format to encode AI code "dddd".
    For example, to encode AI code 21 with value d12345777, and AI code 21 with value 12345678.
    barcode.setData("AI(21)d12345777AI(21)12345678").



4. All Barcode Types