Console: Generate "designer" 2D barcodes with dots as cells.
Posted: Wed Jul 25, 2018 4:47 pm
Here is C# example that shows how to generate "designer" 2D barcodes with dots as cells:
Code: Select all
/// <summary>
/// Returns a graphic path, which represents 2D barcode with dots as cells.
/// </summary>
/// <param name="barcodeType">A barcode type.</param>
/// <param name="barcodeValue">A barcode value.</param>
/// <returns>
/// A dots 2D barcode.
/// </returns>
public static System.Drawing.Drawing2D.GraphicsPath GetDots2DBarcodeAsGraphicsPath(
Vintasoft.Barcode.BarcodeType barcodeType,
string barcodeValue)
{
Vintasoft.Barcode.BarcodeWriter writer = new Vintasoft.Barcode.BarcodeWriter();
writer.Settings.Barcode = barcodeType;
writer.Settings.Value = barcodeValue;
return GetDots2DBarcodeAsGraphicsPath(writer);
}
/// <summary>
/// Returns a graphic path, which represents 2D barcode with dots as cells.
/// </summary>
/// <param name="writer">A barcode writer.</param>
/// <returns>
/// A dots 2D barcode.
/// </returns>
public static System.Drawing.Drawing2D.GraphicsPath GetDots2DBarcodeAsGraphicsPath(
Vintasoft.Barcode.BarcodeWriter writer)
{
// get barcode as structure
Vintasoft.Barcode.BarcodeStructure.MatrixBarcodeStructure barcode = (Vintasoft.Barcode.BarcodeStructure.MatrixBarcodeStructure)writer.GetBarcodeStructure();
// get barcode bounding box
System.Drawing.Rectangle barcodeBBox = barcode.GetBoundingBox();
int x0 = barcodeBBox.X;
int x1 = barcodeBBox.X + barcodeBBox.Width;
int y0 = barcodeBBox.Y;
int y1 = barcodeBBox.Y + barcodeBBox.Height;
// create barcode graphic path
System.Drawing.Drawing2D.GraphicsPath barcodeGraphicPath = new System.Drawing.Drawing2D.GraphicsPath();
// for each cell in barcode
for (int y = y0; y <= y1; y++)
{
for (int x = x0; x <= x1; x++)
{
// if (x, y) cell is black sell
if (barcode.IsBlackCell(x, y))
{
// if (x, y) cell is search pattern
if (barcode.IsSearchPattern(x, y))
{
// get barcode element
Vintasoft.Barcode.BarcodeStructure.BarcodeElement barcodeElement = barcode.GetMatrixElement(x, y);
// if barcode element is QR search pattern
if (barcodeElement == Vintasoft.Barcode.BarcodeStructure.BarcodeElements.QrSearchPattern)
{
// (x,y)
// #######
// # #
// # ### #
// # ### #
// # ### #
// # #
// #######
// (x+7,y+7)
// add outer rectangle 7x7
barcodeGraphicPath.AddRectangle(new System.Drawing.RectangleF(x + 1.5f, y + 1.5f, 6, 6));
// add inner cell 3x3
barcodeGraphicPath.AddRectangle(new System.Drawing.RectangleF(x + 3.75f, y + 3.75f, 1.5f, 1.5f));
barcodeGraphicPath.AddRectangle(new System.Drawing.RectangleF(x + 3.5f, y + 3.5f, 2f, 2f));
}
// if barcode element is Aztec search pattern
else if (barcodeElement == Vintasoft.Barcode.BarcodeStructure.BarcodeElements.AztecCompactSearchPattern || barcodeElement == Vintasoft.Barcode.BarcodeStructure.BarcodeElements.AztecFullRangeSearchPattern)
{
// Compact/Rune Full Range
// (x,y)
// #############
// (x,y) # #
// ######### # ######### #
// # # # # # #
// # ##### # # # ##### # #
// # # # # # # # # # #
// # # # # # # # # # # # #
// # # # # # # # # # #
// # ##### # # # ##### # #
// # # # # # #
// ######### # ######### #
// (x+9,y+9) # #
// #############
// (x+13,y+13)
// add outer rectangles: 13x13, 9x9, 5x5
int rectCount;
if (barcodeElement == Vintasoft.Barcode.BarcodeStructure.BarcodeElements.AztecFullRangeSearchPattern)
rectCount = 3;
else
rectCount = 2;
for (int i = 0; i < rectCount; i++)
{
barcodeGraphicPath.AddRectangle(new System.Drawing.RectangleF(x + 0.5f, y + 0.5f,
(rectCount - i) * 4, (rectCount - i) * 4));
x += 2;
y += 2;
}
// add inner cell 1x1
barcodeGraphicPath.AddEllipse(new System.Drawing.RectangleF(x, y, 1, 1));
}
// if barcode element is DataMatrix or Han Xin Code search patterns
else
{
// get barcode matrix element
Vintasoft.Barcode.BarcodeStructure.BarcodeMatrixElement element = barcodeElement as Vintasoft.Barcode.BarcodeStructure.BarcodeMatrixElement;
// if barcode matrix element is not empty
if (element != null)
{
// add as single pattern use graphics path
using (System.Drawing.Drawing2D.GraphicsPath patternPath = new System.Drawing.Drawing2D.GraphicsPath())
{
for (int yc = 0; yc < element.Height; yc++)
{
for (int xc = 0; xc < element.Width; xc++)
{
if (element.Matrix[yc, xc] == Vintasoft.Barcode.BarcodeStructure.BarcodeElements.BlackCell)
barcodeGraphicPath.AddRectangle(new System.Drawing.Rectangle(x + xc, y + yc, 1, 1));
}
}
// add pattern to barcode graphic path
barcodeGraphicPath.AddPath(barcodeGraphicPath, false);
}
}
// if barcode matrix element is empty
else
{
// add single rectangle to barcode graphic path
barcodeGraphicPath.AddRectangle(new System.Drawing.RectangleF(x, y, 1, 1));
}
}
}
// if (x, y) cell is not search pattern
else
{
// add single dot to barcode graphic path
barcodeGraphicPath.AddEllipse(new System.Drawing.RectangleF(x, y, 1, 1));
}
}
}
}
return barcodeGraphicPath;
}