-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Various methods for barcode scanning in .NET MAUI
- Loading branch information
1 parent
c4eb699
commit 6051530
Showing
100 changed files
with
1,295 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace MauiBarcode; | ||
|
||
public partial class AppShell : Shell | ||
{ | ||
public AppShell() | ||
{ | ||
InitializeComponent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.