-
-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathCommunityToolkitCameraPage.xaml.cs
81 lines (71 loc) · 1.99 KB
/
CommunityToolkitCameraPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
namespace MauiBarcode;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Views;
using Microsoft.Maui.Graphics.Platform;
using ZXing;
using ZXing.Common;
public partial class CommunityToolkitCameraPage : ContentPage
{
private readonly IBarcodeReaderGeneric barcodeReader = new BarcodeReaderGeneric()
{
AutoRotate = true,
Options = new DecodingOptions()
{
PossibleFormats = [BarcodeFormat.QR_CODE],
TryHarder = true,
TryInverted = true,
PureBarcode = false
}
};
PeriodicTimer timer = new(TimeSpan.FromMilliseconds(3000));
public CommunityToolkitCameraPage()
{
InitializeComponent();
}
#if !TIZEN
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);
}
protected override void OnDisappearing()
{
ToolkitCameraView.MediaCaptured -= OnMediaCaptured;
base.OnDisappearing();
}
private async void OnMediaCaptured(object? sender, MediaCapturedEventArgs e)
{
var image = (PlatformImage)PlatformImage.FromStream(e.Media);
var result = barcodeReader.Decode(new ImageLuminanceSource(image));
if (result is null)
{
return;
}
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Toast.Make(result.Text, ToastDuration.Long).Show();
});
}
#endif
private async void StartButton_OnClicked(object? sender, EventArgs e)
{
timer = new(TimeSpan.FromMilliseconds(3000));
#if !TIZEN
await ToolkitCameraView.StartCameraPreview(CancellationToken.None);
while (await timer.WaitForNextTickAsync())
{
await ToolkitCameraView.CaptureImage(CancellationToken.None);
}
#endif
}
private void StopButton_OnClicked(object? sender, EventArgs e)
{
timer.Dispose();
#if !TIZEN
ToolkitCameraView.StopCameraPreview();
#endif
}
}