|
| 1 | +using System; |
| 2 | + |
| 3 | +using Android.App; |
| 4 | +using Android.Content; |
| 5 | +using Android.Content.PM; |
| 6 | +using Android.Runtime; |
| 7 | +using Android.Views; |
| 8 | +using Android.Widget; |
| 9 | +using Android.OS; |
| 10 | + |
| 11 | +using ZxingSharp.Mobile; |
| 12 | + |
| 13 | +namespace ZxingSharp.MonoForAndroid.Sample |
| 14 | +{ |
| 15 | + [Activity (Label = "ZxingSharp", MainLauncher = true, ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.KeyboardHidden)] |
| 16 | + public class Activity1 : Activity |
| 17 | + { |
| 18 | + Button buttonScanCustomView; |
| 19 | + Button buttonScanDefaultView; |
| 20 | + |
| 21 | + ZxingScanner scanner; |
| 22 | + |
| 23 | + protected override void OnCreate (Bundle bundle) |
| 24 | + { |
| 25 | + base.OnCreate (bundle); |
| 26 | + |
| 27 | + // Set our view from the "main" layout resource |
| 28 | + SetContentView (Resource.Layout.Main); |
| 29 | + |
| 30 | + //Create a new instance of our Scanner |
| 31 | + scanner = new ZxingScanner(this); |
| 32 | + |
| 33 | + buttonScanDefaultView = this.FindViewById<Button>(Resource.Id.buttonScanDefaultView); |
| 34 | + buttonScanDefaultView.Click += delegate { |
| 35 | + |
| 36 | + //Tell our scanner to use the default overlay |
| 37 | + scanner.UseCustomOverlay = false; |
| 38 | + //We can customize the top and bottom text of the default overlay |
| 39 | + scanner.TopText = "Hold the camera up to the barcode\nAbout 6 inches away"; |
| 40 | + scanner.BottomText = "Wait for the barcode to automatically scan!"; |
| 41 | + |
| 42 | + //Start scanning |
| 43 | + scanner.StartScanning(ZxingScanningOptions.Default, (barcode) => { |
| 44 | + //Scanning finished callback |
| 45 | + HandleScanResult(barcode); |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + buttonScanCustomView = this.FindViewById<Button>(Resource.Id.buttonScanCustomView); |
| 50 | + buttonScanCustomView.Click += delegate { |
| 51 | + |
| 52 | + //Tell our scanner we want to use a custom overlay instead of the default |
| 53 | + scanner.UseCustomOverlay = true; |
| 54 | + |
| 55 | + //Inflate our custom overlay from a resource layout |
| 56 | + var zxingOverlay = LayoutInflater.FromContext(this).Inflate(Resource.Layout.ZxingOverlay, null); |
| 57 | + |
| 58 | + //Find the button from our resource layout and wire up the click event |
| 59 | + var flashButton = zxingOverlay.FindViewById<Button>(Resource.Id.buttonZxingFlash); |
| 60 | + flashButton.Click += (sender, e) => { |
| 61 | + //Tell our scanner to toggle the torch/flash |
| 62 | + scanner.ToggleTorch(); |
| 63 | + }; |
| 64 | + |
| 65 | + //Set our custom overlay |
| 66 | + scanner.CustomOverlay = zxingOverlay; |
| 67 | + |
| 68 | + //Start scanning! |
| 69 | + scanner.StartScanning(ZxingScanningOptions.Default, (barcode) => { |
| 70 | + //Our scanning finished callback |
| 71 | + HandleScanResult(barcode); |
| 72 | + }); |
| 73 | + |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + void HandleScanResult (ZxingBarcodeResult result) |
| 78 | + { |
| 79 | + string msg = ""; |
| 80 | + |
| 81 | + if (result != null && !string.IsNullOrEmpty(result.Value)) |
| 82 | + msg = "Found Barcode: " + result.Value; |
| 83 | + else |
| 84 | + msg = "Scanning Canceled!"; |
| 85 | + |
| 86 | + Toast.MakeText(this, msg, ToastLength.Short).Show(); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | + |
0 commit comments