Generate "designer" 2D barcodes with dots as cells.

Code samples for VintaSoft Barcode .NET SDK. Here you can request a code sample.

Moderator: Alex

Post Reply
Alex
Site Admin
Posts: 1587
Joined: Thu Jul 10, 2008 2:21 pm

Generate "designer" 2D barcodes with dots as cells.

Post by Alex » Wed Jul 25, 2018 4:47 pm

Here is an 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 GraphicsPath GetDots2DBarcodeAsGraphicsPath(BarcodeType barcodeType, string barcodeValue)

{
    BarcodeWriter writer = new BarcodeWriter();
    writer.Settings.Barcode = barcodeType;
    writer.Settings.Value = barcodeValue;
    GraphicsPath path = GetDots2DBarcodeAsGraphicsPath(writer);
    return path;
}

/// <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 GraphicsPath GetDots2DBarcodeAsGraphicsPath(BarcodeWriter writer)
{
    // get barcode as structure
    MatrixBarcodeStructure barcode = (MatrixBarcodeStructure)writer.GetBarcodeStructure();
    // get barcode bounding box
    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
    GraphicsPath barcodeGraphicPath = new 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
                    BarcodeElement barcodeElement = barcode.GetMatrixElement(x, y);
                    // if barcode element is QR search pattern
                    if (barcodeElement == BarcodeElements.QrSearchPattern)
                    {
                        // (x,y)
                        //    #######
                        //    #     #
                        //    # ### #
                        //    # ### #
                        //    # ### #
                        //    #     #
                        //    #######
                        //       (x+7,y+7)                
                        // add outer rectangle 7x7
                        barcodeGraphicPath.AddRectangle(new RectangleF(x + 1.5f, y + 1.5f, 6, 6));
                        // add inner cell 3x3
                        barcodeGraphicPath.AddRectangle(new RectangleF(x + 3.75f, y + 3.75f, 1.5f, 1.5f));
                        
                        barcodeGraphicPath.AddRectangle(new RectangleF(x + 3.5f, y + 3.5f, 2f, 2f));
                    }
                    // if barcode element is Aztec search pattern
                    else if (barcodeElement == BarcodeElements.AztecCompactSearchPattern || barcodeElement == 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 == BarcodeElements.AztecFullRangeSearchPattern)
                            rectCount = 3;
                        else
                            rectCount = 2;
                        for (int i = 0; i < rectCount; i++)
                        {
                            barcodeGraphicPath.AddRectangle(new RectangleF(x + 0.5f, y + 0.5f,
                                (rectCount - i) * 4, (rectCount - i) * 4));
                            x += 2;
                            y += 2;
                        }

                        // add inner cell 1x1
                        barcodeGraphicPath.AddEllipse(new RectangleF(x, y, 1, 1));
                    }
                    // if barcode element is DataMatrix or Han Xin Code search patterns
                    else
                    {
                        // get barcode matrix element
                        BarcodeMatrixElement element = barcodeElement as BarcodeMatrixElement;
                        // if barcode matrix element is not empty
                        if (element != null)
                        {
                            // add as single pattern use graphics path
                            using (GraphicsPath patternPath = new GraphicsPath())
                            {
                                for (int yc = 0; yc < element.Height; yc++)
                                {
                                    for (int xc = 0; xc < element.Width; xc++)
                                    {
                                        if (element.Matrix[yc, xc] == BarcodeElements.BlackCell)
                                            barcodeGraphicPath.AddRectangle(new 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 RectangleF(x, y, 1, 1));
                        }
                    }
                }
                // if (x, y) cell is not search pattern
                else
                {
                    // add single dot to barcode graphic path
                    barcodeGraphicPath.AddEllipse(new RectangleF(x, y, 1, 1));
                }
            }
        }
    }

    return barcodeGraphicPath;
}

Post Reply