LEADTOOLS Barcode (Leadtools.Barcode assembly)
LEAD Technologies, Inc

DatamatrixBarcodeData Class

Example 







Members 
Contains the data for a single Datamatrix barcode. .NET support Silverlight support
Object Model
DatamatrixBarcodeData Class
Syntax
[TypeConverterAttribute()]
[SerializableAttribute()]
public class DatamatrixBarcodeData : BarcodeData, System.ICloneable  
'Declaration
 
<TypeConverterAttribute()>
<SerializableAttribute()>
Public Class DatamatrixBarcodeData 
   Inherits BarcodeData
   Implements System.ICloneable 
'Usage
 
Dim instance As DatamatrixBarcodeData
public sealed class DatamatrixBarcodeData : System.ICloneable  
ObjectiveC Syntax
Java Syntax
function Leadtools.Barcode.DatamatrixBarcodeData()
[TypeConverterAttribute()]
[SerializableAttribute()]
public ref class DatamatrixBarcodeData : public BarcodeData, System.ICloneable  
Remarks

The DatamatrixBarcodeData class derives from the base BarcodeData to add specific data that can be read or written when using Datamatrix barcodes.

Reading Barcodes

When the BarcodeReader object detects and reads a Datamatrix ( BarcodeSymbology.Datamatrix), the BarcodeReader.ReadBarcode and BarcodeReader.ReadBarcodes methods will return the DatamatrixBarcodeData object for each barcode cast down to the base BarcodeData class.

You can can perform standard .NET casting to get the object as DatamatrixBarcodeData and then examine the extra data properties associated with it.

In addition to all the data members returned as usual in the base BarcodeData object, this class adds the following members:

In addition to the members above, other Datamatrix read options not related to the barcode data can be set using the DatamatrixBarcodeReadOptions class.

Writing Barcodes

The BarcodeWriter class contains the WriteBarcode method that is used to write a barcode to an image. This method accepts the data parameter as the base BarcodeData object. To write a Datamatrix barcode, create an instance of DatamatrixBarcodeData and fill the required parameters, then pass this object as the data parameter to WriteBarcode.

In addition to all the data members that must be set as usual in the base BarcodeData object, also specify the following members:

In addition to the members above, other Datamatrix write options not related to the barcode data can be set using the DatamatrixBarcodeWriteOptions class.

You can use the static (or Shared in Visual Basic) BarcodeData.CreateDefaultBarcodeData method to create a new instance of DatamatrixBarcodeData that contains default parameters ready for writing to an image:

[Visual Basic]

    
             Dim datamatrixData As DatamatrixBarcodeData = DirectCast(BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix), DatamatrixBarcodeData)
             

  

[C#]

    
             DatamatrixBarcodeData datamatrixData = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix) as DatamatrixBarcodeData;
             

  

Example
Copy CodeCopy Code  
Public Sub DatamatrixBarcode_Example()
      Dim imageFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "MyDatamatrixBarcodes.tif")

      Dim engine As New BarcodeEngine()

      ' Create the image to write the barcodes to
      Dim resolution As Integer = 300
      Using image As RasterImage = RasterImage.Create(CType(8.5 * resolution, Integer), CType(11.0 * resolution, Integer), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White))
         ' Write two Datamatrix barcodes, a square and a rectangle one
         WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size16X36, "Datamatrix1 16x36", False)
         WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size132X132, "Datamatrix2 132x132", True)

         ' Save the image
         Using codecs As New RasterCodecs()
            codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1)
         End Using
      End Using

      ' Now read the barcodes again
      Using codecs As New RasterCodecs()
         Using image As RasterImage = codecs.Load(imageFileName)
            ' Read only square symbols, should only read the first barcode
            ReadBarcodes(engine.Reader, image, True)

            ' Read all symbols, should read both barcodes
            ReadBarcodes(engine.Reader, image, False)
         End Using
      End Using
   End Sub

   Private Sub WriteBarcode(ByVal writer As BarcodeWriter, ByVal image As RasterImage, ByVal symbolSize As DatamatrixBarcodeSymbolSize, ByVal value As String, ByVal topLeft As Boolean)
      ' Create the barcode data
      Dim barcode As DatamatrixBarcodeData = DirectCast(BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix), DatamatrixBarcodeData)
      barcode.SymbolSize = symbolSize
      barcode.Value = value

      ' We will use the alignment to position the barcodes, so use all of the image
      barcode.Bounds = New LogicalRectangle(0, 0, image.ImageWidth, image.ImageHeight, LogicalUnit.Pixel)

      ' Set the write options
      Dim options As New DatamatrixBarcodeWriteOptions()

      If topLeft Then
         options.HorizontalAlignment = BarcodeAlignment.Near
         options.VerticalAlignment = BarcodeAlignment.Near
      Else
         options.HorizontalAlignment = BarcodeAlignment.Far
         options.VerticalAlignment = BarcodeAlignment.Far
      End If

      options.DisableCompression = False
      options.GroupNumber = 0
      options.GroupTotal = 0
      options.FileIdNumberLowByte = 1
      options.FileIdNumberHighByte = 1
      options.XModule = 30

      ' Write it
      Console.WriteLine("Writing barcode with symbol size: {0} and Data: {1}", symbolSize, value)
      writer.WriteBarcode(image, barcode, options)
   End Sub

   Private Sub ReadBarcodes(ByVal reader As BarcodeReader, ByVal image As RasterImage, ByVal squareOnly As Boolean)
      ' Setup read options
      Dim options As New DatamatrixBarcodeReadOptions()
      options.ReadSquareSymbolsOnly = squareOnly
      options.EnableDoublePass = False
      options.EnableInvertedSymbols = False
      options.EnableFastMode = True
      options.EnableSmallSymbols = True

      Console.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly)
      Dim barcodes() As BarcodeData = reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, New BarcodeSymbology() {BarcodeSymbology.Datamatrix}, New BarcodeReadOptions() {options})
      Console.WriteLine("{0} barcodes found:", barcodes.Length)
      For Each barcode As DatamatrixBarcodeData In barcodes
         Console.WriteLine(" At {0}, symbol size: {1} and data: {2}", barcode.Bounds, barcode.SymbolSize, barcode.Value)
      Next
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
public void DatamatrixBarcode_Example()
   {
      string imageFileName = Path.Combine(LEAD_VARS.ImagesDir, "MyDatamatrixBarcodes.tif");

      BarcodeEngine engine = new BarcodeEngine();

      // Create the image to write the barcodes to
      int resolution = 300;
      using(RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.White)))
      {
         // Write two Datamatrix barcodes, a square and a rectangle one
         WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size16X36, "Datamatrix1 16x36", false);
         WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size132X132, "Datamatrix2 132x132", true);

         // Save the image
         using(RasterCodecs codecs = new RasterCodecs())
         {
            codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1);
         }
      }

      // Now read the barcodes again
      using(RasterCodecs codecs = new RasterCodecs())
      {
         using(RasterImage image = codecs.Load(imageFileName))
         {
            // Read only square symbols, should only read the first barcode
            ReadBarcodes(engine.Reader, image, true);

            // Read all symbols, should read both barcodes
            ReadBarcodes(engine.Reader, image, false);
         }
      }
   }

   private void WriteBarcode(BarcodeWriter writer, RasterImage image, DatamatrixBarcodeSymbolSize symbolSize, string value, bool topLeft)
   {
      // Create the barcode data
      DatamatrixBarcodeData barcode = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix) as DatamatrixBarcodeData;
      barcode.SymbolSize = symbolSize;
      barcode.Value = value;

      // We will use the alignment to position the barcodes, so use all of the image
      barcode.Bounds = new LogicalRectangle(0, 0, image.ImageWidth, image.ImageHeight, LogicalUnit.Pixel);

      // Set the write options
      DatamatrixBarcodeWriteOptions options = new DatamatrixBarcodeWriteOptions();

      if(topLeft)
      {
         options.HorizontalAlignment = BarcodeAlignment.Near;
         options.VerticalAlignment = BarcodeAlignment.Near;
      }
      else
      {
         options.HorizontalAlignment = BarcodeAlignment.Far;
         options.VerticalAlignment = BarcodeAlignment.Far;
      }

      options.DisableCompression = false;
      options.GroupNumber = 0;
      options.GroupTotal = 0;
      options.FileIdNumberLowByte = 1;
      options.FileIdNumberHighByte = 1;
      options.XModule = 30;

      // Write it
      Console.WriteLine("Writing barcode with symbol size: {0} and Data: {1}", symbolSize, value);
      writer.WriteBarcode(image, barcode, options);
   }

   private void ReadBarcodes(BarcodeReader reader, RasterImage image, bool squareOnly)
   {
      // Setup read options
      DatamatrixBarcodeReadOptions options = new DatamatrixBarcodeReadOptions();
      options.ReadSquareSymbolsOnly = squareOnly;
      options.EnableDoublePass = false;
      options.EnableInvertedSymbols = false;
      options.EnableFastMode = true;
      options.EnableSmallSymbols = true;

      Console.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly);
      BarcodeData[] barcodes = reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.Datamatrix }, new BarcodeReadOptions[] { options });
      Console.WriteLine("{0} barcodes found:", barcodes.Length);
      foreach(DatamatrixBarcodeData barcode in barcodes)
      {
         Console.WriteLine(" At {0}, symbol size: {1} and data: {2}", barcode.Bounds, barcode.SymbolSize, barcode.Value);
      }
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
[TestMethod]
public async Task DatamatrixBarcode_Example()
{
   string imageFileName = @"MyDatamatrixBarcodes.tif";
   BarcodeEngine engine = new BarcodeEngine();

   // Create the image to write the barcodes to
   int resolution = 300;
   using(RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColorHelper.FromKnownColor(RasterKnownColor.White)))
   {
      // Write two Datamatrix barcodes, a square and a rectangle one
      WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size16X36, "Datamatrix1 16x36", false);
      WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size132X132, "Datamatrix2 132x132", true);

      // Save the image
      using(RasterCodecs codecs = new RasterCodecs())
      {
         StorageFile saveFile = await Tools.AppLocalFolder.CreateFileAsync(imageFileName);
         await codecs.SaveAsync(image, LeadStreamFactory.Create(saveFile), RasterImageFormat.CcittGroup4, 1);
      }
   }

   // Now read the barcodes again
   using(RasterCodecs codecs = new RasterCodecs())
   {
      StorageFile loadFile = await Tools.AppInstallFolder.GetFileAsync(imageFileName);
      using(RasterImage image = await codecs.LoadAsync(LeadStreamFactory.Create(loadFile)))
      {
         // Read only square symbols, should only read the first barcode
         ReadBarcodes(engine.Reader, image, true);

         // Read all symbols, should read both barcodes
         ReadBarcodes(engine.Reader, image, false);
      }
   }
}

private void WriteBarcode(BarcodeWriter writer, RasterImage image, DatamatrixBarcodeSymbolSize symbolSize, string value, bool topLeft)
{
   // Create the barcode data
   BarcodeData barcode = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix);
   barcode.Datamatrix_SymbolSize = symbolSize;
   barcode.Value = value;

   // We will use the alignment to position the barcodes, so use all of the image
   barcode.Bounds = LeadRectHelper.Create(0, 0, image.ImageWidth, image.ImageHeight);

   // Set the write options
   DatamatrixBarcodeWriteOptions options = new DatamatrixBarcodeWriteOptions();

   if(topLeft)
   {
      options.HorizontalAlignment = BarcodeAlignment.Near;
      options.VerticalAlignment = BarcodeAlignment.Near;
   }
   else
   {
      options.HorizontalAlignment = BarcodeAlignment.Far;
      options.VerticalAlignment = BarcodeAlignment.Far;
   }

   options.DisableCompression = false;
   options.GroupNumber = 0;
   options.GroupTotal = 0;
   options.FileIdNumberLowByte = 1;
   options.FileIdNumberHighByte = 1;
   options.XModule = 30;

   // Write it
   Debug.WriteLine("Writing barcode with symbol size: {0} and Data: {1}", symbolSize, value);
   writer.WriteBarcode(image, barcode, options);
}

private void ReadBarcodes(BarcodeReader reader, RasterImage image, bool squareOnly)
{
   // Setup read options
   DatamatrixBarcodeReadOptions options = new DatamatrixBarcodeReadOptions();
   options.ReadSquareSymbolsOnly = squareOnly;
   options.EnableDoublePass = false;
   options.EnableInvertedSymbols = false;
   options.EnableFastMode = true;
   options.EnableSmallSymbols = true;

   Debug.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly);
   BarcodeData[] barcodes = reader.ReadBarcodes(image, LeadRectHelper.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.Datamatrix }, new IBarcodeReadOptions[] { options });
   if (barcodes == null)
   {
      Debug.WriteLine("No barcodes found");
      return;
   }

   Debug.WriteLine("{0} barcodes found:", barcodes.Length);
   foreach(BarcodeData barcode in barcodes)
   {
      Debug.WriteLine(" At {0}, symbol size: {1} and data: {2}", barcode.Bounds, barcode.Datamatrix_SymbolSize, barcode.Value);
   }
}
public void DatamatrixBarcode_Example(RasterImage image, Stream outStream)
{
   BarcodeEngine engine = new BarcodeEngine();
   // Create the image to write the barcodes to
   RasterCodecs codecs = new RasterCodecs();

   // Write two Datamatrix barcodes, a square and a rectangle one
   WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size16X36, "Datamatrix1 16x36", false);
   WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size132X132, "Datamatrix2 132x132", true);

   // Save the image
   codecs.Save(image, outStream, RasterImageFormat.CcittGroup4, 1);

   // Now read the barcodes again
   // Read only square symbols, should only read the first barcode
   ReadBarcodes(engine.Reader, image, true);

   // Read all symbols, should read both barcodes
   ReadBarcodes(engine.Reader, image, false);
}

private void WriteBarcode(BarcodeWriter writer, RasterImage image, DatamatrixBarcodeSymbolSize symbolSize, string value, bool topLeft)
{
   // Create the barcode data
   DatamatrixBarcodeData barcode = BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix) as DatamatrixBarcodeData;
   barcode.SymbolSize = symbolSize;
   barcode.Value = value;

   // We will use the alignment to position the barcodes, so use all of the image
   barcode.Bounds = new LogicalRectangle(0, 0, image.ImageWidth, image.ImageHeight, LogicalUnit.Pixel);

   // Set the write options
   DatamatrixBarcodeWriteOptions options = new DatamatrixBarcodeWriteOptions();

   if(topLeft)
   {
      options.HorizontalAlignment = BarcodeAlignment.Near;
      options.VerticalAlignment = BarcodeAlignment.Near;
   }
   else
   {
      options.HorizontalAlignment = BarcodeAlignment.Far;
      options.VerticalAlignment = BarcodeAlignment.Far;
   }

   options.DisableCompression = false;
   options.GroupNumber = 0;
   options.GroupTotal = 0;
   options.FileIdNumberLowByte = 1;
   options.FileIdNumberHighByte = 1;
   options.XModule = 30;

   // Write it
   Console.WriteLine("Writing barcode with symbol size: {0} and Data: {1}", symbolSize, value);
   writer.WriteBarcode(image, barcode, options);
}

private void ReadBarcodes(BarcodeReader reader, RasterImage image, bool squareOnly)
{
   // Setup read options
   DatamatrixBarcodeReadOptions options = new DatamatrixBarcodeReadOptions();
   options.ReadSquareSymbolsOnly = squareOnly;
   options.EnableDoublePass = false;
   options.EnableInvertedSymbols = false;
   options.EnableFastMode = true;
   options.EnableSmallSymbols = true;

   Console.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly);
   BarcodeData[] barcodes = reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, new BarcodeSymbology[] { BarcodeSymbology.Datamatrix }, new BarcodeReadOptions[] { options });
   Console.WriteLine("{0} barcodes found:", barcodes.Length);
   foreach(DatamatrixBarcodeData barcode in barcodes)
   {
      Console.WriteLine(" At {0}, symbol size: {1} and data: {2}", barcode.Bounds, barcode.SymbolSize, barcode.Value);
   }
}
Public Sub DatamatrixBarcode_Example(ByVal image As RasterImage, ByVal outStream As Stream)
  Dim engine As BarcodeEngine = New BarcodeEngine()
  ' Create the image to write the barcodes to
  Dim codecs As RasterCodecs = New RasterCodecs()

  ' Write two Datamatrix barcodes, a square and a rectangle one
  WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size16X36, "Datamatrix1 16x36", False)
  WriteBarcode(engine.Writer, image, DatamatrixBarcodeSymbolSize.Size132X132, "Datamatrix2 132x132", True)

  ' Save the image
  codecs.Save(image, outStream, RasterImageFormat.CcittGroup4, 1)

  ' Now read the barcodes again
  ' Read only square symbols, should only read the first barcode
  ReadBarcodes(engine.Reader, image, True)

  ' Read all symbols, should read both barcodes
  ReadBarcodes(engine.Reader, image, False)
End Sub

Private Sub WriteBarcode(ByVal writer As BarcodeWriter, ByVal image As RasterImage, ByVal symbolSize As DatamatrixBarcodeSymbolSize, ByVal value As String, ByVal topLeft As Boolean)
  ' Create the barcode data
  Dim barcode As DatamatrixBarcodeData = TryCast(BarcodeData.CreateDefaultBarcodeData(BarcodeSymbology.Datamatrix), DatamatrixBarcodeData)
  barcode.SymbolSize = symbolSize
  barcode.Value = value

  ' We will use the alignment to position the barcodes, so use all of the image
  barcode.Bounds = New LogicalRectangle(0, 0, image.ImageWidth, image.ImageHeight, LogicalUnit.Pixel)

  ' Set the write options
  Dim options As DatamatrixBarcodeWriteOptions = New DatamatrixBarcodeWriteOptions()

  If topLeft Then
    options.HorizontalAlignment = BarcodeAlignment.Near
    options.VerticalAlignment = BarcodeAlignment.Near
  Else
    options.HorizontalAlignment = BarcodeAlignment.Far
    options.VerticalAlignment = BarcodeAlignment.Far
  End If

  options.DisableCompression = False
  options.GroupNumber = 0
  options.GroupTotal = 0
  options.FileIdNumberLowByte = 1
  options.FileIdNumberHighByte = 1
  options.XModule = 30

  ' Write it
  Console.WriteLine("Writing barcode with symbol size: {0} and Data: {1}", symbolSize, value)
  writer.WriteBarcode(image, barcode, options)
End Sub

Private Sub ReadBarcodes(ByVal reader As BarcodeReader, ByVal image As RasterImage, ByVal squareOnly As Boolean)
  ' Setup read options
  Dim options As DatamatrixBarcodeReadOptions = New DatamatrixBarcodeReadOptions()
  options.ReadSquareSymbolsOnly = squareOnly
  options.EnableDoublePass = False
  options.EnableInvertedSymbols = False
  options.EnableFastMode = True
  options.EnableSmallSymbols = True

  Console.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly)
  Dim barcodes As BarcodeData() = reader.ReadBarcodes(image, LogicalRectangle.Empty, 0, New BarcodeSymbology() { BarcodeSymbology.Datamatrix }, New BarcodeReadOptions() { options })
  Console.WriteLine("{0} barcodes found:", barcodes.Length)
  For Each barcode As DatamatrixBarcodeData In barcodes
    Console.WriteLine(" At {0}, symbol size: {1} and data: {2}", barcode.Bounds, barcode.SymbolSize, barcode.Value)
  Next barcode
End Sub
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

DatamatrixBarcodeData Members
Leadtools.Barcode Namespace
BarcodeData Class
BarcodeReader Class
BarcodeWriter Class
BarcodeSymbology Enumeration
BarcodeEngine Class
MicroPDF417BarcodeData Class
PDF417BarcodeData Class
QRBarcodeData Class
DatamatrixBarcodeReadOptions Class
DatamatrixBarcodeWriteOptions Class
BarcodeReader.ReadBarcode
BarcodeReader.ReadBarcodes
BarcodeWriter.WriteBarcode
Programming with LEADTOOLS Barcode
Supported Barcode Symbologies
Unlocking Barcode Support
Reading Barcodes Tutorial
Writing Barcodes Tutorial
Datamatrix Barcodes in LEADTOOLS
Writing Barcodes - Bounds and XModule

 

 


Products | Support | Contact Us | Copyright Notices

© 2006-2012 All Rights Reserved. LEAD Technologies, Inc.

Leadtools.Barcode requires a Barcode Module license and unlock key. For more information, refer to: LEADTOOLS Toolkit Features