Gere códigos de barras 2D com compressão XFA em .NET

Categoria do blog: Código de barras.NET

16.06.2020

Os dados do código de barras, que incluem sequências recorrentes de símbolos (por exemplo, uma lista de números de série, dados XML, URLs, etc.), podem ter seu tamanho reduzido se forem compactados usando a compressão Flate (ZIP).
Reduzir o tamanho dos dados do código de barras pode ser necessário caso você queira:
VintaSoft Barcode .NET SDK oferece 2 maneiras de compactar dados de códigos de barras bidimensionais.

MÉTODO 1: Comprima os dados você mesmo e gere um código de barras bidimensional com dados binários comprimidos. Isso é possível porque todos os códigos de barras bidimensionais permitem armazenar dados binários (para obter informações detalhadas, consulte a classe BinaryValueItem).

MÉTODO 2: Gere um código de barras bidimensional de acordo com a especificação XML Forms Architecture (XFA). Neste caso, os dados do código de barras serão comprimidos usando o algoritmo de compressão "DEFLATE Compressed Data Format" (RFC1951).


Neste artigo, mostraremos como gerar códigos de barras bidimensionais de acordo com a especificação XML Forms Architecture (XFA).

VintaSoft Barcode .NET SDK oferece classes que permitem criar e reconhecer facilmente códigos de barras com dados compactados em XFA:

Aqui está um código C# que permite criar um código de barras Aztec com dados compactados em conformidade com a especificação XFA:
/// <summary>
/// Generates Aztec barcode with XFA compression.
/// </summary>
public void GenerateAztecBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed Aztec barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedAztecBarcodeSymbology xfaCompressedAztecBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedAztecBarcodeSymbology();
    // encode barcode text using XFA compressed Aztec barcode symbology
    xfaCompressedAztecBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

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

Aqui está uma imagem de um código de barras Aztec que contém uma lista de URLs compactadas de acordo com a especificação XFA:
An image of Aztec barcode that contains a list of URLs compressed according to the XFA specification
Aqui está uma imagem de um código de barras Aztec que contém uma lista de URLs na forma de texto não compactado:
An image of Aztec barcode that contains a list of URLs in the form of uncompressed text

Aqui está um código C# que permite criar um código de barras DataMatrix com dados compactados em conformidade com a especificação XFA:
/// <summary>
/// Generates DataMatrix barcode with XFA compression.
/// </summary>
public void GenerateDataMatrixBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed DataMatrix barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedDataMatrixBarcodeSymbology xfaCompressedDataMatrixBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedDataMatrixBarcodeSymbology();
    // encode barcode text using XFA compressed DataMatrix barcode symbology
    xfaCompressedDataMatrixBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

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

Aqui está uma imagem de um código de barras DataMatrix que contém uma lista de URLs compactadas de acordo com a especificação XFA:
An image of DataMatrix barcode that contains a list of URLs compressed according to the XFA specification
Aqui está uma imagem de um código de barras DataMatrix que contém uma lista de URLs na forma de texto não compactado:
An image of DataMatrix barcode that contains a list of URLs in the form of uncompressed text

Aqui está um código C# que permite Para criar um código de barras PDF417 com dados compactados em conformidade com a especificação XFA:
/// <summary>
/// Generates PDF417 barcode with XFA compression.
/// </summary>
public void GeneratePdf417BarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed PDF417 barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedPDF417BarcodeSymbology xfaCompressedPdf417BarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedPDF417BarcodeSymbology();
    // encode barcode text using XFA compressed PDF417 barcode symbology
    xfaCompressedPdf417BarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

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

Aqui está uma imagem de um código de barras PDF417 que contém uma lista de URLs compactadas de acordo com a especificação XFA:
An image of PDF417 barcode that contains a list of URLs compressed according to the XFA specification
Aqui está uma imagem de um código de barras PDF417 que contém uma lista de URLs na forma de texto não compactado:
An image of PDF417 barcode that contains a list of URLs in the form of uncompressed text

Aqui está o código C# que permite criar um código de barras QR com dados compactados em conformidade com a especificação XFA:
/// <summary>
/// Generates QR barcode with XFA compression.
/// </summary>
public void GenerateQrCodeBarcodeWithXfaCompression()
{
    string barcodeText =
        "https://www.vintasoft.com/vsbarcode-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vstwain-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsimaging-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsannotation-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vspdf-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjbig2-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsjpeg2000-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdoccleanup-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsocr-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsdicom-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsformsprocessing-dotnet-index.html" + System.Environment.NewLine +
        "https://www.vintasoft.com/vsoffice-dotnet-index.html";

    // create the barcode writer
    Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter();

    // create the XFA compressed QR barcode symbology
    Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedQRCodeBarcodeSymbology xfaCompressedQrCodeBarcodeSymbology =
        new Vintasoft.Barcode.SymbologySubsets.XFACompressed.XFACompressedQRCodeBarcodeSymbology();
    // encode barcode text using XFA compressed QR barcode symbology
    xfaCompressedQrCodeBarcodeSymbology.Encode(barcodeText, barcodeWriter.Settings);

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

Aqui está uma imagem de um código de barras QR que contém uma lista de URLs compactadas de acordo com a especificação XFA:
An image of QR barcode that contains a list of URLs compressed according to the XFA specification
Aqui está uma imagem de um código de barras QR que contém uma lista de URLs na forma de texto não compactado:
An image of QR barcode that contains a list of URLs in the form of uncompressed text


Aqui está o código C# que permite reconhecer códigos de barras bidimensionais gerados em conformidade com a especificação XFA:
/// <summary>
/// Recognizes 2D barcode with XFA compressed data.
/// </summary>
/// <param name="barcodeImageFile">A path to a barcode image file.</param>
public void Recognize2DBarcodeWithXfaCompressedData(string barcodeImageFile)
{
    // create barcode reader
    using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
    {
        // specify that reader must search for XFA compressed Aztec barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedAztec);
        // specify that reader must search for XFA compressed DataMatrix barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedDataMatrix);
        // specify that reader must search for XFA compressed PDF417 barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedPDF417);
        // specify that reader must search for XFA compressed QR barcodes
        reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.XFACompressedQRCode);

        // 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(barcodeImageFile);

        // 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();
            }
        }
    }
    System.Console.WriteLine();
    System.Console.WriteLine();
}


Deve-se observar que a compressão plana (ZIP) pode aumentar a quantidade de dados se houver pouca redundância de dados ou se o fluxo de dados for muito curto.