Genera codici a barre 2D con compressione XFA in .NET

Categoria del blog: Barcode.NET

16.06.2020

I dati del codice a barre, che includono sequenze ricorrenti di simboli (ad esempio un elenco di numeri di serie, dati XML, URL, ecc.), possono essere ridotti di dimensione se i dati del codice a barre vengono compressi utilizzando la compressione Flate (ZIP).
Potrebbe essere necessario ridurre le dimensioni dei dati del codice a barre nei casi in cui si desidera:
VintaSoft Barcode .NET SDK fornisce 2 modi per comprimere i dati del codice a barre bidimensionale.

MODO 1: Comprimi i dati autonomamente e genera un codice a barre bidimensionale con dati binari compressi.Ciò è possibile perché tutti i codici a barre bidimensionali consentono di memorizzare dati binari (per informazioni dettagliate, fare riferimento alla classe BinaryValueItem).

MODO 2: Generare un codice a barre bidimensionale secondo la specifica XML Forms Architecture (XFA). In questo caso, i dati del codice a barre verranno compressi utilizzando l'algoritmo di compressione "DEFLATE Compressed Data Format" (RFC1951).


In questo articolo mostreremo come generare codici a barre bidimensionali secondo la specifica XML Forms Architecture (XFA).

VintaSoft Barcode .NET SDK offre classi che consentono di creare e riconoscere facilmente codici a barre con dati compressi XFA:

Ecco il codice C# che consente di creare un codice a barre Aztec con dati compressi in conformità con le specifiche 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");
    }
}

Ecco un'immagine del codice a barre Aztec che contiene un elenco di URL compressi in base alle specifiche XFA:
Un'immagine del codice a barre Aztec che contiene un elenco di URL compressi secondo la specifica XFA
Ecco un'immagine del codice a barre Aztec che contiene un elenco di URL sotto forma di testo non compresso:
Un'immagine di un codice a barre azteco contenente un elenco di URL sotto forma di testo non compresso

Ecco il codice C# che consente di creare un codice a barre DataMatrix con dati compressi in conformità con la specifica 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");
    }
}

Ecco un'immagine di un codice a barre DataMatrix contenente un elenco di URL compressi in conformità con la specifica XFA:
Un'immagine del codice a barre DataMatrix che contiene un elenco di URL compressi secondo la specifica XFA
Ecco un'immagine del codice a barre DataMatrix che contiene un elenco di URL sotto forma di testo non compresso:
Un'immagine del codice a barre DataMatrix contenente un elenco di URL sotto forma di testo non compresso

Ecco il codice C# che consente di creare un codice a barre PDF417 con dati compressi in conformità con la specifica 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");
    }
}

Ecco un'immagine del codice a barre PDF417 contenente un elenco di URL compressi in base alla specifica XFA:
Un'immagine del codice a barre PDF417 contenente un elenco di URL compressi secondo la specifica XFA
Ecco un'immagine del codice a barre PDF417 che contiene un elenco di URL sotto forma di testo non compresso:
Un'immagine del codice a barre PDF417 che contiene un elenco di URL sotto forma di testo non compresso

Ecco il codice C# che consente di creare un codice a barre QR con dati compressi in conformità con la specifica 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");
    }
}

Ecco un'immagine del codice a barre QR che contiene un elenco di URL compressi secondo la specifica XFA:
Un'immagine di codice a barre QR che contiene un elenco di URL compressi secondo la specifica XFA
Ecco un'immagine di codice a barre QR che contiene un elenco di URL sotto forma di testo non compresso:
Un'immagine di un codice a barre QR che contiene un elenco di URL sotto forma di testo non compresso


Ecco il codice C# che consente di riconoscere i codici a barre bidimensionali generati in conformità con la specifica 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();
}


È opportuno notare che la compressione Flate (ZIP) può aumentare la quantità di dati se la ridondanza dei dati è scarsa o se il flusso di dati è troppo breve.