-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathMainActivity.cs
More file actions
114 lines (89 loc) · 3.49 KB
/
Copy pathMainActivity.cs
File metadata and controls
114 lines (89 loc) · 3.49 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Android.Content.PM;
using Android.Views;
using AndroidX.Fragment.App;
using Xamarin.Essentials;
using ZXing.Mobile;
namespace Sample.NetAndroid
{
[Activity(
Label = "@string/app_name",
MainLauncher = true,
Theme = "@style/Theme.AppCompat.Light",
ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden)]
public class MainActivity : Activity
{
MobileBarcodeScanner scanner;
Button buttonScanCustomView;
Button buttonScanDefaultView;
Button buttonContinuousScan;
Button buttonFragmentScanner;
Button buttonGenerate;
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);
Platform.Init(Application);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
//Create a new instance of our Scanner
scanner = new MobileBarcodeScanner();
buttonScanDefaultView = FindViewById<Button>(Resource.Id.buttonScanDefaultView);
buttonScanDefaultView.Click +=
async delegate
{
//Tell our scanner to use the default overlay
scanner.UseCustomOverlay = false;
//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
scanner.BottomText = "Wait for the barcode to automatically scan!";
//Start scanning
var result = await scanner.Scan();
HandleScanResult(result);
};
buttonContinuousScan = FindViewById<Button>(Resource.Id.buttonScanContinuous);
buttonContinuousScan.Click +=
delegate
{
scanner.UseCustomOverlay = false;
//We can customize the top and bottom text of the default overlay
scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away";
scanner.BottomText = "Wait for the barcode to automatically scan!";
var opt = new MobileBarcodeScanningOptions();
opt.DelayBetweenContinuousScans = 3000;
//Start scanning
scanner.ScanContinuously(opt, HandleScanResult);
};
Button flashButton;
View zxingOverlay;
buttonScanCustomView = this.FindViewById<Button>(Resource.Id.buttonScanCustomView);
buttonScanCustomView.Click +=
async delegate
{
//Tell our scanner we want to use a custom overlay instead of the default
scanner.UseCustomOverlay = true;
//Inflate our custom overlay from a resource layout
zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.ZxingOverlay, null);
//Find the button from our resource layout and wire up the click event
flashButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingFlash);
flashButton.Click += (sender, e) => scanner.ToggleTorch();
//Set our custom overlay
scanner.CustomOverlay = zxingOverlay;
//Start scanning!
var result = await scanner.Scan(new MobileBarcodeScanningOptions { AutoRotate = true });
HandleScanResult(result);
};
buttonFragmentScanner = FindViewById<Button>(Resource.Id.buttonFragment);
buttonFragmentScanner.Click += delegate { StartActivity(typeof(FragmentActivity)); };
buttonGenerate = FindViewById<Button>(Resource.Id.buttonGenerate);
buttonGenerate.Click += delegate { StartActivity(typeof(ImageActivity)); };
}
void HandleScanResult(ZXing.Result result)
{
var msg = "";
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = "Found Barcode: " + result.Text;
else
msg = "Scanning Canceled!";
RunOnUiThread(() => Toast.MakeText(this, msg, ToastLength.Short).Show());
}
}
}