Skip to content

Commit

Permalink
Various methods for barcode scanning in .NET MAUI
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jun 29, 2024
1 parent c4eb699 commit 6051530
Show file tree
Hide file tree
Showing 100 changed files with 1,295 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Auth/MauiAuthBlazor/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="page">
<main>
<div class="top-row">
<a href="https://vladislavantonyuk.azurewebsites.net/" target="_blank">About</a>
<a href="https://vladislavantonyuk.github.io/" target="_blank">About</a>
</div>

<article class="content">
Expand Down
2 changes: 1 addition & 1 deletion BottomSheet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-a-bottom-sheet-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Creating-a-bottom-sheet-using-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion BottomSheet/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-a-bottom-sheet-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Creating-a-bottom-sheet-using-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion CardLayout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-a-card-stack-layout-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Creating-a-card-stack-layout-using-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion CardLayout/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-a-card-stack-layout-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Creating-a-card-stack-layout-using-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion Database/Sqlite/MauiSqliteBlazor/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<main>
<div class="top-row px-4">
<a href="https://vladislavantonyuk.azurewebsites.net/articles/Adding-SQLite-to-the-.NET-MAUI-application" target="_blank">About</a>
<a href="https://vladislavantonyuk.github.io/articles/Adding-SQLite-to-the-.NET-MAUI-application" target="_blank">About</a>
</div>

<article class="content px-4">
Expand Down
2 changes: 1 addition & 1 deletion DynamicFab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-dynamic-floating-action-button
Article: https://vladislavantonyuk.github.io/articles/Creating-dynamic-floating-action-button

## Images

Expand Down
2 changes: 1 addition & 1 deletion DynamicFab/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-dynamic-floating-action-button
Article: https://vladislavantonyuk.github.io/articles/Creating-dynamic-floating-action-button

## Images

Expand Down
2 changes: 1 addition & 1 deletion KanbanBoard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-Kanban-Board-using-Xamarin-Forms-5
Article: https://vladislavantonyuk.github.io/articles/Creating-Kanban-Board-using-Xamarin-Forms-5

## Build
```pwsh
Expand Down
2 changes: 1 addition & 1 deletion KanbanBoard/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Creating-Kanban-Board-using-Xamarin-Forms-5
Article: https://vladislavantonyuk.github.io/articles/Creating-Kanban-Board-using-Xamarin-Forms-5

## Build
```pwsh
Expand Down
2 changes: 1 addition & 1 deletion MauiAnimation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Create-custom-animations-using-.NET-MAUI-CommunityToolkit
Article: https://vladislavantonyuk.github.io/articles/Create-custom-animations-using-.NET-MAUI-CommunityToolkit

## Images

Expand Down
2 changes: 1 addition & 1 deletion MauiAnimation/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Create-custom-animations-using-.NET-MAUI-CommunityToolkit
Article: https://vladislavantonyuk.github.io/articles/Create-custom-animations-using-.NET-MAUI-CommunityToolkit

## Images

Expand Down
2 changes: 1 addition & 1 deletion MauiBadge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/AppIcon-Badge-with-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/AppIcon-Badge-with-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion MauiBadge/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/AppIcon-Badge-with-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/AppIcon-Badge-with-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion MauiBank/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Buy Me A Coffee](https://ik.imagekit.io/VladislavAntonyuk/vladislavantonyuk/misc/bmc-button.png)](https://www.buymeacoffee.com/vlad.antonyuk)

Article: https://vladislavantonyuk.azurewebsites.net/articles/Replicate-a-bank-application-UI-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Replicate-a-bank-application-UI-using-.NET-MAUI

## Images

Expand Down
2 changes: 1 addition & 1 deletion MauiBank/README.mdpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

!INCLUDE "./md/header.mdpp"

Article: https://vladislavantonyuk.azurewebsites.net/articles/Replicate-a-bank-application-UI-using-.NET-MAUI
Article: https://vladislavantonyuk.github.io/articles/Replicate-a-bank-application-UI-using-.NET-MAUI

## Images

Expand Down
14 changes: 14 additions & 0 deletions MauiBarcode/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version = "1.0" encoding = "UTF-8" ?>
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiBarcode"
x:Class="MauiBarcode.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
11 changes: 11 additions & 0 deletions MauiBarcode/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace MauiBarcode;

public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
}
28 changes: 28 additions & 0 deletions MauiBarcode/AppShell.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="MauiBarcode.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiBarcode"
Shell.FlyoutBehavior="Disabled"
Title="MauiBarcode">

<TabBar>
<ShellContent
Title="CommunityToolkit MAUI Camera"
ContentTemplate="{DataTemplate local:CommunityToolkitCameraPage}"
Route="CommunityToolkitCameraPage" />

<ShellContent
Title="ZXing Camera MAUI"
ContentTemplate="{DataTemplate local:ZxingCameraPage}"
Route="ZxingCameraPage" />

<ShellContent
IsVisible="{OnPlatform Android=true, Default=false}"
Title="MLKit Scanner (Android only)"
ContentTemplate="{DataTemplate local:MainPage}"
Route="MainPage" />
</TabBar>

</Shell>
9 changes: 9 additions & 0 deletions MauiBarcode/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace MauiBarcode;

public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
}
}
11 changes: 11 additions & 0 deletions MauiBarcode/CommunityToolkitCameraPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MauiBarcode.CommunityToolkitCameraPage">

<Grid>
<toolkit:CameraView x:Name="ToolkitCameraView" />
</Grid>

</ContentPage>
78 changes: 78 additions & 0 deletions MauiBarcode/CommunityToolkitCameraPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
namespace MauiBarcode;

#if WINDOWS
using Windows.Graphics.Imaging;
using System.Runtime.InteropServices.WindowsRuntime;
#endif
using Camera.MAUI.ZXing;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Graphics.Platform;

public partial class CommunityToolkitCameraPage : ContentPage
{
private readonly ZXingBarcodeDecoder barcodeReader = new ();

public CommunityToolkitCameraPage()
{
InitializeComponent();
}

protected override async void OnAppearing()
{
base.OnAppearing();
ToolkitCameraView.MediaCaptured += OnMediaCaptured;

var cameras = await ToolkitCameraView.GetAvailableCameras(CancellationToken.None);
ToolkitCameraView.SelectedCamera = cameras.FirstOrDefault(x => x.Position != CommunityToolkit.Maui.Core.Primitives.CameraPosition.Front);

await Task.Delay(1000);
await ToolkitCameraView.StartCameraPreview(CancellationToken.None);

PeriodicTimer timer = new(TimeSpan.FromMilliseconds(3000));
while (await timer.WaitForNextTickAsync())
{
await ToolkitCameraView.CaptureImage(CancellationToken.None);
}
}

protected override void OnDisappearing()
{
ToolkitCameraView.MediaCaptured -= OnMediaCaptured;
base.OnDisappearing();
}

private async void OnMediaCaptured(object? sender, MediaCapturedEventArgs e)
{
try
{
var image = PlatformImage.FromStream(e.Media);
#if ANDROID
var results = barcodeReader.Decode(image.AsBitmap());
#elif IOS || MACCATALYST
var results = barcodeReader.Decode(image.AsUIImage());
#elif WINDOWS
var softwareBitmap = SoftwareBitmap.CreateCopyFromBuffer(
image.AsBytes().AsBuffer(),
BitmapPixelFormat.Rgba16,
(int)image.Width,
(int)image.Height);
var results = barcodeReader.Decode(softwareBitmap);
#else
var results = new List<BarcodeResult>();
#endif
foreach (var result in results ?? [])
{
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Toast.Make(result.Text, ToastDuration.Long).Show();
});
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
18 changes: 18 additions & 0 deletions MauiBarcode/MainPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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="MauiBarcode.MainPage">

<ScrollView>
<VerticalStackLayout
Padding="30,0"
Spacing="25">

<Button
Text="MLKit Scanner (Android only)"
Clicked="OnMlKitScannerClicked"
HorizontalOptions="Fill" />
</VerticalStackLayout>
</ScrollView>

</ContentPage>
26 changes: 26 additions & 0 deletions MauiBarcode/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace MauiBarcode;

using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;

public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}

private async void OnMlKitScannerClicked(object sender, EventArgs e)
{
#if ANDROID
using var mlkit = new MlKitBarcodeScanner();
var barcode = await mlkit.ScanAsync();
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Toast.Make(barcode is null ? "Error has occurred during barcode scanning" : barcode.RawValue, ToastDuration.Long).Show();
});
#else
await Toast.Make("This feature is only available on Android", ToastDuration.Long).Show();
#endif
}
}
38 changes: 38 additions & 0 deletions MauiBarcode/MauiBarcode.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<RootNamespace>MauiBarcode</RootNamespace>

<!-- Display name -->
<ApplicationTitle>MauiBarcode</ApplicationTitle>

<!-- App Identifier -->
<ApplicationId>com.vladislavantonyuk.mauibarcode</ApplicationId>

<!-- Versions -->
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<ApplicationVersion>1</ApplicationVersion>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />

<!-- Splash Screen -->
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />

</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == '$(NetVersion)-android'">
<PackageReference Include="Xamarin.AndroidX.Collection.Ktx" Version="1.4.0.1" />
<PackageReference Include="Xamarin.GooglePlayServices.Code.Scanner" Version="116.1.0.4" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Camera.MAUI.ZXing" Version="1.0.0" />
<PackageReference Include="CommunityToolkit.Maui.Camera" Version="1.0.2" />
<PackageReference Include="CommunityToolkit.Maui" Version="9.0.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit 6051530

Please sign in to comment.