Generare un codice a barre in Linux usando un'applicazione .NET

Categoria del blog: Barcode.NETLinux

22.12.2023

Questo articolo spiega come creare un'applicazione console .NET e generare un codice a barre in Ubuntu. Per la generazione di codici a barre viene utilizzato VintaSoft Barcode .NET SDK.

Ecco i passaggi per completare questa operazione:
  1. Aprire il desktop di Ubuntu.

  2. Creare una cartella in cui archiviare i file dell'applicazione .NET. Creiamo la cartella "Generate_Barcode" sul desktop dell'utente corrente e procediamo alla sua creazione.


  3. Aprire il terminale di comando della console. Questo può essere fatto selezionando la voce "Apri nel terminale" nel menu contestuale o premendo la combinazione di tasti Ctrl+Alt+T.


  4. Richiamare il comando nel terminale, che crea un progetto per una nuova applicazione .NET della console:
    dotnet new console --framework net8.0
    



    Il progetto creato contiene il file di progetto "Generate_Barcode.csproj" e il file "Program.cs", che contiene il codice C# dell'applicazione. Chiudere il terminale.

  5. Aprire il file di progetto "Generate_Barcode.csproj" nell'editor di testo e modificare il testo del file con il seguente testo:
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>ConsoleApp1</RootNamespace>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="SkiaSharp" Version="2.88.6" />
        <PackageReference Include="SkiaSharp.NativeAssets.Linux" Version="2.88.6" />
        <PackageReference Include="Vintasoft.Barcode" Version="14.2.0.3" />
        <PackageReference Include="Vintasoft.Barcode.SkiaSharp" Version="14.2.0.3" />
        <PackageReference Include="Vintasoft.Shared" Version="4.1.0.3" />
      </ItemGroup>
    
    </Project>
    



    Il progetto modificato fa riferimento ai pacchetti nuget per VintaSoft Barcode .NET SDK (Vintasoft.Shared.dll, Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll).

  6. Aprire il file "Program.cs" e modificare il suo codice nel seguente codice C#:
    namespace Generate_Barcode
    {
        class Program
        {
            static void Main(string[] args)
            {
                // register the evaluation license for VintaSoft Barcode .NET SDK
                Vintasoft.Barcode.BarcodeGlobalSettings.Register("%EVAL_LIC_USER_NAME%", "%EVAL_LIC_USER_EMAIL%", "%EVAL_LIC_DATE%", "%EVAL_LIC_REG_CODE%");
    
                // Vintasoft.Barcode.SkiaSharp.dll allows to load bitmap from BMP, DNG, GIF, HEIF, JPEG, PNG, WEBP files using image codecs from SkiaSharp library
                Vintasoft.Barcode.SkiaSharpAssembly.Init();
    
                // generate Code128 barcode as a raster image and save to a PNG file
                GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType.Code128, "VintaSoft Barcode .NET SDK", "rasterBarcodeImage.png");
    
                // generate QR barcode as vector image and save to a SVG file
                GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType.QR, "VintaSoft Barcode .NET SDK", "vectorBarcode.svg");
            }
    
            /// <summary>
            /// Generates barcode as raster image and saves to an image file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="pngFilename">The filename, where barcode image must be saved.</param>
            public static void GenerateBarcodeAsRasterImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string pngFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate barcode as raster image and save to a PNG file
                    barcodeWriter.SaveBarcodeAsImage(pngFilename);
                }
            }
    
            /// <summary>
            /// Generates barcode in vector form and saves to a SVG file.
            /// </summary>
            /// <param name="barcodeType">Barcode type.</param>
            /// <param name="barcodeValue">Barcode value.</param>
            /// <param name="svgFilename">The filename, where SVG file must be saved.</param>
            public static void GenerateBarcodeAsVectorImage(Vintasoft.Barcode.BarcodeType barcodeType, string barcodeValue, string svgFilename)
            {
                // create the barcode writer
                using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                {
                    // set barcode writer settings
                    barcodeWriter.Settings.Barcode = barcodeType;
                    barcodeWriter.Settings.Value = barcodeValue;
    
                    // generate SVG file
                    string svgFile = barcodeWriter.GetBarcodeAsSvgFile();
    
                    // save SVG file
                    System.IO.File.WriteAllText(svgFilename, svgFile);
                }
            }
    
        }
    }
    



    Il codice dell'applicazione genererà il codice a barre Code128 come immagine raster e il codice a barre QR in formato vettoriale; i codici a barre generati verranno salvati nei file.

  7. Ottieni il codice per utilizzare la versione di valutazione in Linux seguendo il metodo descritto nella documentazione e inserisci il codice ottenuto nel codice C# del file "Program.cs".


  8. Apri il terminale e compila il progetto .NET utilizzando il seguente comando:
    dotnet build Generate_Barcode.csproj
    



    Chiudi il terminale.

  9. Vai alla cartella "bin/Debug/net8.0/".


  10. Apri il terminale ed esegui l'applicazione .NET utilizzando il seguente comando:
    dotnet ./Generate_Barcode.dll
    



    Chiudi il terminale e guarda i file PNG e SVG generati.