Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

How to Implement Barcode Reading and Writing on Android using .NET MAUI

Based on https://ironsoftware.com/get-started/android/

.NET MAUI (Multi-platform App UI), a modern successor to Xamarin.Forms, allows developers to design and deploy applications across Android, iOS, macOS, and Windows, all using .NET. This framework facilitates the development of native interfaces that are consistent across different platforms.

Incorporate barcode functionalities into your Android applications with the BarCode.Android package!

IronBarcode Android Package Overview

The BarCode.Android package is specifically tailored to include barcode functionality on Android platforms within .NET multi-platform projects, making the general BarCode package unnecessary.

:InstallCmd Install-Package BarCode.Android
C# NuGet Library for PDF

Install using NuGet

Install-Package BarCode.Android
nuget.org/packages/BarCode.Android/

Starting a .NET MAUI Project

Launch Visual Studio and initiate a new project by clicking "Create a new project". Look up MAUI, choose the .NET MAUI App template, and proceed by clicking "Next".

Integration of BarCode.Android Library

Adding this library into your project can be done through several methods. NuGet offers the simplest approach.

  1. In Visual Studio, right-click "Dependencies" and select "Manage NuGet Packages ...".
  2. Navigate to the "Browse" tab and look for "BarCode.Android".
  3. Choose the "BarCode.Android" package and click on "Install".

To avoid any platform conflict, configure the .csproj to load this package only for Android-specific builds as follows:

  1. Right-click the *.csproj file of your project and select "Edit Project File".
  2. Add a new ItemGroup element with a conditional filter:
<ItemGroup Condition="$(TargetFramework.Contains('android')) == true">
    <PackageReference Include="BarCode.Android" Version="2025.3.4" />
</ItemGroup>
  1. Relocate the "BarCode.Android" PackageReference into the newly established ItemGroup.

This setup restricts usage of the "BarCode.Android" package to Android platforms only. For iOS, consider integrating BarCode.iOS.

Configuring the Android Bundle

Modify the ".csproj" file to define the bundle settings for Android:

<AndroidBundleConfigurationFile>BundleConfig.json</AndroidBundleConfigurationFile>

Establish a "BundleConfig.json" at the project's root, containing key configurations for your Android bundle:

{
    "optimizations": {
        "uncompress_native_libraries": {}
    }
}

These settings ensure that native libraries are not compressed, optimizing their performance within the Android system.

Application User Interface Design

Expand your XAML configuration to facilitate user interactions for generating and reading barcodes or QR codes. Here's an example setup:

<?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="IronBarcodeMauiAndroid.MainPage">

    <VerticalStackLayout Padding="20">
        <HorizontalStackLayout>
            <CheckBox x:Name="generatePdfCheckBox" IsChecked="{Binding IsGeneratePdfChecked}" />
            <Label Text="PDF (unchecked for PNG)" VerticalOptions="Center"/>
        </HorizontalStackLayout>
        
        <Entry x:Name="barcodeInput" Placeholder="Enter barcode value..." />
        <Button Text="Generate and save barcode" Clicked="WriteBarcode" />

        <Entry x:Name="qrInput" Placeholder="Enter QR code value..." />
        <Button Text="Generate and save QR code" Clicked="WriteQRcode" />

        <Button
            Text="Read Barcode"
            Clicked="ReadBarcode"
            HorizontalOptions="Center"
            Margin="20, 20, 20, 10"/>
        <ScrollView
            BackgroundColor="LightGray"
            Padding="10"
            Margin="10, 10, 10, 30">
            <Label x:Name="OutputText"/>
        </ScrollView>
    </VerticalStackLayout>

</ContentPage>

Barcode Operations

The functions above allow the user to dynamically switch between PDF and PNG output formats for barcodes and QR codes. Set your trial or full license key before generation begins.

The subsequent code demonstrates capturing input for barcode generation, creating the barcode, and saving the output file. For iOS compatibility, ensure the file path customization allows access to the Files app.