-
Notifications
You must be signed in to change notification settings - Fork 703
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Animation on Cancel whilst scanning #345
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<Result> Scan (bool useAVCaptureEngine) | ||
|
@@ -51,63 +59,59 @@ public override Task<Result> Scan (MobileBarcodeScanningOptions options) | |
} | ||
|
||
|
||
public override void ScanContinuously (MobileBarcodeScanningOptions options, Action<Result> scanHandler) | ||
{ | ||
ScanContinuously (options, false, scanHandler); | ||
} | ||
|
||
public void ScanContinuously (MobileBarcodeScanningOptions options, bool useAVCaptureEngine, Action<Result> 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<Result> scanHandler) | ||
{ | ||
ScanContinuously (options, false, scanHandler); | ||
} | ||
|
||
public void ScanContinuously (MobileBarcodeScanningOptions options, bool useAVCaptureEngine, Action<Result> 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<Result> Scan (MobileBarcodeScanningOptions options, bool useAVCaptureEngine) | ||
{ | ||
|
@@ -119,18 +123,15 @@ public Task<Result> 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) | ||
allRequestedFormatsSupported = AVCaptureScannerView.SupportsAllRequestedBarcodeFormats(options.PossibleFormats); | ||
|
||
this.appController.InvokeOnMainThread(() => { | ||
|
||
|
||
if (useAVCaptureEngine && is7orgreater && allRequestedFormatsSupported) | ||
{ | ||
viewController = new AVCaptureScannerViewController(options, this); | ||
|
@@ -147,24 +148,24 @@ public Task<Result> 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment conflicts with the code. On ios7 value is true - is it ok? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, should have changed the comment. On iOS7 value of is7orgreater should be false and I've just looked at my check in the Initialise method and it's wrong, should be:
|
||
}); | ||
} | ||
|
||
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; } | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be replaced with UIDevice.CurrentDevice.CheckSystemVersion(7, 0)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have realised my check is wrong here. In haste I wrote greater than or equal to 7 but in actual fact it should only be greater than 7. Do you think I could use this to check that my version is greater than 7:
Since there is no version 7.2 of iOS it should in theory give me the expected result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use CheckSystemVersion(8, 0) then.