scan page

This commit is contained in:
Kyle Spearrin 2019-05-22 20:54:17 -04:00
parent ea3dcd6250
commit 543e3418a5
3 changed files with 152 additions and 0 deletions

View file

@ -64,6 +64,9 @@
<Compile Update="Pages\Vault\CollectionsPage.xaml.cs"> <Compile Update="Pages\Vault\CollectionsPage.xaml.cs">
<DependentUpon>CollectionsPage.xaml</DependentUpon> <DependentUpon>CollectionsPage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Update="Pages\Vault\ScanPage.xaml.cs">
<DependentUpon>ScanPage.xaml</DependentUpon>
</Compile>
<Compile Update="Pages\Vault\SharePage.xaml.cs"> <Compile Update="Pages\Vault\SharePage.xaml.cs">
<DependentUpon>SharePage.xaml</DependentUpon> <DependentUpon>SharePage.xaml</DependentUpon>
</Compile> </Compile>

View file

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8" ?>
<pages:BaseContentPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Bit.App.Pages.ScanPage"
xmlns:pages="clr-namespace:Bit.App.Pages"
xmlns:u="clr-namespace:Bit.App.Utilities"
xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
x:Name="_page"
Title="{u:I18n ScanQrTitle}">
<Grid
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<zxing:ZXingScannerView
x:Name="_zxing"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
AutomationId="zxingScannerView"
OnScanResult="OnScanResult">
</zxing:ZXingScannerView>
<Grid
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand"
AutomationId="zxingDefaultOverlay">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<BoxView
Grid.Column="0"
Grid.Row="0"
VerticalOptions="Fill"
HorizontalOptions="FillAndExpand"
BackgroundColor="Black"
Opacity="0.7" />
<Label
Text="{u:I18n CameraInstructionTop}"
AutomationId="zxingDefaultOverlay_TopTextLabel"
Grid.Column="0"
Grid.Row="0"
VerticalOptions="Center"
HorizontalOptions="Center"
BackgroundColor="White" />
<BoxView
Grid.Column="0"
Grid.Row="1"
VerticalOptions="Fill"
HorizontalOptions="FillAndExpand"
BackgroundColor="Transparent" />
<BoxView
Grid.Column="0"
Grid.Row="2"
VerticalOptions="Fill"
HorizontalOptions="FillAndExpand"
BackgroundColor="Black"
Opacity="0.7" />
<Label
Text="{u:I18n CameraInstructionBottom}"
AutomationId="zxingDefaultOverlay_BottomTextLabel"
Grid.Column="0"
Grid.Row="2"
VerticalOptions="Center"
HorizontalOptions="Center"
BackgroundColor="White" />
</Grid>
</Grid>
</pages:BaseContentPage>

View file

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms;
namespace Bit.App.Pages
{
public partial class ScanPage : BaseContentPage
{
private readonly Action<string> _callback;
private DateTime? _timerStarted = null;
private TimeSpan _timerMaxLength = TimeSpan.FromMinutes(3);
public ScanPage(Action<string> callback)
{
_callback = callback;
_zxing.Options = new ZXing.Mobile.MobileBarcodeScanningOptions
{
UseNativeScanning = true,
PossibleFormats = new List<ZXing.BarcodeFormat> { ZXing.BarcodeFormat.QR_CODE },
AutoRotate = false,
};
InitializeComponent();
}
protected override void OnAppearing()
{
base.OnAppearing();
_zxing.IsScanning = true;
_timerStarted = DateTime.Now;
Device.StartTimer(new TimeSpan(0, 0, 2), () =>
{
if(_timerStarted == null || (DateTime.Now - _timerStarted) > _timerMaxLength)
{
return false;
}
_zxing.AutoFocus();
return true;
});
}
protected override void OnDisappearing()
{
_timerStarted = null;
_zxing.IsScanning = false;
base.OnDisappearing();
}
private void OnScanResult(ZXing.Result result)
{
// Stop analysis until we navigate away so we don't keep reading barcodes
_zxing.IsAnalyzing = false;
_zxing.IsScanning = false;
if(!string.IsNullOrWhiteSpace(result?.Text) &&
Uri.TryCreate(result.Text, UriKind.Absolute, out Uri uri) &&
!string.IsNullOrWhiteSpace(uri?.Query))
{
var queryParts = uri.Query.Substring(1).ToLowerInvariant().Split('&');
foreach(var part in queryParts)
{
if(part.StartsWith("secret="))
{
_callback(part.Substring(7)?.ToUpperInvariant());
return;
}
}
}
_callback(null);
}
}
}