Ստեղծել անհրաժեշտ ձևի ացտեկական շտրիխ կոդ .NET-ում

Բլոգի կատեգորիա՝ Barcode.NET

03.06.2020

Որոշակի պահի կարող է անհրաժեշտ լինել ստեղծել շտրիխ կոդ և կոդավորել այն Այն տվյալները, որոնց չափը գերազանցում է շտրիխ կոդի տարողունակությունը, անհնարին դարձնելով բոլոր տվյալները մեկ շտրիխ կոդի մեջ տեղադրելը: Այս խնդիրը լուծելու համար դուք պետք է սկզբնական տվյալները բաժանեք մի քանի տվյալների բլոկների և կոդավորեք այդ տվյալների բլոկները մի քանի շտրիխ կոդերի մեջ: Դուք նաև պետք է նշեք տվյալների բլոկները, որպեսզի դրանք կարողանան միավորվել սկզբնական տվյալների մեջ:

Երկչափ շտրիխ կոդերի համար կա Structure Append տեխնոլոգիա, որը թույլ է տալիս բաժանել սկզբնական տվյալները տվյալների բլոկների և այդ տվյալների բլոկներից ստանալ սկզբնական տվյալները:
DataMatrix և QR Code շտրիխ կոդերի համար Structure Append տեխնոլոգիան թույլ է տալիս բաժանել սկզբնական տվյալները մինչև 16 տվյալների բլոկների և կոդավորել այդ տվյալների բլոկները 16 DataMatrix կամ QR Code շտրիխ կոդերի մեջ:
Aztec շտրիխ կոդերի համար դուք կարող եք բաժանել սկզբնական տվյալները մինչև 26 տվյալների բլոկների և կոդավորել դրանք 26 Aztec շտրիխ կոդերի մեջ:
PDF417 շտրիխ կոդերի համար Structure Append տեխնոլոգիան թույլ է տալիս բաժանել աղբյուրի տվյալները 99999 տվյալների բլոկների:
Իսկ Code16K շտրիխ կոդերի համար Structure Append տեխնոլոգիան թույլ է տալիս 9 տվյալների բլոկ:

Յուրաքանչյուր շտրիխ կոդ, որը պարունակում է տվյալների բլոկի դիրքը աղբյուրի տվյալներում, ուստի պատկերում տվյալների բլոկներով շտրիխ կոդերը կարող են դասավորվել պատահական կարգով: Այս գործառույթը կարող է օգտագործվել շտրիխ կոդերը պատկերի վրա առավելագույնս օպտիմալ կերպով տեղադրելու համար:

Ահա C# կոդ, որը թույլ է տալիս ստեղծել Aztec շտրիխ կոդ՝ "VintaSoft Barcode .NET SDK-ն պրոֆեսիոնալ 1D և 2D շտրիխ կոդերի ընթերցող և շտրիխ կոդերի գեներատոր գրադարան է .NET, WPF և WEB-ի համար":
/// <summary>
/// Generates Aztec barcode as raster image.
/// </summary>
public void GenerateAztecBarcode()
{
    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // set barcode writer settings
    barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.Aztec;
    barcodeWriter.Settings.Value = "VintaSoft Barcode .NET SDK is the professional 1D & 2D barcode reader and barcode generator library for .NET, WPF and WEB.";

    // get barcode as image
    using (System.Drawing.Image barcodeImage = barcodeWriter.GetBarcodeAsBitmap())
    {
        // save the barcode image to a file
        barcodeImage.Save("aztec-barcode.png");
    }
}

Ահա ստեղծված Aztec շտրիխ կոդի պատկերը.
Aztec barcode

Ահա C# կոդը, որը թույլ է տալիս կարդալ ստեղծված Aztec շտրիխ կոդում կոդավորված տվյալները.
/// <summary>
/// Recognizes Aztec barcode in image.
/// </summary>
/// <param name="barcodeImageFilePath">A path to a barcode image file.</param>
public void RecognizeAztecBarcode()
{
    // create barcode reader
    using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
    {
        // specify that reader must search for Aztec barcodes
        reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Aztec;

        // specify that reader must search for horizontal barcodes only
        reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal;

        // read barcodes from image file
        Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes("aztec-barcode.png");

        // if barcodes are not detected
        if (barcodeInfos.Length == 0)
        {
            System.Console.WriteLine("Barcodes are not found.");
        }
        // if barcodes are detected
        else
        {
            // get information about recognized barcodes

            System.Console.WriteLine(string.Format("{0} barcode(s) found:", barcodeInfos.Length));
            System.Console.WriteLine();
            for (int i = 0; i < barcodeInfos.Length; i++)
            {
                Vintasoft.Barcode.IBarcodeInfo barcodeInfo = barcodeInfos[i];
                System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, barcodeInfo.BarcodeType));
                System.Console.WriteLine(string.Format("Value:      {0}", barcodeInfo.Value));
                System.Console.WriteLine(string.Format("Region:     {0}", barcodeInfo.Region));
                System.Console.WriteLine();
            }
        }
    }
}

Ահա ստեղծված Aztec շտրիխ կոդից տվյալների ընթերցման արդյունքը.
1 barcode(s) found:

[1:Aztec]
Value: VintaSoft Barcode .NET SDK is the professional 1D & 2D barcode reader and barcode generator library for .NET, WPF and WEB.


Aztec շտրիխ կոդը քառակուսի ձև ունի, և դա կարող է հանգեցնել պատկերի տարածքի ոչ օպտիմալ օգտագործման, եթե շտրիխ կոդը պետք է տեղադրվի ուղղանկյուն տարածքում:

Հաջորդը մենք շտրիխ կոդի տեքստը բաժանում ենք 4 տվյալների բլոկների, մեկ շտրիխ կոդի փոխարեն ստեղծում ենք 4 շտրիխ կոդ և տեղադրում այդ 4 շտրիխ կոդերը հորիզոնական՝ շտրիխ կոդի դիրքավորման համար նախատեսված ուղղանկյուն ձևի տարածքը օպտիմալ կերպով օգտագործելու համար:

Ահա C# կոդը, որը թույլ է տալիս բաժանել տեքստը. "VintaSoft Barcode .NET SDK-ն պրոֆեսիոնալ 1D և 2D շտրիխ կոդ կարդացող և շտրիխ կոդ գեներացնող գրադարան է .NET, WPF և WEB լեզուների համար": բաժանեք 4 տվյալների բլոկների, ստեղծեք 4 Aztec շտրիխ կոդ այս տվյալների բլոկներով և տեղադրեք Aztec շտրիխ կոդերը հորիզոնական դիրքով.
/// <summary>
/// Generates several Aztec barcodes with structure append information and aligns barcodes horizontally.
/// </summary>
public void GenerateAztecBarcodesWithStructureAppendAndAlignBarcodeHorizontally()
{
    // source text
    string text = "VintaSoft Barcode .NET SDK is the professional 1D & 2D barcode reader and barcode generator library for .NET, WPF and WEB.";
    
    // count of text blocks
    int textBlockCount = 4;

    // maximum length of text block
    int textBlockLen = text.Length / textBlockCount;
    if ((text.Length % textBlockCount) != 0)
        textBlockLen++;

    // array with barcode images
    System.Drawing.Image[] barcodeImages = new System.Drawing.Image[textBlockCount];
    // for each text block
    for (int i = 0; i < textBlockCount; i++)
    {
        // calculate text block string

        string textBlock = text.Substring(0, textBlockLen);
        text = text.Substring(textBlockLen);

        // generate barcode image with text block
        barcodeImages[i] = GenerateImageOfAztecBarcodeWithStructureAppendInformation(textBlock, i + 1, textBlockCount);
    }

    // width of image with merged barcodes
    int mergedBarcodeWidth = 0;
    // for each barcode image
    for (int i = 0; i < barcodeImages.Length; i++)
    {
        // increase width of image with merged barcodes
        mergedBarcodeWidth += barcodeImages[i].Width;
    }

    // calculate height of image with merged barcodes
    int mergedBarcodeHeight = barcodeImages[0].Height;

    // create image with merged barcodes
    using (System.Drawing.Image mergedBarcodesImage = new System.Drawing.Bitmap(mergedBarcodeWidth, mergedBarcodeHeight))
    {
        // create Graphics for drawing on image with merged barcodes
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(mergedBarcodesImage))
        {
            // fill image with merged barcodes using white color
            g.FillRectangle(System.Drawing.Brushes.White, new System.Drawing.RectangleF(0, 0, mergedBarcodesImage.Width, mergedBarcodesImage.Height));

            // the horizontal position of barcode image on image with merged barcodes
            int barcodeXPosition = 0;
            // for each barcode image
            for (int i = 0; i < barcodeImages.Length; i++)
            {
                // draw barcode image on image with merged barcodes
                g.DrawImage(barcodeImages[i], new System.Drawing.PointF(barcodeXPosition, 0));

                // change the horizontal position of barcode image on image with merged barcodes
                barcodeXPosition += barcodeImages[i].Width;
            }
        }

        // save image with merged barcodes to a file
        mergedBarcodesImage.Save("aztec-barcodes-with-structure-append-and-aligned-horizontally.png");
    }

    // for each barcode image
    for (int i = 0; i < barcodeImages.Length; i++)
    {
        // dispose barcode image
        barcodeImages[i].Dispose();
    }
}

/// <summary>
/// Generates an image of Aztec barcode, which contains information about the structure append.
/// </summary>
/// <param name="barcodeValue">Barcode value.</param>
/// <param name="symbolPosition">Value position in structure append. Value from 1 to <i>symbolCount</i>.</param>
/// <param name="symbolCount">Count of values in structure append.</param>
/// <returns>An image of Aztec barcode, which contains information about the structure append.</returns>
public System.Drawing.Image GenerateImageOfAztecBarcodeWithStructureAppendInformation(string barcodeValue, int symbolPosition, int symbolCount)
{
    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // specify that Aztec barcode must be created
    barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.Aztec;

    // specify that created Aztec barcode should not have padding
    barcodeWriter.Settings.Padding = 0;

    // specify the barcode values
    barcodeWriter.Settings.ValueItems = new Vintasoft.Barcode.BarcodeInfo.ValueItemBase[] {
        // create the non-data flag that contains information about structure append
        Vintasoft.Barcode.BarcodeInfo.NonDataFlags.CreateAztecStructuredAppendCharacter(symbolPosition, symbolCount, null),
        // create a text value item
        new Vintasoft.Barcode.BarcodeInfo.TextValueItem(barcodeValue)
    };

    // return generated barcode image
    return barcodeWriter.GetBarcodeAsBitmap();
}

Ահա տվյալների բլոկներով ստեղծված Aztec շտրիխ կոդերի պատկերը.
Aztec barcodes with Structure Append and aligned horizontally

Տվյալների բլոկներով Aztec շտրիխ կոդեր պարունակող պատկերը ունի ուղղանկյուն ձև և, հետևաբար, կարող է օպտիմալ կերպով տեղակայվել ուղղանկյուն տարածքում:

Ահա C# կոդը, որը թույլ է տալիս ճանաչել ստեղծված Aztec շտրիխ կոդերից ստացված տվյալները և միավորել տվյալների բլոկները սկզբնական տվյալների մեջ.
/// <summary>
/// Recognizes Aztec barcodes with structure append information.
/// </summary>
/// <param name="barcodeImageFilePath">A path to a barcode image file.</param>
public void RecognizeAztecBarcodesWithStructureAppend()
{
    // create barcode reader
    using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
    {
        // specify that reader must search for Aztec barcodes
        reader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.Aztec;

        // specify that reader must search for horizontal barcodes only
        reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal;

        // read barcodes from image file
        Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = reader.ReadBarcodes("aztec-barcodes-with-structure-append-and-aligned-horizontally.png");

        // if barcodes are not detected
        if (barcodeInfos.Length == 0)
        {
            System.Console.WriteLine("Barcodes are not found.");
        }
        // if barcodes are detected
        else
        {
            // reconstruct barcode info from barcode infos with structure append information
            Vintasoft.Barcode.BarcodeInfo.StructuredAppendBarcodeInfo[] reconstructedBarcodeInfos =
                Vintasoft.Barcode.BarcodeInfo.StructuredAppendBarcodeInfo.ReconstructFrom(barcodeInfos);

            // get information about recognized barcodes

            System.Console.WriteLine(string.Format("{0} reconstructed barcode(s) found:", reconstructedBarcodeInfos.Length));
            System.Console.WriteLine();
            for (int i = 0; i < reconstructedBarcodeInfos.Length; i++)
            {
                Vintasoft.Barcode.BarcodeInfo.StructuredAppendBarcodeInfo reconstructedBarcodeInfo = reconstructedBarcodeInfos[i];
                System.Console.WriteLine(string.Format("[{0}:{1}]", i + 1, reconstructedBarcodeInfo.BarcodeType));
                System.Console.WriteLine(string.Format("Value:      {0}", reconstructedBarcodeInfo.Value));
                System.Console.WriteLine();
            }
        }
    }
}

Ահա տվյալների բլոկներով ստեղծված Aztec շտրիխ կոդերից ճանաչման արդյունքը.
1 reconstructed barcode(s) found:

[1:Aztec]
Value: VintaSoft Barcode .NET SDK is the professional 1D & 2D barcode reader and barcode generator library for .NET, WPF and WEB.


Aztec շտրիխ կոդերը չեն պահանջում շտրիխ կոդի շուրջ լռության գոտի, և դա թույլ է տալիս շտրիխ կոդերը միմյանց մոտ տեղադրել: