.NET アプリケーションを使用して Linux でイメージ内のバーコードを認識する

ブログ カテゴリ: バーコード.NETLinux

2023/12/15

この記事では、Ubuntu でコンソール .NET アプリケーションを作成し、イメージ内のバーコードを認識する方法について説明します。画像からのバーコード認識には、VintaSoft Barcode .NET SDKを使用します。

タスクを完了する手順は次のとおりです。
  1. Ubuntu デスクトップを開きます。

  2. .NETアプリケーションのファイルを保存するフォルダを作成します。現在のユーザーのデスクトップに「Recognize_Barcodes_In_Image」フォルダを作成し、そのフォルダに移動します。


  3. コンソール コマンド ターミナルを開きます。これは、コンテキスト メニューの [ターミナルで開く] 項目を選択するか、Ctrl+Alt+T キーの組み合わせを押すことで実行できます。


  4. ターミナルでコマンドを呼び出し、新しいコンソール .NET アプリケーションのプロジェクトを作成します。
    dotnet new console --framework net8.0
    



    作成されたプロジェクトには、プロジェクトファイル「Recognize_Barcodes_In_Image.csproj」と、アプリケーションのC#コードを含む「Program.cs」ファイルが含まれています。ターミナルを閉じます。

  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.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>
    
      <ItemGroup>
        <Content Include="AllSupportedBarcodes.png">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </Content>
      </ItemGroup>
    
    </Project>
    



    変更されたプロジェクトは、VintaSoft Barcode .NET SDK (Vintasoft.Shared.dll、Vintasoft.Barcode.dll、Vintasoft.Barcode.SkiaSharp.dll) の NuGet パッケージを参照します。

  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. ドキュメント に記載されている方法を使用して、Linux で評価版を使用するためのコードを取得し、取得したコードを "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
    



    バーコード認識結果を確認し、ターミナルを閉じます。