Riconoscere i codici a barre in macOS utilizzando l'applicazione console .NET

Categoria del blog: Barcode.NETmacOS

25.12.2023

Questo articolo spiega come creare un'applicazione console .NET e riconoscere i codici a barre in un'immagine in macOS. Per il riconoscimento dei codici a barre da un'immagine viene utilizzato VintaSoft Barcode .NET SDK.

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

  2. Creare una cartella in cui verranno salvati i file dell'applicazione .NET. Creiamo la cartella "Recognize_Barcodes_In_Image" sul desktop dell'utente corrente e procediamo alla sua apertura.


  3. Aprire il terminale di comando della console.


  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 "Recognize_Barcodes_In_Image.csproj" e il file "Program.cs", che contiene il codice C# dell'applicazione. Chiudere il terminale.

  5. Aprire il file di progetto "Recognize_Barcodes_In_Image.csproj" nell'editor di testo e modificare il testo del file come segue:
    <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>
    



    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 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();
                        }
                    }
                }
            }
    
        }
    }
    



    Il codice dell'applicazione riconoscerà i codici a barre dall'immagine e restituirà il risultato su una console.

  7. Ottenere il codice per utilizzare la versione di valutazione in macOS seguendo le istruzioni descritte nella documentazione e inserire il codice ottenuto nel codice C# del file "Program.cs".


  8. Copia il file "AllSupportedBarcodes.png" nella cartella del progetto.


    Puoi utilizzare qualsiasi altro file contenente codici a barre al posto del file "AllSupportedBarcodes.png".

  9. Apri il terminale e compila il progetto .NET utilizzando il seguente comando:
    dotnet build Recognize_Barcodes_In_Image.csproj
    



    Chiudi il terminale.

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


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



    Visualizza il risultato del riconoscimento del codice a barre e chiudi il terminale.