Générer un code-barres sous macOS à l'aide d'une application .NET

Catégorie du blog: Code-barres.NETmacOS

25.12.2023

Cet article explique comment créer une application console .NET et générer des codes-barres sous macOS. Le VintaSoft Barcode .NET SDK est utilisé pour la génération des codes-barres.

Voici les étapes à suivre:
  1. Ouvrir le bureau macOS.

  2. Créez un dossier qui stockera les fichiers de l'application .NET. Créons le dossier "Generate_Barcode" sur le bureau de l'utilisateur actuel et accédons-y.


  3. Ouvrir le terminal console.


  4. Exécutez la commande dans le terminal pour créer un projet d'application console .NET:
    dotnet new console --framework net8.0
    



    Le projet créé contient le fichier projet "Generate_Barcode.csproj" et le fichier "Program.cs", qui contient le code C# de l'application. Fermez le terminal.

  5. Ouvrez le fichier projet "Generate_Barcode.csproj" dans un éditeur de texte et modifiez son contenu comme suit:
    <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.macOS" 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>
    



    Le projet modifié référence les packages NuGet du VintaSoft Barcode .NET SDK (Vintasoft.Shared.dll, Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll).

  6. Ouvrez le fichier "Program.cs" et modifiez son code pour utiliser le code C# suivant:
    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);
                }
            }
    
        }
    }
    



    L'application générera un code-barres Code128 sous forme d'image raster et un code-barres QR au format vectoriel. Les codes-barres générés seront enregistrés dans des fichiers.

  7. Obtenez le code pour utiliser la version d'évaluation sous macOS en suivant la méthode décrite dans la documentation et insérez le code obtenu dans le code C# du fichier "Program.cs".


  8. Ouvrez le terminal et compilez le projet .NET à l’aide de la commande suivante:
    dotnet build Generate_Barcode.csproj
    



    Fermez le terminal.

  9. Accédez au dossier "bin/Debug/net8.0/".


  10. Ouvrez le terminal et exécutez l'application .NET à l'aide de la commande suivante:
    dotnet ./Generate_Barcode.dll
    



    Fermez le terminal pour visualiser les fichiers PNG et SVG générés.