Barcode Xpress for .NET Core v14.0 - Updated
Getting Started / Create Your First Project (Command Line)
In This Topic
    Create Your First Project (Command Line)
    In This Topic

    Introduction

    This step-by-step tutorial will guide you through how to build a complete C# application that analyzes 1D barcodes using the command line and a text editor, like Visual Studio Code.

    Steps

    1. Create a new console application project from the command line by creating a directory for your project and initializing it with the dotnet new command.

      mkdir MyProject
      dotnet new console
      
    2. Add the necessary dependencies to the generated project file MyProjectcsproj: We can add both required dependencies for this project through the command line:

      dotnet add package Accusoft.BarcodeXpress.NetCore
      

      If you would like to use a local copy of Barcode Xpress instead of fetching one from Nuget, you can use an ItemGroup containing a reference instead of the PackageReference created by adding the packages via NuGet:

      <ItemGroup>
         <Reference Include="Accusoft.BarcodeXpress.NetCore">      
         <HintPath>..\BarcodeXpress\Accusoft.BarcodeXpress14.NetCore.dll</HintPath>
         </Reference>
      </ItemGroup>
      

      Additionally, add the using statement for BarcodeXpressSdk to the top of your generated Program class:

      using System;
      using Accusoft.BarcodeXpressSdk;
      namespace MyProject
       {
      
    3. Create an instance of the BarcodeXpress class:

      namespace MyProject
      {
          class Program
          {
              static void Main(string[] args)
              {
                  BarcodeXpress barcodeXpress = new BarcodeXpress(".");
      
                       }
          }
      }
      
    4. Open an image and pass it to the SDK’s Analyze method:

      namespace MyProject
      {
          class Program
          {
              static void Main(string[] args)
              {
                  BarcodeXpress barcodeXpress = new BarcodeXpress(".");
                  using (Stream stream = File.OpenRead("barcode.bmp"))
                  {
                       Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(stream);
                  }
      
      
      
                                                          }
          }
      }
      
    5. Access the fields of returned result objects to get data about barcodes detected in the image:

      namespace MyProject
      {
          class Program
          {
              static void Main(string[] args)
              {
                  BarcodeXpress barcodeXpress = new BarcodeXpress(".");
                  using (Stream stream = File.OpenRead("barcode.bmp"))
                  {
                       Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(stream);
                       if (results.Length > 0)
                       {
                           foreach (Accusoft.BarcodeXpressSdk.Result result in results)
                           {
                               Console.WriteLine("{0} : {1}", result.BarcodeType.ToString(), result.BarcodeValue);
                           }
                       }
                       else
                       {
                           Console.WriteLine("No Barcodes Found.");
                       }
                  }                  
              }
          }
      }
      

      BarcodeXpress.reader.Analyze returns an array of Result objects which contain information about all barcodes detected on the page. You can iterate over that array to access all known properties of each barcode.

    6. Finally, clean up the Barcode Xpress object by releasing unmanaged resources using Dispose() method:

      namespace MyProject
      {
          class Program
          {
              static void Main(string[] args)
              {
                  BarcodeXpress barcodeXpress = new BarcodeXpress(".");
                  using (Stream stream = File.OpenRead("barcode.bmp"))
                  {
                       Accusoft.BarcodeXpressSdk.Result[] results = barcodeXpress.reader.Analyze(stream);
                       if (results.Length > 0)
                       {
                           foreach (Accusoft.BarcodeXpressSdk.Result result in results)
                           {
                               Console.WriteLine("{0} : {1}", result.BarcodeType.ToString(), result.BarcodeValue);
                           }
                       }
                       else
                       {
                           Console.WriteLine("No Barcodes Found.");
                       }
                  }
                  barcodeXpress.Dispose();                  
              }
          }
      }