使用 .NET 应用程序在 Linux 中生成条形码

博客类别:条形码.NETLinux

2023/12/22

本文介绍了如何在 Ubuntu 中创建 .NET 控制台应用程序并生成条形码。条形码生成使用了 VintaSoft Barcode .NET SDK

以下是完成此任务的步骤:
  1. 打开 Ubuntu 桌面。

  2. 创建一个文件夹,用于存储 .NET 应用程序的文件。让我们在当前用户的桌面上创建一个名为"Generate_Barcode"的文件夹,并进入该文件夹。


  3. 打开控制台命令终端。可以通过在上下文菜单中选择"在终端中打开"或按组合键 Ctrl+Alt+T 来完成此操作。


  4. 在终端中调用命令,创建一个新的 .NET 控制台应用程序项目:
    dotnet new console --framework net8.0
    



    创建的项目包含项目文件"Generate_Barcode.csproj"和包含应用程序 C# 代码的"Program.cs"文件。关闭终端。

  5. 在文本编辑器中打开项目文件"Generate_Barcode.csproj",并将文件内容更改为以下内容:
    <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>
    



    修改后的项目引用了 VintaSoft Barcode .NET SDK 的 NuGet 包(Vintasoft.Shared.dll、Vintasoft.Barcode.dll 和 Vintasoft.Barcode.SkiaSharp.dll)。

  6. 打开文件"Program.cs",并将其代码更改为以下 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);
                }
            }
    
        }
    }
    



    应用程序代码将生成 Code128 条形码(光栅图像)和 QR 条形码(矢量格式),生成的条形码将保存到文件中。

  7. 按照文档中描述的方法获取在 Linux 中使用评估版本的代码,并将获得的代码插入到"Program.cs"文件的 C# 代码中。


  8. 打开终端并使用以下命令编译 .NET 项目:
    dotnet build Generate_Barcode.csproj
    



    关闭终端。

  9. 转到"bin/Debug/net8.0/"文件夹。


  10. 打开终端并使用以下命令运行 .NET 应用程序:
    dotnet ./Generate_Barcode.dll
    



    关闭终端并查看生成的 PNG 和 SVG 文件。