Recognize barcode in image in MAUI application for Android

Blog category: Barcode.NETAndroid

May 8, 2024

This article explains how to create a MAUI application, load image from image gallery and recognize barcodes in image in Android. For barcode recognition 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 reference to the Vintasoft Barcode nuget-package to MAUI application.
    Add reference to the Vintasoft.Barcode 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.Shared" RootMode="library" />
        <TrimmerRootAssembly Include="Mono.Android" RootMode="library" />
    </ItemGroup>
    
  5. Add an UI-control for displaying opened image, button for opening image from image gallery, button for recognizing barcodes in opened image.
    Open XAML layout of Main page in "MainPage.xaml" file and add an UI-control for displaying opened image, button for opening image from image gallery, button for recognizing barcodes in opened image.


    Here is XAML code of Main page (page contains an UI-control for displaying opened image, button for opening image from image gallery, button for recognizing barcodes in opened 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="500" Aspect="AspectFit"/>
                <HorizontalStackLayout>
                    <Button x:Name="openBarcodeButton" Text="Open Image..." Clicked="openBarcodeButton_Clicked" Margin="2"/>
                    <Button x:Name="recognizeBarcodeButton" Text="Recognize Barcode" Clicked="recognizeBarcodeButton_Clicked" Margin="2"/>
                </HorizontalStackLayout>
            </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 opens image from image gallery and recognizes barcodes in opened image.
    Open file "MainPage.xaml.cs" and add C# code, which opens image from image gallery, to the openBarcodeButton_Clicked method.
    Open file "MainPage.xaml.cs" and add C# code, which recognizes barcodes in opened image, to the recognizeBarcodeButton_Clicked method.


    Here is C# code that opens image from image gallery and recognizes barcodes in opened image:
    using Vintasoft.Barcode;
    
    namespace MauiApp1
    {
        public partial class MainPage : ContentPage
        {
    
            /// <summary>
            /// The current image stream.
            /// </summary>
            Stream _currentImageStream;
    
    
    
            public MainPage()
            {
                InitializeComponent();
            }
    
    
    
            private async void openBarcodeButton_Clicked(object sender, EventArgs e)
            {
                PickOptions options = new PickOptions();
                options.FileTypes = FilePickerFileType.Images;
                var result = await FilePicker.Default.PickAsync(options);
                if (result != null)
                {
                    LoadImage(File.OpenRead(result.FullPath));
                }
            }
    
            private async void recognizeBarcodeButton_Clicked(object sender, EventArgs e)
            {
                try
                {
                    recognizeBarcodeButton.IsEnabled = false;
    
                    if (_currentImageStream == null)
                        throw new Exception("Barcode image is not loaded.");
    
                    using (BarcodeReader reader = new BarcodeReader())
                    {
                        reader.Settings.ScanBarcodeTypes = BarcodeType.DataMatrix;
                        reader.Settings.BarcodeCharacteristics = BarcodeCharacteristics.NormalSizeBarcodes;
    
                        _currentImageStream.Position = 0;
                        IBarcodeInfo[] infos = reader.ReadBarcodes(_currentImageStream);
                        if (infos.Length > 0)
                        {
                            IBarcodeInfo info = infos[0];
                            await DisplayAlert("Barcode Reader", $"Recognized '{info.BarcodeType}' barcode ({reader.RecognizeTime.TotalMilliseconds} ms):\n{info.Value}", "OK");
                        }
                        else
                        {
                            throw new Exception($"No barcodes found ({reader.RecognizeTime.TotalMilliseconds} ms).");
                        }
                    }
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Barcode Reader Error", ex.Message, "OK");
                }
                finally
                {
                    recognizeBarcodeButton.IsEnabled = true;
                }
            }
    
            /// <summary>
            /// Loads the image.
            /// </summary>
            /// <param name="stream">The image stream.</param>
            private async void LoadImage(Stream stream)
            {
                if (_currentImageStream != null)
                    _currentImageStream.Dispose();
    
                _currentImageStream = stream;
    
                try
                {
                    barcodeImage.Source = ImageSource.FromStream(() => CreateMemoryStream(_currentImageStream));
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Load Image Error", ex.Message, "OK");
                }
            }
    
            /// <summary>
            /// Creates the memory stream from specified stream.
            /// </summary>
            /// <param name="stream">The stream.</param>
            /// <returns></returns>
            private static MemoryStream CreateMemoryStream(Stream stream)
            {
                MemoryStream result = new MemoryStream((int)stream.Length);
                CopyStream(stream, result);
                result.Position = 0;
                return result;
            }
    
            /// <summary>
            /// Copies the stream.
            /// </summary>
            /// <param name="sourceStream">The source stream.</param>
            /// <param name="destStream">The dest stream.</param>
            private static void CopyStream(Stream sourceStream, Stream destStream)
            {
                int bufferLength = 32 * 1024;
                try
                {
                    if (sourceStream.Length > 0 && sourceStream.Length < bufferLength)
                        bufferLength = (int)sourceStream.Length;
                }
                catch
                {
                }
                byte[] buffer = new byte[bufferLength];
    
                int bytesRead;
                while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    destStream.Write(buffer, 0, bytesRead);
                }
            }
    
        }
    }
    
  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.