Generate barcode image in MAUI application for Android

Blog category: Barcode.NETAndroid

May 7, 2024

This article explains how to create a MAUI application and generate barcode image in Android. For barcode generation is used VintaSoft Barcode .NET SDK.

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 Barcode nuget-packages to MAUI application.
    Add references to the Vintasoft.Barcode nuget-package and Vintasoft.Barcode.SkiaSharp nuget-package to the MAUI application.
    Reference to Vintasoft.Barcode.SkiaSharp nuget-package is necessary only if SDK should draw text value of barcode on barcode image. Vintasoft.Barcode.ImageSharp nuget-package can be used instead of Vintasoft.Barcode.SkiaSharp nuget-package.



    Add reference to the SkiaSharp nuget-package to the 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 an UI-control for displaying generated barcode image, button for generating barcode image.
    Open XAML layout of Main page in "MainPage.xaml" file and add an UI-control for displaying generated barcode image, button for generating barcode image.


    Here is XAML code of Main page (page contains an UI-control for displaying generated barcode image, button for generating barcode image):
    <?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="MauiApp1.MainPage">
        <ScrollView>
            <VerticalStackLayout
                Padding="30,0"
                Spacing="25">
                <Image x:Name="barcodeImage" HeightRequest="300" Aspect="AspectFit"/>
    
                <Button
                    x:Name="GenerateBarcodeBtn"
                    Text="Generate Barcode" 
                    Clicked="OnGenerateBarcodeClicked"
                    HorizontalOptions="Fill" />
            </VerticalStackLayout>
        </ScrollView>
    </ContentPage>
    
  6. 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
    if (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");
    }
    
  7. Add C# code that generates barcode image.
    Open file "MainPage.xaml.cs" and add C# code, which generates barcode image, to the OnGenerateBarcodeClicked method.


    Here is C# code that generates barcode image and displays generated image in Image-control:
    namespace MauiApp1
    {
        public partial class MainPage : ContentPage
        {
            int count = 0;
    
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void OnGenerateBarcodeClicked(object sender, EventArgs e)
            {
                // get the application name
                string applicationName = Vintasoft.Barcode.BarcodeGlobalSettings.ApplicationName;
                // if MAUI application is using in Android
                if (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;
    
                    barcodeImage.Source = ImageSource.FromStream(() => stream);
                }
            }
        }
    }
    
  8. 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.
  9. Run MAUI application in Android emulator and see the result.