Recognize barcodes in image in macOS using a .NET application

Blog category: Barcode.NETmacOS

December 25, 2023

This article explains how to create a console .NET application and recognize barcodes in image in macOS. For barcode recognition from image is used VintaSoft Barcode .NET SDK.

Here are the steps to complete that task:
  1. Open macOS desktop.

  2. Create a folder, which will store the files of .NET application. Let us create "Recognize_Barcodes_In_Image" folder on current user's desktop and proceed to the folder.


  3. Open the console command terminal.


  4. Call the command in terminal, which creates a project of new console .NET application:
    dotnet new console --framework net8.0
    



    The created project contains the project file "Recognize_Barcodes_In_Image.csproj" and "Program.cs" file, which contains C# code of application. Close terminal.

  5. Open the project file "Recognize_Barcodes_In_Image.csproj" in text editor and change the file text to the following text:
    <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>
    



    The changed project references nuget-packages for VintaSoft Barcode .NET SDK (Vintasoft.Shared.dll, Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll).

  6. Open the file "Program.cs" and change its code to the following C# code:
    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();
                        }
                    }
                }
            }
    
        }
    }
    



    The application code will recognize barcodes from image and output result to a console.

  7. Get the code for using evaluation version in macOS using the way described in documentation and insert the obtained code into C# code of "Program.cs" file.


  8. Copy "AllSupportedBarcodes.png" file to the project folder.


    You can use any other file containing barcodes instead of "AllSupportedBarcodes.png" file.

  9. Open terminal and compile the .NET project using the following command:
    dotnet build Recognize_Barcodes_In_Image.csproj
    



    Close the terminal.

  10. Go to "bin/Debug/net8.0/" folder.


  11. Open the terminal and run the .NET application using the following command:
    dotnet ./Recognize_Barcodes_In_Image.dll
    



    See the barcode recognition result and close the terminal.