This repository was archived by the owner on Mar 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathViewController.cs
More file actions
192 lines (146 loc) · 7.26 KB
/
Copy pathViewController.cs
File metadata and controls
192 lines (146 loc) · 7.26 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using UIKit;
using Microblink;
namespace iOS
{
public partial class ViewController : UIViewController
{
// MBRawParser is used to parse raw text
MBRawParser rawParser;
MBDocumentCaptureRecognizer documentCaptureRecognizer;
// there are plenty of recognizers available - see iOS documentation
// for more information: https://github.com/BlinkInput/blinkinput-ios/blob/master/README.md
CustomDelegate customDelegate;
CustomDocumentCaptureDelegate customDocumentCaptureDelegate;
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);
customDocumentCaptureDelegate = new CustomDocumentCaptureDelegate(this);
// set license key for iOS with bundle ID com.microblink.xamarin.blinkinput
MBMicroblinkSDK.SharedInstance.SetLicenseKey("sRwAAAEhY29tLm1pY3JvYmxpbmsueGFtYXJpbi5ibGlua2lucHV0nQTDQAl4ZhU14Vz0g/pqe9DXYHPmVxK2PMxh1GVTtooenkkGwpeQQ0tQDzqAFbvKPCWrgivNeY7tRkq2IUOdnvrhaea5cfDCSazxegeLudGV/QvNcl5OCk+wupGwnzNtz3P8PVJ7q7TjXrF8skeRDWOowlsGCyHXCF3N3fCeA8mIq0nTXEYhD400l9bZPg==");
}
public override void DidReceiveMemoryWarning ()
{
base.DidReceiveMemoryWarning ();
// Release any cached data, images, etc that aren't in use.
}
partial void StartScanningButton_TouchUpInside (UIButton sender)
{
rawParser = new MBRawParser();
MBScanElement scanElement = new MBScanElement("Raw", rawParser);
scanElement.LocalizedTitle = "Raw Text";
scanElement.LocalizedTooltip = @"Scan text";
// create a collection of MBScanElements that will be used for scanning
var scanElements = new MBScanElement[1];
scanElements[0] = scanElement;
// create a feild by field settings that takes array of scan elements
var fieldByFieldOverlaySettings = new MBFieldByFieldOverlaySettings(scanElements);
var fieldByFieldOverlayVC = new MBFieldByFieldOverlayViewController(fieldByFieldOverlaySettings, customDelegate);
// finally, create a recognizerRunnerViewController
var recognizerRunnerViewController = MBViewControllerFactory.RecognizerRunnerViewControllerWithOverlayViewController(fieldByFieldOverlayVC);
// 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 IMBReocognizerRunnerViewController into UIViewController
// that can be presented
this.PresentViewController(ObjCRuntime.Runtime.GetINativeObject<UIViewController>(recognizerRunnerViewController.Handle, false), true, null);
}
class CustomDelegate : MBFieldByFieldOverlayViewControllerDelegate
{
ViewController me;
public CustomDelegate(ViewController me)
{
this.me = me;
}
public override void FieldByFieldOverlayViewController(MBFieldByFieldOverlayViewController fieldByFieldOverlayViewController, MBScanElement[] scanElements)
{
fieldByFieldOverlayViewController.RecognizerRunnerViewController.PauseScanning();
var title = "Result";
var message = "";
if (scanElements.Length > 0)
{
var rawParserResult = scanElements[0];
message = "Identifier: " + rawParserResult.Identifier + " " + "Value: " + rawParserResult.Value;
}
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 FieldByFieldOverlayViewControllerWillClose(MBFieldByFieldOverlayViewController fieldByFieldOverlayViewController)
{
me.DismissViewController(true, null);
}
}
partial void StartDocumentCaptureButton_TouchUpInside(UIButton sender)
{
documentCaptureRecognizer = new MBDocumentCaptureRecognizer();
documentCaptureRecognizer.ReturnFullDocumentImage = true;
// create a settings object for overlay that will be used. For ID it's best to use DocumentOverlayViewController
// there are also other overlays available - check iOS documentation
var documentCaptureOverlaySettings = new MBDocumentCaptureOverlaySettings();
var documentCaptureOverlay = new MBDocumentCaptureOverlayViewController(documentCaptureOverlaySettings, documentCaptureRecognizer, customDocumentCaptureDelegate);
// finally, create a recognizerRunnerViewController
var recognizerRunnerViewController = MBViewControllerFactory.RecognizerRunnerViewControllerWithOverlayViewController(documentCaptureOverlay);
// 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 IMBReocognizerRunnerViewController into UIViewController
// that can be presented
this.PresentViewController(ObjCRuntime.Runtime.GetINativeObject<UIViewController>(recognizerRunnerViewController.Handle, false), true, null);
}
class CustomDocumentCaptureDelegate : MBDocumentCaptureOverlayViewControllerDelegate
{
ViewController me;
MBImage highResolutionImage;
public CustomDocumentCaptureDelegate(ViewController me)
{
this.me = me;
}
public override void DocumentCaptureOverlayViewControllerDidTapClose(MBDocumentCaptureOverlayViewController documentCaptureOverlayViewController)
{
me.DismissViewController(true, null);
}
public override void DocumentCaptureOverlayViewControllerDidFinishScanning(MBDocumentCaptureOverlayViewController documentCaptureOverlayViewController, MBRecognizerResultState state)
{
if (state == MBRecognizerResultState.Valid)
{
documentCaptureOverlayViewController.RecognizerRunnerViewController.PauseScanning();
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
me.metadataImageView.Image = me.documentCaptureRecognizer.Result.FullDocumentImage.Image;
if (this.highResolutionImage != null)
{
me.highResImageView.Image = this.highResolutionImage.Image;
}
me.DismissViewController(true, null);
});
}
}
public override void DocumentCaptureOverlayViewControllerDidCaptureHighResolutionImage(MBDocumentCaptureOverlayViewController documentCaptureOverlayViewController, MBImage highResImage, MBRecognizerResultState state)
{
this.highResolutionImage = highResImage;
if (state == MBRecognizerResultState.Uncertain)
{
documentCaptureOverlayViewController.RecognizerRunnerViewController.PauseScanning();
UIApplication.SharedApplication.InvokeOnMainThread(delegate
{
me.metadataImageView.Image = this.highResolutionImage.Image;
me.DismissViewController(true, null);
});
}
}
}
}
}