VintaSoft Barcode .NET SDK 15.1: Documentation for .NET developer
In This Topic
    Generate barcode image in MAUI application for Android
    In This Topic
    This tutorial shows how to create a blank MAUI application in Visual Studio .NET 2022, generate barcode image and recognize barcode in generated image in MAUI application for Android.

    Here are steps, which must be done:
    1. Create a blank MAUI application.

      Open Visual Studio .NET 2022 and create a new project, of .NET MAUI application type:

      Configure the project to use .NET 8.0:

    2. Specify that MAUI application will be used in Android.

      Open project file and select target frameworks: Android.

    3. Add references to the Vintasoft assemblies to MAUI application.

      Add references to the Vintasoft.Barcode.dll, Vintasoft.Barcode.SkiaSharp.dll and Vintasoft.Shared.dll assemblies from "<InstallPath>\VintaSoft Barcode .NET 15.1\Bin\DotNet8\AnyCPU\" folder in MAUI application.
      Comment: Reference to Vintasoft.Barcode.SkiaSharp.dll assembly is necessary only if SDK should draw text value of barcode on barcode image. Vintasoft.Barcode.ImageSharp.dll can be used instead of Vintasoft.Barcode.SkiaSharp.dll assembly.



      Add reference to the SkiaSharp nuget-package in MAUI application.

    4. Specify that .NET-compiler should not trim/minimize Vintasoft assemblies and Mono.Android assembly.

      Open MAUI project file and add project items, which allow to specify that .NET-compiler should not trim/minimize Vintasoft assemblies and Mono.Android assembly if .NET-assemblies of MAUI application must be trimmed/minimized.


      Here is project items, which allow to specify that .NET-compiler should not trim/minimize Vintasoft assemblies and Mono.Android assembly if .NET-assemblies of MAUI application must be trimmed/minimized:
      <ItemGroup>
              <TrimmerRootAssembly Include="Vintasoft.Barcode" RootMode="library" />
              <TrimmerRootAssembly Include="Vintasoft.Barcode.SkiaSharp" RootMode="library" />
              <TrimmerRootAssembly Include="Vintasoft.Shared" RootMode="library" />
              <TrimmerRootAssembly Include="Mono.Android" RootMode="library" />
      </ItemGroup>
      
      
    5. Add C# code that registers the evaluation version for VintaSoft Barcode .NET SDK.

      Get the C# code for using evaluation version in Android using the way described in documentation and insert the obtained code into C# code of "MainPage.xaml.cs" file.


      Here is C# code that registers the evaluation version for VintaSoft Barcode .NET SDK:
      // get the application name
      string applicationName = Vintasoft.Barcode.BarcodeGlobalSettings.ApplicationName;
      // if MAUI application is using in Android, Linux or macOS
      if (applicationName.StartsWith("Linux") || applicationName.StartsWith("Android"))
      {
          // register the evaluation license for VintaSoft Barcode .NET SDK
          Vintasoft.Barcode.BarcodeGlobalSettings.Register("LINUX_EVAL_USER", "LINUX_EVAL_USER_EMAIL", "LINUX_EVAL_END_DATE", "LINUX_EVAL_REG_CODE");
      }
      
      
    6. Add C# code that generates barcode image and recognizes barcode in generated image.

      Open file "MainPage.xaml.cs" and add C# code, which generates barcode image and recognizes barcode in generated image, to the OnCounterClicked method.


      Here is C# code that generates barcode image and recognizes barcode in generated image:
      namespace MauiApp1
      {
          public partial class MainPage : ContentPage
          {
              int count = 0;
      
              public MainPage()
              {
                  InitializeComponent();
              }
      
              private void OnCounterClicked(object sender, EventArgs e)
              {
                  // get the application name
                  string applicationName = Vintasoft.Barcode.BarcodeGlobalSettings.ApplicationName;
                  // if MAUI application is using in Android, Linux or macOS
                  if (applicationName.StartsWith("Linux") || applicationName.StartsWith("Android"))
                  {
                      // register the evaluation license for VintaSoft Barcode .NET SDK
                      Vintasoft.Barcode.BarcodeGlobalSettings.Register("LINUX_EVAL_USER", "LINUX_EVAL_USER_EMAIL", "LINUX_EVAL_END_DATE", "LINUX_EVAL_REG_CODE");
                  }
      
                  // create the barcode writer
                  using (Vintasoft.Barcode.BarcodeWriter barcodeWriter = new Vintasoft.Barcode.BarcodeWriter())
                  {
                      // specify that barcode writer must generate DataMatrix barcode
                      barcodeWriter.Settings.Barcode = Vintasoft.Barcode.BarcodeType.DataMatrix;
                      // specify value for DataMatrix barcode
                      barcodeWriter.Settings.Value = "12345";
      
                      // create memory stream that will store generated barcode image as PNG file
                      MemoryStream stream = new MemoryStream();
                      // generate barcode image and save as PNG file to the memory stream
                      barcodeWriter.SaveBarcodeAsImage(stream, "png");
                      stream.Position = 0;
      
                      // create barcode reader
                      using  (Vintasoft.Barcode.BarcodeReader barcodeReader = new Vintasoft.Barcode.BarcodeReader())
                      {
                          // specify that barcode reader must recognize DataMatrix barcodes
                          barcodeReader.Settings.ScanBarcodeTypes = Vintasoft.Barcode.BarcodeType.DataMatrix;
                          // recognize barcodes in PNG file that is stored in memory stream
                          Vintasoft.Barcode.IBarcodeInfo[] barcodeInfos = barcodeReader.ReadBarcodes(stream);
                          // if barcode is not recognized
                          if (barcodeInfos.Length == 0)
                          {
                              DisplayAlert("Barcode Reader", "Barcode is not recognized.", "OK");
                          }
                          // if barcode is recognized
                          else
                          {
                              // display information about recognized DataMatrix barcode
                              DisplayAlert(
                                  "Barcode Reader",
                                  string.Format("Barcode is recognized: Type='{0}', Value='{1}'",
                                      barcodeInfos[0].BarcodeType,
                                      barcodeInfos[0].Value),
                                  "OK");
                          }
                      }
                  }
      
                  count++;
                  if (count == 1)
                      CounterBtn.Text = $"Clicked {count} time";
                  else
                      CounterBtn.Text = $"Clicked {count} times";
      
                  SemanticScreenReader.Announce(CounterBtn.Text);
              }
          }
      
      }
      
      
    7. Sign the MAUI application assembly using strong name file.

      If you have strong name file, please sign the MAUI application assembly using strong name file.


      If you do not have the strong name file, please read how to generate strong name file here.
    8. Run MAUI application in Android emulator and see the result.