Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion BigIslandBarcode/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public App()
{
InitializeComponent();

MainPage = new MainPage();
MainPage = new NavigationPage(new MainPage());
}
}
}
16 changes: 15 additions & 1 deletion BigIslandBarcode/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@
<Grid
Grid.Row="0"
BackgroundColor="#aa000000">
<Label Grid.Row="2" Text="Top text..." HorizontalOptions="Center" VerticalOptions="Center" TextColor="White" />

<Button Text="Pick File"
HorizontalOptions="Start"
VerticalOptions="Center"
Clicked="PickClicked" />

<Label Text="Top text..."
HorizontalOptions="Center"
VerticalOptions="Center"
TextColor="White" />

<Button Text="Take Photo"
HorizontalOptions="End"
VerticalOptions="Center"
Clicked="TakeClicked" />
</Grid>

<Grid
Expand Down
14 changes: 12 additions & 2 deletions BigIslandBarcode/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ void SwitchCameraButton_Clicked(object sender, EventArgs e)
void TorchButton_Clicked(object sender, EventArgs e)
{
barcodeView.IsTorchOn = !barcodeView.IsTorchOn;
}
}
}

async void PickClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new PickImagePage());
}

async void TakeClicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new TakeImagePage());
}
}
}
56 changes: 56 additions & 0 deletions BigIslandBarcode/PickImage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
xmlns:zxing2="clr-namespace:ZXing.Net.Maui;assembly=ZXing.Net.MAUI"
x:Class="BigIslandBarcode.PickImagePage"
BackgroundColor="#000000">

<ScrollView HorizontalOptions="CenterAndExpand">
<StackLayout Padding="0,30"
Spacing="30"
HorizontalOptions="CenterAndExpand">

<Button x:Name="PickButton"
Text="Pick a Barcode"
Clicked="PickClicked"
HorizontalOptions="Center"/>

<Label x:Name="FileInfo"
HorizontalTextAlignment="Center"
HorizontalOptions="CenterAndExpand"/>

<Image x:Name="ImageOutput"
WidthRequest="150"
HeightRequest="150" />

<VerticalStackLayout x:Name="ActivityContainer"
Spacing="30"
HorizontalOptions="Center"
IsVisible="False">

<ActivityIndicator x:Name="ActivityIndicator"
HorizontalOptions="Center"
Scale="1.5" />
<Label x:Name="ActivityLabel"
HorizontalTextAlignment="Center"
HorizontalOptions="Center"/>
</VerticalStackLayout>

<Label x:Name="ParseResult"
HorizontalTextAlignment="Center"
HorizontalOptions="CenterAndExpand"/>

<zxing:BarcodeGeneratorView
x:Name="barcodeGenerator"
HorizontalOptions="Center"
HeightRequest="100"
WidthRequest="150"
ForegroundColor="DarkBlue"
Format="QrCode"
BarcodeMargin="1"
IsVisible="False"/>

</StackLayout>
</ScrollView>

</ContentPage>
164 changes: 164 additions & 0 deletions BigIslandBarcode/PickImage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Storage;
using ZXing.Net.Maui;
using ZXing.Net.Maui.Readers;

namespace BigIslandBarcode;

public partial class PickImagePage : ContentPage
{
readonly IBarcodeReader _reader;
bool _working = false;

public PickImagePage()
{
InitializeComponent();

_reader =
new ZXingBarcodeReader
{
Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.All,
AutoRotate = true
}
};

NavigatedTo += PageNavigatedTo;
}

async void PageNavigatedTo(object? sender, NavigatedToEventArgs e)
=> await CallFilePicker();

async void PickClicked(object sender, EventArgs e)
=> await CallFilePicker();

async Task CallFilePicker()
{
if (!_working)
{
_working = true;

FileInfo.Text = "Awaiting file selection";
ParseResult.Text = "";
ImageOutput.Source = null;

try
{
var result =
await FilePicker.PickAsync(
new PickOptions
{
FileTypes = FilePickerFileType.Images,
PickerTitle = "Choose your barcode"
}
);

if (result == null)
{
FileInfo.Text = "No file selected";
_working = false;
}
else
{
ActivityContainer.IsVisible = true;
ActivityIndicator.IsRunning = true;
PickButton.IsEnabled = false;
barcodeGenerator.IsVisible = false;

var worker = new BackgroundWorker();
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_Completed;

worker.RunWorkerAsync(result);
}
}
catch (Exception ex)
{
FileInfo.Text = $"Something's wrong: {ex.Message}";
ParseResult.Text = $"Something's wrong: {ex.Message}";

_working = false;
}
}
else
{
await DisplayAlert("Task Already Running", "Please cancel the runnning task first", "Okay");
}
}

async void Worker_DoWork(object? sender, DoWorkEventArgs e)
{
var fileResult = e.Argument as FileResult;
BarcodeResult? result = null;

var sW = new Stopwatch();

if (fileResult != null)
{

await MainThread.InvokeOnMainThreadAsync(() => ActivityLabel.Text = "Loading file");

using var stream = await fileResult.OpenReadAsync();

await MainThread.InvokeOnMainThreadAsync(() =>
{
FileInfo.Text = $"Name: {fileResult.FileName} - Size: {(stream.Length / 1024d):#.##} KiB";
ActivityLabel.Text = "Decoding";
});

sW.Start();

var decodeResult = _reader.Decode(stream);

sW.Stop();

result = decodeResult?.FirstOrDefault();
}

await MainThread.InvokeOnMainThreadAsync(() =>
{
if (fileResult != null)
ImageOutput.Source = ImageSource.FromFile(fileResult.FullPath);

if (result != null)
{
ParseResult.Text = $"Found Barcode (in {sW.ElapsedMilliseconds}ms)\nValue: {result.Value}";

try
{
barcodeGenerator.Value = result.Value;
barcodeGenerator.Format = result.Format;
barcodeGenerator.IsVisible = true;
}
catch (Exception ex)
{
ParseResult.Text += "\r\nError displaying barcode: " + ex.Message;
}
}
else
{
ParseResult.Text = $"No Barcode Found (in {sW.ElapsedMilliseconds}ms)";
}

Worker_Completed(true, null!);
});
}

void Worker_Completed(object? sender, RunWorkerCompletedEventArgs e)
{
if (sender is bool finished && finished == true)
{
ActivityContainer.IsVisible = false;
ActivityIndicator.IsRunning = false;
PickButton.IsEnabled = true;
_working = false;
}
}
}
56 changes: 56 additions & 0 deletions BigIslandBarcode/TakeImage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
xmlns:zxing2="clr-namespace:ZXing.Net.Maui;assembly=ZXing.Net.MAUI"
x:Class="BigIslandBarcode.TakeImagePage"
BackgroundColor="#000000">

<ScrollView HorizontalOptions="CenterAndExpand">
<StackLayout Padding="0,30"
Spacing="30"
HorizontalOptions="CenterAndExpand">

<Button x:Name="CaptureButton"
Text="Take a picture of your barcode"
Clicked="CaptureClicked"
HorizontalOptions="Center"/>

<Label x:Name="FileInfo"
HorizontalTextAlignment="Center"
HorizontalOptions="CenterAndExpand"/>

<Image x:Name="ImageOutput"
WidthRequest="150"
HeightRequest="150" />

<VerticalStackLayout x:Name="ActivityContainer"
Spacing="30"
HorizontalOptions="Center"
IsVisible="False">

<ActivityIndicator x:Name="ActivityIndicator"
HorizontalOptions="Center"
Scale="1.5" />
<Label x:Name="ActivityLabel"
HorizontalTextAlignment="Center"
HorizontalOptions="Center"/>
</VerticalStackLayout>

<Label x:Name="ParseResult"
HorizontalTextAlignment="Center"
HorizontalOptions="CenterAndExpand"/>

<zxing:BarcodeGeneratorView
x:Name="barcodeGenerator"
HorizontalOptions="Center"
HeightRequest="100"
WidthRequest="150"
ForegroundColor="DarkBlue"
Format="QrCode"
BarcodeMargin="1"
IsVisible="False"/>

</StackLayout>
</ScrollView>

</ContentPage>
Loading