diff --git a/Source/ZXing.Net.Mobile.iOS/MobileBarcodeScanner.cs b/Source/ZXing.Net.Mobile.iOS/MobileBarcodeScanner.cs index 981e3c025..440e5ff16 100644 --- a/Source/ZXing.Net.Mobile.iOS/MobileBarcodeScanner.cs +++ b/Source/ZXing.Net.Mobile.iOS/MobileBarcodeScanner.cs @@ -14,17 +14,18 @@ namespace ZXing.Mobile { - public class MobileBarcodeScanner : MobileBarcodeScannerBase + public class MobileBarcodeScanner : MobileBarcodeScannerBase { //ZxingCameraViewController viewController; IScannerViewController viewController; UIViewController appController; ManualResetEvent scanResultResetEvent = new ManualResetEvent(false); - + private bool is7orgreater = false; public MobileBarcodeScanner (UIViewController delegateController) { appController = delegateController; + Initialise (); } public MobileBarcodeScanner () @@ -37,6 +38,13 @@ public MobileBarcodeScanner () break; } } + Initialise (); + } + + private void Initialise() { + Version sv = new Version (0, 0, 0); + Version.TryParse (UIDevice.CurrentDevice.SystemVersion, out sv); + is7orgreater = sv.Major >= 7; } public Task Scan (bool useAVCaptureEngine) @@ -51,63 +59,59 @@ public override Task Scan (MobileBarcodeScanningOptions options) } - public override void ScanContinuously (MobileBarcodeScanningOptions options, Action scanHandler) - { - ScanContinuously (options, false, scanHandler); - } - - public void ScanContinuously (MobileBarcodeScanningOptions options, bool useAVCaptureEngine, Action scanHandler) - { - try - { - Version sv = new Version (0, 0, 0); - Version.TryParse (UIDevice.CurrentDevice.SystemVersion, out sv); - - var is7orgreater = sv.Major >= 7; - var allRequestedFormatsSupported = true; - - if (useAVCaptureEngine) - allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats); - - this.appController.InvokeOnMainThread(() => { - - - if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) - { - viewController = new AVCaptureScannerViewController(options, this); - viewController.ContinuousScanning = true; - } - else - { - if (useAVCaptureEngine && !is7orgreater) - Console.WriteLine("Not iOS 7 or greater, cannot use AVCapture for barcode decoding, using ZXing instead"); - else if (useAVCaptureEngine && !allRequestedFormatsSupported) - Console.WriteLine("Not all requested barcode formats were supported by AVCapture, using ZXing instead"); - - viewController = new ZXing.Mobile.ZXingScannerViewController(options, this); - viewController.ContinuousScanning = true; - } - - viewController.OnScannedResult += barcodeResult => { - - // If null, stop scanning was called - if (barcodeResult == null) { - ((UIViewController)viewController).InvokeOnMainThread(() => { - ((UIViewController)viewController).DismissViewController(true, null); - }); - } - - scanHandler (barcodeResult); - }; - - appController.PresentViewController((UIViewController)viewController, true, null); - }); - } - catch (Exception ex) - { - Console.WriteLine(ex); - } - } + public override void ScanContinuously (MobileBarcodeScanningOptions options, Action scanHandler) + { + ScanContinuously (options, false, scanHandler); + } + + public void ScanContinuously (MobileBarcodeScanningOptions options, bool useAVCaptureEngine, Action scanHandler) + { + try + { + var allRequestedFormatsSupported = true; + + if (useAVCaptureEngine) + allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats); + + this.appController.InvokeOnMainThread(() => { + + + if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) + { + viewController = new AVCaptureScannerViewController(options, this); + viewController.ContinuousScanning = true; + } + else + { + if (useAVCaptureEngine && !is7orgreater) + Console.WriteLine("Not iOS 7 or greater, cannot use AVCapture for barcode decoding, using ZXing instead"); + else if (useAVCaptureEngine && !allRequestedFormatsSupported) + Console.WriteLine("Not all requested barcode formats were supported by AVCapture, using ZXing instead"); + + viewController = new ZXing.Mobile.ZXingScannerViewController(options, this); + viewController.ContinuousScanning = true; + } + + viewController.OnScannedResult += barcodeResult => { + + // If null, stop scanning was called + if (barcodeResult == null) { + ((UIViewController)viewController).InvokeOnMainThread(() => { + ((UIViewController)viewController).DismissViewController(true, null); + }); + } + + scanHandler (barcodeResult); + }; + + appController.PresentViewController((UIViewController)viewController, true, null); + }); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } + } public Task Scan (MobileBarcodeScanningOptions options, bool useAVCaptureEngine) { @@ -119,10 +123,7 @@ public Task Scan (MobileBarcodeScanningOptions options, bool useAVCaptur Result result = null; - Version sv = new Version (0, 0, 0); - Version.TryParse (UIDevice.CurrentDevice.SystemVersion, out sv); - var is7orgreater = sv.Major >= 7; var allRequestedFormatsSupported = true; if (useAVCaptureEngine) @@ -130,7 +131,7 @@ public Task Scan (MobileBarcodeScanningOptions options, bool useAVCaptur this.appController.InvokeOnMainThread(() => { - + if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) { viewController = new AVCaptureScannerViewController(options, this); @@ -147,24 +148,24 @@ public Task Scan (MobileBarcodeScanningOptions options, bool useAVCaptur viewController.OnScannedResult += barcodeResult => { - ((UIViewController)viewController).InvokeOnMainThread(() => { - - viewController.Cancel(); - - // Handle error situation that occurs when user manually closes scanner in the same moment that a QR code is detected - try { - ((UIViewController) viewController).DismissViewController(true, () => { - result = barcodeResult; - scanResultResetEvent.Set(); - }); - } catch (ObjectDisposedException) { - // In all likelihood, iOS has decided to close the scanner at this point. But just in case it executes the - // post-scan code instead, set the result so we will not get a NullReferenceException. - result = barcodeResult; - scanResultResetEvent.Set(); - } - }); - }; + ((UIViewController)viewController).InvokeOnMainThread(() => { + + viewController.Cancel(); + + // Handle error situation that occurs when user manually closes scanner in the same moment that a QR code is detected + try { + ((UIViewController) viewController).DismissViewController(true, () => { + result = barcodeResult; + scanResultResetEvent.Set(); + }); + } catch (ObjectDisposedException) { + // In all likelihood, iOS has decided to close the scanner at this point. But just in case it executes the + // post-scan code instead, set the result so we will not get a NullReferenceException. + result = barcodeResult; + scanResultResetEvent.Set(); + } + }); + }; appController.PresentViewController((UIViewController)viewController, true, null); }); @@ -190,11 +191,11 @@ public override void Cancel () ((UIViewController)viewController).InvokeOnMainThread(() => { viewController.Cancel(); - // Calling with animated:true here will result in a blank screen when the scanner is closed on iOS 7. - ((UIViewController)viewController).DismissViewController(false, null); + // Calling with animated:true here will result in a blank screen when the scanner is closed on iOS 7. + ((UIViewController)viewController).DismissViewController(is7orgreater, null); }); } - + scanResultResetEvent.Set(); } @@ -214,15 +215,15 @@ public override void AutoFocus () //Does nothing on iOS } - public override void PauseAnalysis () - { - viewController.PauseAnalysis (); - } + public override void PauseAnalysis () + { + viewController.PauseAnalysis (); + } - public override void ResumeAnalysis () - { - viewController.ResumeAnalysis (); - } + public override void ResumeAnalysis () + { + viewController.ResumeAnalysis (); + } public override bool IsTorchOn { get { @@ -231,5 +232,4 @@ public override bool IsTorchOn { } public UIView CustomOverlay { get;set; } } -} - +} \ No newline at end of file