From 6051530fdf54460391a292b9d5e7adf1fa59a0e7 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Sat, 29 Jun 2024 14:53:27 +0300 Subject: [PATCH] Various methods for barcode scanning in .NET MAUI --- Auth/MauiAuthBlazor/Shared/MainLayout.razor | 2 +- BottomSheet/README.md | 2 +- BottomSheet/README.mdpp | 2 +- CardLayout/README.md | 2 +- CardLayout/README.mdpp | 2 +- .../MauiSqliteBlazor/Shared/MainLayout.razor | 2 +- DynamicFab/README.md | 2 +- DynamicFab/README.mdpp | 2 +- KanbanBoard/README.md | 2 +- KanbanBoard/README.mdpp | 2 +- MauiAnimation/README.md | 2 +- MauiAnimation/README.mdpp | 2 +- MauiBadge/README.md | 2 +- MauiBadge/README.mdpp | 2 +- MauiBank/README.md | 2 +- MauiBank/README.mdpp | 2 +- MauiBarcode/App.xaml | 14 + MauiBarcode/App.xaml.cs | 11 + MauiBarcode/AppShell.xaml | 28 ++ MauiBarcode/AppShell.xaml.cs | 9 + MauiBarcode/CommunityToolkitCameraPage.xaml | 11 + .../CommunityToolkitCameraPage.xaml.cs | 78 ++++ MauiBarcode/MainPage.xaml | 18 + MauiBarcode/MainPage.xaml.cs | 26 ++ MauiBarcode/MauiBarcode.csproj | 38 ++ MauiBarcode/MauiProgram.cs | 19 + .../Platforms/Android/AndroidManifest.xml | 13 + MauiBarcode/Platforms/Android/MainActivity.cs | 10 + .../Platforms/Android/MainApplication.cs | 15 + .../Platforms/Android/MlKitBarcodeScanner.cs | 29 ++ .../Android/OnBarcodeResultListener.cs | 25 + .../Android/Resources/values/colors.xml | 6 + .../Platforms/MacCatalyst/AppDelegate.cs | 9 + .../Platforms/MacCatalyst/Entitlements.plist | 14 + MauiBarcode/Platforms/MacCatalyst/Info.plist | 40 ++ MauiBarcode/Platforms/MacCatalyst/Program.cs | 15 + MauiBarcode/Platforms/Tizen/Main.cs | 17 + .../Platforms/Tizen/tizen-manifest.xml | 15 + MauiBarcode/Platforms/Windows/App.xaml | 8 + MauiBarcode/Platforms/Windows/App.xaml.cs | 23 + .../Platforms/Windows/Package.appxmanifest | 46 ++ MauiBarcode/Platforms/Windows/app.manifest | 15 + MauiBarcode/Platforms/iOS/AppDelegate.cs | 9 + MauiBarcode/Platforms/iOS/Info.plist | 34 ++ MauiBarcode/Platforms/iOS/Program.cs | 15 + MauiBarcode/Properties/launchSettings.json | 8 + MauiBarcode/README.md | 7 + MauiBarcode/README.mdpp | 7 + MauiBarcode/Resources/AppIcon/appicon.svg | 4 + MauiBarcode/Resources/AppIcon/appiconfg.svg | 8 + MauiBarcode/Resources/Splash/splash.svg | 8 + MauiBarcode/Resources/Styles/Colors.xaml | 45 ++ MauiBarcode/Resources/Styles/Styles.xaml | 426 ++++++++++++++++++ MauiBarcode/ZxingCameraPage.xaml | 16 + MauiBarcode/ZxingCameraPage.xaml.cs | 69 +++ MauiBlazorPhotoGallery/README.md | 2 +- MauiBlazorPhotoGallery/README.mdpp | 2 +- .../Shared/MainLayout.razor | 2 +- MauiCaptcha/README.md | 2 +- MauiCaptcha/README.mdpp | 2 +- MauiDraggableView/README.md | 2 +- MauiDraggableView/README.mdpp | 2 +- MauiDynamicConfiguration/README.md | 2 +- MauiDynamicConfiguration/README.mdpp | 2 +- MauiImageEffects/README.md | 2 +- MauiImageEffects/README.mdpp | 2 +- MauiLocalization/README.md | 2 +- MauiLocalization/README.mdpp | 2 +- MauiMaps/README.md | 2 +- MauiMaps/README.mdpp | 2 +- MauiMarkdown/README.md | 2 +- MauiMarkdown/README.mdpp | 2 +- MauiMultiWindow/README.md | 2 +- MauiMultiWindow/README.mdpp | 2 +- MauiNotifications/README.md | 8 + MauiNotifications/README.mdpp | 8 + MauiPaint/MainPageViewModel.cs | 2 +- MauiPaint/README.md | 2 +- MauiPaint/README.mdpp | 2 +- MauiSamples.sln | 12 +- MauiShellCustomization/README.md | 2 +- MauiShellCustomization/README.mdpp | 2 +- MauiSpeech/README.md | 2 +- MauiSpeech/README.mdpp | 2 +- MauiTabView/README.md | 2 +- MauiTabView/README.mdpp | 2 +- MauiWorkflowBuilder/README.md | 2 +- MauiWorkflowBuilder/README.mdpp | 2 +- README.md | 2 + README.mdpp | 2 + RepositoryContributors/README.md | 2 +- RepositoryContributors/README.mdpp | 2 +- .../Shared/MainLayout.razor | 2 +- TutorialHelp/README.md | 2 +- TutorialHelp/README.mdpp | 2 +- iOSExtensions/JokeApp/README.md | 2 +- iOSExtensions/JokeApp/README.mdpp | 2 +- .../AppContainer/MainPage.xaml.cs | 2 +- iOSExtensions/ShareExtension/README.md | 2 +- iOSExtensions/ShareExtension/README.mdpp | 2 +- 100 files changed, 1295 insertions(+), 57 deletions(-) create mode 100644 MauiBarcode/App.xaml create mode 100644 MauiBarcode/App.xaml.cs create mode 100644 MauiBarcode/AppShell.xaml create mode 100644 MauiBarcode/AppShell.xaml.cs create mode 100644 MauiBarcode/CommunityToolkitCameraPage.xaml create mode 100644 MauiBarcode/CommunityToolkitCameraPage.xaml.cs create mode 100644 MauiBarcode/MainPage.xaml create mode 100644 MauiBarcode/MainPage.xaml.cs create mode 100644 MauiBarcode/MauiBarcode.csproj create mode 100644 MauiBarcode/MauiProgram.cs create mode 100644 MauiBarcode/Platforms/Android/AndroidManifest.xml create mode 100644 MauiBarcode/Platforms/Android/MainActivity.cs create mode 100644 MauiBarcode/Platforms/Android/MainApplication.cs create mode 100644 MauiBarcode/Platforms/Android/MlKitBarcodeScanner.cs create mode 100644 MauiBarcode/Platforms/Android/OnBarcodeResultListener.cs create mode 100644 MauiBarcode/Platforms/Android/Resources/values/colors.xml create mode 100644 MauiBarcode/Platforms/MacCatalyst/AppDelegate.cs create mode 100644 MauiBarcode/Platforms/MacCatalyst/Entitlements.plist create mode 100644 MauiBarcode/Platforms/MacCatalyst/Info.plist create mode 100644 MauiBarcode/Platforms/MacCatalyst/Program.cs create mode 100644 MauiBarcode/Platforms/Tizen/Main.cs create mode 100644 MauiBarcode/Platforms/Tizen/tizen-manifest.xml create mode 100644 MauiBarcode/Platforms/Windows/App.xaml create mode 100644 MauiBarcode/Platforms/Windows/App.xaml.cs create mode 100644 MauiBarcode/Platforms/Windows/Package.appxmanifest create mode 100644 MauiBarcode/Platforms/Windows/app.manifest create mode 100644 MauiBarcode/Platforms/iOS/AppDelegate.cs create mode 100644 MauiBarcode/Platforms/iOS/Info.plist create mode 100644 MauiBarcode/Platforms/iOS/Program.cs create mode 100644 MauiBarcode/Properties/launchSettings.json create mode 100644 MauiBarcode/README.md create mode 100644 MauiBarcode/README.mdpp create mode 100644 MauiBarcode/Resources/AppIcon/appicon.svg create mode 100644 MauiBarcode/Resources/AppIcon/appiconfg.svg create mode 100644 MauiBarcode/Resources/Splash/splash.svg create mode 100644 MauiBarcode/Resources/Styles/Colors.xaml create mode 100644 MauiBarcode/Resources/Styles/Styles.xaml create mode 100644 MauiBarcode/ZxingCameraPage.xaml create mode 100644 MauiBarcode/ZxingCameraPage.xaml.cs diff --git a/Auth/MauiAuthBlazor/Shared/MainLayout.razor b/Auth/MauiAuthBlazor/Shared/MainLayout.razor index 260f8483..65b16397 100644 --- a/Auth/MauiAuthBlazor/Shared/MainLayout.razor +++ b/Auth/MauiAuthBlazor/Shared/MainLayout.razor @@ -3,7 +3,7 @@
- About + About
diff --git a/BottomSheet/README.md b/BottomSheet/README.md index aedf1f3b..b0ae35c7 100644 --- a/BottomSheet/README.md +++ b/BottomSheet/README.md @@ -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 diff --git a/BottomSheet/README.mdpp b/BottomSheet/README.mdpp index ad7d4860..0944efc0 100644 --- a/BottomSheet/README.mdpp +++ b/BottomSheet/README.mdpp @@ -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 diff --git a/CardLayout/README.md b/CardLayout/README.md index 771872ef..beba2180 100644 --- a/CardLayout/README.md +++ b/CardLayout/README.md @@ -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 diff --git a/CardLayout/README.mdpp b/CardLayout/README.mdpp index c15f34f4..34d39db3 100644 --- a/CardLayout/README.mdpp +++ b/CardLayout/README.mdpp @@ -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 diff --git a/Database/Sqlite/MauiSqliteBlazor/Shared/MainLayout.razor b/Database/Sqlite/MauiSqliteBlazor/Shared/MainLayout.razor index 34ca6c17..8715101f 100644 --- a/Database/Sqlite/MauiSqliteBlazor/Shared/MainLayout.razor +++ b/Database/Sqlite/MauiSqliteBlazor/Shared/MainLayout.razor @@ -7,7 +7,7 @@
- About + About
diff --git a/DynamicFab/README.md b/DynamicFab/README.md index 5fa6604f..f292ea1b 100644 --- a/DynamicFab/README.md +++ b/DynamicFab/README.md @@ -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 diff --git a/DynamicFab/README.mdpp b/DynamicFab/README.mdpp index 66d48b05..8f9c0447 100644 --- a/DynamicFab/README.mdpp +++ b/DynamicFab/README.mdpp @@ -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 diff --git a/KanbanBoard/README.md b/KanbanBoard/README.md index 9a21cb39..87d09f28 100644 --- a/KanbanBoard/README.md +++ b/KanbanBoard/README.md @@ -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 diff --git a/KanbanBoard/README.mdpp b/KanbanBoard/README.mdpp index fbfd4afa..ffe375eb 100644 --- a/KanbanBoard/README.mdpp +++ b/KanbanBoard/README.mdpp @@ -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 diff --git a/MauiAnimation/README.md b/MauiAnimation/README.md index 00b77a97..d0328274 100644 --- a/MauiAnimation/README.md +++ b/MauiAnimation/README.md @@ -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 diff --git a/MauiAnimation/README.mdpp b/MauiAnimation/README.mdpp index 9af3cf57..9df863f3 100644 --- a/MauiAnimation/README.mdpp +++ b/MauiAnimation/README.mdpp @@ -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 diff --git a/MauiBadge/README.md b/MauiBadge/README.md index 65487734..356de35e 100644 --- a/MauiBadge/README.md +++ b/MauiBadge/README.md @@ -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 diff --git a/MauiBadge/README.mdpp b/MauiBadge/README.mdpp index b546950f..d0c74676 100644 --- a/MauiBadge/README.mdpp +++ b/MauiBadge/README.mdpp @@ -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 diff --git a/MauiBank/README.md b/MauiBank/README.md index a7548c0c..bcafa9c4 100644 --- a/MauiBank/README.md +++ b/MauiBank/README.md @@ -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 diff --git a/MauiBank/README.mdpp b/MauiBank/README.mdpp index 8d4a2fa7..1004e557 100644 --- a/MauiBank/README.mdpp +++ b/MauiBank/README.mdpp @@ -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 diff --git a/MauiBarcode/App.xaml b/MauiBarcode/App.xaml new file mode 100644 index 00000000..a3e97d5b --- /dev/null +++ b/MauiBarcode/App.xaml @@ -0,0 +1,14 @@ + + + + + + + + + + + diff --git a/MauiBarcode/App.xaml.cs b/MauiBarcode/App.xaml.cs new file mode 100644 index 00000000..132ff99f --- /dev/null +++ b/MauiBarcode/App.xaml.cs @@ -0,0 +1,11 @@ +namespace MauiBarcode; + +public partial class App : Application +{ + public App() + { + InitializeComponent(); + + MainPage = new AppShell(); + } +} \ No newline at end of file diff --git a/MauiBarcode/AppShell.xaml b/MauiBarcode/AppShell.xaml new file mode 100644 index 00000000..34dfd4d1 --- /dev/null +++ b/MauiBarcode/AppShell.xaml @@ -0,0 +1,28 @@ + + + + + + + + + + + + diff --git a/MauiBarcode/AppShell.xaml.cs b/MauiBarcode/AppShell.xaml.cs new file mode 100644 index 00000000..0b83671a --- /dev/null +++ b/MauiBarcode/AppShell.xaml.cs @@ -0,0 +1,9 @@ +namespace MauiBarcode; + +public partial class AppShell : Shell +{ + public AppShell() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/MauiBarcode/CommunityToolkitCameraPage.xaml b/MauiBarcode/CommunityToolkitCameraPage.xaml new file mode 100644 index 00000000..e8f5ebdf --- /dev/null +++ b/MauiBarcode/CommunityToolkitCameraPage.xaml @@ -0,0 +1,11 @@ + + + + + + + + diff --git a/MauiBarcode/CommunityToolkitCameraPage.xaml.cs b/MauiBarcode/CommunityToolkitCameraPage.xaml.cs new file mode 100644 index 00000000..e90eaac9 --- /dev/null +++ b/MauiBarcode/CommunityToolkitCameraPage.xaml.cs @@ -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(); +#endif + foreach (var result in results ?? []) + { + await MainThread.InvokeOnMainThreadAsync(async () => + { + await Toast.Make(result.Text, ToastDuration.Long).Show(); + }); + } + } + catch (Exception exception) + { + Console.WriteLine(exception); + } + } +} \ No newline at end of file diff --git a/MauiBarcode/MainPage.xaml b/MauiBarcode/MainPage.xaml new file mode 100644 index 00000000..2b8e116f --- /dev/null +++ b/MauiBarcode/MainPage.xaml @@ -0,0 +1,18 @@ + + + + + + +