Generar códigos de barras 2D con compresión XFA en .NET

Categoría del blog: Barcode.NET

16.06.2020

Datos de código de barras,que incluyen secuencias recurrentes de símbolos (por ejemplo, una lista de números de serie, datos XML, URL, etc.), se pueden reducir en tamaño si los datos del código de barras se comprimen utilizando la compresión Flate (ZIP).
Reducir el tamaño de los datos del código de barras puede ser necesario en caso de que desee:
VintaSoft Barcode .NET SDK proporciona dos maneras de comprimir datos de códigos de barras bidimensionales.

MÉTODO 1: Comprima los datos usted mismo y genere un código de barras bidimensional con datos binarios comprimidos. Esto es posible porque todos los códigos de barras bidimensionales le permiten almacenar datos binarios (para obtener información detallada, consulte la clase BinaryValueItem).

MÉTODO 2: Generar un código de barras bidimensional según la especificación de XML Forms Architecture (XFA). En este caso, los datos del código de barras se comprimirán mediante el algoritmo de compresión "DEFLATE Compressed Data Format" (RFC1951).


En este artículo, mostraremos cómo generar códigos de barras bidimensionales según la especificación de XML Forms Architecture (XFA).

VintaSoft Barcode .NET SDK ofrece clases que permiten crear y reconocer fácilmente códigos de barras con datos comprimidos XFA:

Aquí tiene código C# que le permite crear un código de barras Aztec con datos comprimidos de acuerdo con la especificación 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");
    }
}

Aquí tiene una imagen de un código de barras Aztec que contiene una lista de URL comprimidas de acuerdo con la especificación XFA:
Una imagen de código de barras Aztec que contiene una lista de URL comprimidas según la especificación XFA
Aquí hay una imagen de código de barras Aztec que contiene una lista de URL en forma de texto sin comprimir:
Una imagen de código de barras Aztec que contiene una lista de URL en forma de texto sin comprimir

Aquí hay código C# que le permite crear un código de barras DataMatrix con datos comprimidos de conformidad con la especificación 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");
    }
}

Aquí hay una imagen de un código de barras DataMatrix que contiene una lista de URL comprimidas según la especificación XFA:
Una imagen de código de barras DataMatrix que contiene una lista de URL comprimidas según la especificación XFA
Aquí hay una imagen de un código de barras DataMatrix que contiene una lista de URL en forma de texto sin comprimir:
Una imagen de un código de barras DataMatrix que contiene una lista de URL en forma de texto sin comprimir

Aquí hay un código C# que le permite crear un código de barras PDF417 con datos comprimidos de acuerdo con la especificación 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");
    }
}

Aquí hay una imagen de un código de barras PDF417 que contiene una lista de URL comprimidas según la especificación XFA:
Una imagen de código de barras PDF417 que contiene una lista de URL comprimidas según la especificación XFA
Aquí hay una imagen de código de barras PDF417 que contiene una lista de URL en forma de texto sin comprimir:
Una imagen de código de barras PDF417 que contiene una lista de URL en forma de texto sin comprimir

Aquí hay un código C# que le permite crear un código de barras QR con datos comprimidos de acuerdo con la especificación 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");
    }
}

Aquí hay una imagen de código de barras QR que contiene una lista de URL comprimidas según la especificación XFA:
Una imagen de código de barras QR que contiene una lista de URL comprimidas según la especificación XFA
Aquí hay una imagen de un código de barras QR que contiene una lista de URL en forma de texto sin comprimir:
Una imagen de un código de barras QR que contiene una lista de URL en forma de texto sin comprimir


Aquí hay un código C# que le permite reconocer códigos de barras bidimensionales generados de conformidad con la especificación 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();
}


Cabe señalar que la compresión Flate (ZIP) puede aumentar la cantidad de datos si hay poca redundancia de datos o si el flujo de datos es demasiado corto.