Generar imagen de código de barras en la aplicación MAUI para Android
Categoría del blog: Barcode ; .NET ; Android
07.05.2024
						
						
					
						
						
						
<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>
					
						
<?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>
					
						
// 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");
}
					
						
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);
            }
        }
    }
}