public bool ReadSquareSymbolsOnly { get; set; }
true to enable reading square DataMatrix symbols only (rectangular symbols are not detected). Otherwise, false (both square and rectangular symbols are detected). The default value is false.
LEADTOOLS can detect both square and rectangular symbols. DataMatrix symbols are usually square and not rectangular. If you have a requirement to detect strict square shapes only, then set the value of ReadSquareSymbolsOnly to true.
using Leadtools;
using Leadtools.Codecs;
using Leadtools.Barcode;
using Leadtools.ImageProcessing;
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;
barcode.Symbology= BarcodeSymbology.Datamatrix;
// We will use the alignment to position the barcodes, so use all of the image
barcode.Bounds = new LeadRect(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;
options.HRItoGS1 = false;
// 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.EnableDoublePassIfSuccess = false;
options.EnableInvertedSymbols = false;
options.EnableFastMode = true;
options.EnableSmallSymbols = true;
options.EnablePreprocessing = true;
options.GS1toHRI = false;
Console.WriteLine("Reading Datamatrix barcodes with ReadSquareSymbolsOnly set to {0}", squareOnly);
BarcodeData[] barcodes = reader.ReadBarcodes(image, LeadRect.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} data: {2} and symbology: {3}", barcode.Bounds, barcode.SymbolSize, barcode.Value, barcode.Symbology);
}
}
static class LEAD_VARS
{
public const string ImagesDir = @"C:\LEADTOOLS23\Resources\Images";
}