-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewController.cs
156 lines (125 loc) · 6.42 KB
/
ViewController.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using System;
using UIKit;
using BlinkCard;
namespace iOS
{
public partial class ViewController : UIViewController
{
// MBCBlinkCardRecognizer is used to scan all supported credit cards
MBCBlinkCardRecognizer blinkCardRecognizer;
// for more information: https://github.com/BlinkCard/blinkcard-ios
CustomDelegate customDelegate;
public ViewController (IntPtr handle) : base (handle)
{
}
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
customDelegate = new CustomDelegate(this);
// set license key for iOS with bundle ID com.microblink.sample
MBCMicroblinkSDK.SharedInstance().SetLicenseKey("sRwAAAEVY29tLm1pY3JvYmxpbmsuc2FtcGxl1BIcP6dpSuS/37rVPvGgnEXtW6n0WYNXlN/0i1f88yoVpcC6wVI7C9/PwW96iHudfFxZtXdYuU3G3FGWKgCcqkSdZwRtiHrFeYz8beVEwPAGbLMPGidJ8qm5ZtgfLYHJ5NqR0qfIfqKTIDlsGzUY2D2qp3KUfYcscbf9JftuQdMpQ8VfQ8eu0+x1aUckcowsgAfq8/CTF3cpaSF1mBKMCO+idtTRWI8B52aZZDeybQ==", (licenseError) => {
// here, you can check license error
});
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
partial void StartScanningButton_TouchUpInside (UIButton sender)
{
blinkCardRecognizer = new MBCBlinkCardRecognizer();
// create a collection of recognizers that will be used for scanning
var recognizerCollection = new MBCRecognizerCollection(new MBCRecognizer[] { blinkCardRecognizer });
// create a settings object for overlay that will be used. For credit card it's best to use BlinkCardOverlayViewController
var blinkCardOverlaySettings = new MBCBlinkCardOverlaySettings();
blinkCardOverlaySettings.EnableEditScreen = false;
var blinkCardOverlay = new MBCBlinkCardOverlayViewController(blinkCardOverlaySettings, recognizerCollection, customDelegate);
// finally, create a recognizerRunnerViewController
var recognizerRunnerViewController = MBCViewControllerFactory.RecognizerRunnerViewControllerWithOverlayViewController(blinkCardOverlay);
// in ObjC recognizerRunnerViewController would actually be of type UIViewController<MBRecognizerRunnerViewController>*, but this construct
// is not supported in C#, so we need to use Runtime.GetINativeObject to cast obtained IMBCReocognizerRunnerViewController into UIViewController
// that can be presented
this.PresentViewController(ObjCRuntime.Runtime.GetINativeObject<UIViewController>(recognizerRunnerViewController.Handle, false), true, null);
}
class CustomDelegate : MBCBlinkCardOverlayViewControllerDelegate
{
ViewController me;
public CustomDelegate(ViewController me)
{
this.me = me;
}
public override void BlinkCardOverlayViewControllerDidFinishScanning(MBCBlinkCardOverlayViewController blinkCardOverlayViewController, MBCRecognizerResultState state)
{
// this method is called on background processing thread. The scanning will resume as soon
// as this method ends, so in order to have unchanged results at the time of displaying UIAlertView
// pause the scanning
blinkCardOverlayViewController.RecognizerRunnerViewController.PauseScanning();
var title = "Result";
var message = "";
// we can check ResultState property of the Result to see if the result contains scanned information
if (me.blinkCardRecognizer.Result.ResultState == MBCRecognizerResultState.Valid)
{
var blinkCardResult = me.blinkCardRecognizer.Result;
message += "BlinkCard recognizer result:\n" +
BuildResult(blinkCardResult.CardNumber, "Card number") +
BuildResult(blinkCardResult.Owner, "Owner") +
BuildResult(blinkCardResult.ExpiryDate, "Expiry date") +
BuildResult(blinkCardResult.CardNumberValid, "Card number valid") +
BuildResult(blinkCardResult.Cvv, "CVV");
}
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
UIAlertView alert = new UIAlertView()
{
Title = title,
Message = message
};
alert.AddButton("OK");
alert.Show();
// after alert dialog is dismissed, you can either resume scanning with
// documentOverlayViewController.RecognizerRunnerViewController.ResumeScanningAndResetState(true)
// or you can simply dismiss the RecognizerRunnerViewController
alert.Dismissed += (sender, e) => me.DismissViewController(true, null);
});
}
public override void BlinkCardOverlayViewControllerDidTapClose(MBCBlinkCardOverlayViewController blinkCardOverlayViewController)
{
me.DismissViewController(true, null);
}
private string BuildResult(string result, string propertyName)
{
if (result == null || result.Length == 0)
{
return "";
}
return propertyName + ": " + result + "\n";
}
private string BuildResult(Boolean result, string propertyName)
{
if (result)
{
return propertyName + ": YES" + "\n";
}
return propertyName + ": NO" + "\n";
}
private string BuildResult(int result, string propertyName)
{
if (result < 0)
{
return "";
}
return propertyName + ": " + result + "\n";
}
private string BuildResult(MBCDateResult result, string propertyName)
{
if (result == null || result.Year == 0)
{
return "";
}
return propertyName + ": " + result.OriginalDateString + "\n";
}
}
}
}