Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
188 changes: 94 additions & 94 deletions Source/ZXing.Net.Mobile.iOS/MobileBarcodeScanner.cs
Expand Up @@ -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 ()
Expand All @@ -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;
Copy link
Contributor

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)

Copy link
Author

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:

UIDevice.CurrentDevice.CheckSystemVersion(7, 2)

Since there is no version 7.2 of iOS it should in theory give me the expected result.

Copy link
Contributor

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.

}

public Task<Result> Scan (bool useAVCaptureEngine)
Expand All @@ -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)
{
Expand All @@ -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);
Expand All @@ -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);
});
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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:

is7orgreater = sv.Major > 7;

});
}

scanResultResetEvent.Set();
}

Expand All @@ -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 {
Expand All @@ -231,5 +232,4 @@ public override void ResumeAnalysis ()
}
public UIView CustomOverlay { get;set; }
}
}

}