使用 .NET 应用程序在 macOS 中识别图像中的条形码。

博客类别:条形码.NETmacOS

2023/12/25

本文介绍如何在 macOS 系统中创建 .NET 控制台应用程序并识别图像中的条形码。图像条形码识别使用了 VintaSoft Barcode .NET SDK

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

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


  3. 打开控制台命令终端。


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



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

  5. 在文本编辑器中打开项目文件"Recognize_Barcodes_In_Image.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.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>
    
      <ItemGroup>
        <Content Include="AllSupportedBarcodes.png">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    
    </Project>
    



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

  6. 打开文件"Program.cs",并将其代码更改为以下 C# 代码:
    namespace Recognize_Barcodes_In_Image
    {
        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();
    
                // recognize barcodes from image file
                RecognizeBarcodesFromImage("AllSupportedBarcodes.png");
            }
    
            /// <summary>
            /// Recognizes barcodes from an image file.
            /// </summary>
            /// <param name="filename">A path to an image file.</param>
            public static void RecognizeBarcodesFromImage(string filename)
            {
                // create barcode reader
                using (Vintasoft.Barcode.BarcodeReader reader = new Vintasoft.Barcode.BarcodeReader())
                {
                    // specify that reader must search for Code39, Code39Extended, Code128, SSCC18 and DataMatrix barcodes
                    reader.Settings.ScanBarcodeTypes =
                        Vintasoft.Barcode.BarcodeType.Code39 |
                        Vintasoft.Barcode.BarcodeType.Code128 |
                        Vintasoft.Barcode.BarcodeType.DataMatrix;
                    reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.Code39Extended);
                    reader.Settings.ScanBarcodeSubsets.Add(Vintasoft.Barcode.SymbologySubsets.BarcodeSymbologySubsets.SSCC18);
    
                    // specify that reader must search for horizontal and vertical barcodes only
                    reader.Settings.ScanDirection = Vintasoft.Barcode.ScanDirection.Horizontal | Vintasoft.Barcode.ScanDirection.Vertical;
    
                    // use Automatic Recognition
                    reader.Settings.AutomaticRecognition = true;
    
                    // read barcodes from image file
                    Vintasoft.Barcode.IBarcodeInfo[] infos = reader.ReadBarcodes(filename);
    
                    // if barcodes are not detected
                    if (infos.Length == 0)
                    {
                        Console.WriteLine("No barcodes found.");
                    }
                    // if barcodes are detected
                    else
                    {
                        // get information about extracted barcodes
    
                        Console.WriteLine(string.Format("{0} barcodes found:", infos.Length));
                        Console.WriteLine();
                        for (int i = 0; i < infos.Length; i++)
                        {
                            Vintasoft.Barcode.IBarcodeInfo info = infos[i];
                            Console.WriteLine(string.Format("[{0}:{1}]", i + 1, info.BarcodeType));
                            Console.WriteLine(string.Format("Value:      {0}", info.Value));
                            Console.WriteLine(string.Format("Region:     {0}", info.Region));
                            Console.WriteLine();
                        }
                    }
                }
            }
    
        }
    }
    



    应用程序代码将从图像中识别条形码,并将结果输出到控制台。

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


  8. 将"AllSupportedBarcodes.png"文件复制到项目文件夹中。


    您可以使用任何其他包含条形码的文件代替"AllSupportedBarcodes.png"文件。

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



    关闭终端。

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


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



    查看条形码识别结果并关闭终端。