.NET에서 필요한 모양의 아즈텍 바코드 생성

블로그 카테고리: 바코드.NET

2020/06/03

때때로 바코드를 생성하고 그 안에 인코딩할 데이터의 크기가 바코드 용량을 초과하여 모든 데이터를 하나의 바코드에 담을 수 없는 경우가 있습니다. 이 문제를 해결하려면 원본 데이터를 여러 데이터 블록으로 나누고 각 데이터 블록을 여러 개의 바코드로 인코딩해야 합니다. 또한, 나중에 원본 데이터로 다시 결합할 수 있도록 데이터 블록에 표시를 해야 합니다.

2차원 바코드용 구조 추가(Structure Append) 기술을 사용하면 원본 데이터를 데이터 블록으로 분할하고 각 데이터 블록에서 원래 데이터를 복원할 수 있습니다.
DataMatrix 및 QR 코드 바코드용 Structure Append 기술을 사용하면 원본 데이터를 최대 16개의 데이터 블록으로 분할하고 이러한 데이터 블록을 16개의 DataMatrix 또는 QR 코드 바코드로 인코딩할 수 있습니다.
아즈텍 바코드의 경우 원본 데이터를 최대 26개의 데이터 블록으로 분할하여 26개의 아즈텍 바코드로 인코딩할 수 있습니다.
PDF417 바코드의 경우, 구조 추가 기술을 사용하면 원본 데이터를 99999개의 데이터 블록으로 분할할 수 있습니다.
Code16K 바코드의 경우, Structure Append 기술을 사용하면 9개의 데이터 블록을 사용할 수 있습니다.

데이터 블록이 있는 각 바코드에는 소스 데이터에서 해당 데이터 블록의 위치가 포함되어 있으므로 이미지에서 데이터 블록이 있는 바코드는 임의의 순서로 배열될 수 있습니다. 이 기능을 사용하여 이미지에 바코드를 가장 최적의 방식으로 배치할 수 있습니다.

다음은 "VintaSoft Barcode .NET SDK는 .NET, WPF 및 웹용 전문가용 1D 및 2D 바코드 판독기 및 바코드 생성기 라이브러리입니다."라는 텍스트가 포함된 아즈텍 바코드를 생성하는 C# 코드입니다.
/// <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 barcode

다음은 생성된 아즈텍 바코드에 인코딩된 데이터를 읽는 C# 코드입니다.
/// <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();
            }
        }
    }
}

다음은 생성된 아즈텍 바코드에서 데이터를 읽은 결과입니다.
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.


아즈텍 바코드는 정사각형 모양이므로 바코드를 직사각형 영역에 배치해야 하는 경우 이미지 공간을 최적으로 사용하지 못할 수 있습니다.

따라서 바코드 텍스트를 하나의 바코드 대신 4개의 데이터 블록으로 나누고, 이 4개의 바코드를 가로로 배치하여 바코드 배치를 위해 지정된 직사각형 영역을 최적으로 사용합니다.

다음은 "VintaSoft Barcode .NET SDK는 .NET, WPF 및 웹용 전문 1D 및 2D 바코드 리더 및 바코드 생성기 라이브러리입니다."라는 텍스트를 분할하는 C# 코드입니다. 4개의 데이터 블록으로 나누고, 이 데이터 블록을 사용하여 4개의 아즈텍 바코드를 생성한 다음, 아즈텍 바코드를 가로로 배치합니다.
/// <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 barcodes with Structure Append and aligned horizontally

데이터 블록이 포함된 아즈텍 바코드 이미지는 직사각형 모양이므로 직사각형 영역에 최적으로 배치할 수 있습니다.

다음은 생성된 아즈텍 바코드에서 데이터를 인식하고 데이터 블록을 원래 데이터로 결합하는 C# 코드입니다.
/// <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();
            }
        }
    }
}

다음은 생성된 아즈텍 바코드에서 데이터 블록을 인식한 결과입니다.
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.


아즈텍 바코드는 바코드 주변에 여백이 필요하지 않으므로 바코드를 서로 가깝게 배치할 수 있습니다.