-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMainActivity.cs
170 lines (143 loc) · 7.97 KB
/
MainActivity.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content.PM;
using Android.Content;
using Com.Microblink.Entities.Recognizers.Blinkid.Generic;
using Com.Microblink.Entities.Recognizers;
using Com.Microblink.Util;
using Com.Microblink;
using Com.Microblink.Intent;
using Com.Microblink.Uisettings;
using Android.Runtime;
namespace Android
{
[Activity(Label = "BlinkID Xamarin", MainLauncher = true, Icon = "@mipmap/icon", HardwareAccelerated = true)]
public class MainActivity : Activity
{
const int ACTIVITY_REQUEST_ID = 101;
// BlinkIdCombinedRecognizer is used for automatic classification and data extraction from the supported
// document
BlinkIdCombinedRecognizer blinkidRecognizer;
// there are plenty of recognizers available - see Android documentation
// for more information: https://github.com/BlinkID/blinkid-android/blob/master/README.md
// RecognizerBundle is required for transferring recognizers via Intent to another activity
// and for loading results from them back.
RecognizerBundle recognizerBundle;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
RequestedOrientation = ScreenOrientation.Portrait;
Button button = FindViewById<Button>(Resource.Id.startScanningButton);
// Setup BlinkID before usage
initBlinkId();
// check if BlinkID is supported on current device. Device needs to have camera with autofocus.
if (RecognizerCompatibility.GetRecognizerCompatibilityStatus(this) != RecognizerCompatibilityStatus.RecognizerSupported)
{
button.Enabled = false;
Toast.MakeText(this, "BlinkID is not supported!", ToastLength.Long).Show();
}
else
{
button.Click += delegate {
// create a settings object for activity that will be used. For ID it's best to
// use DocumentUISettings. There are also other UI settings available - check Android documentation
var blinkidUISettings = new BlinkIdUISettings(recognizerBundle);
// start activity associated with given UI settings. After scanning completes,
// OnActivityResult will be invoked
ActivityRunner.StartActivityForResult(this, ACTIVITY_REQUEST_ID, blinkidUISettings);
};
}
}
private void initBlinkId()
{
// set license key for Android with package name com.microblink.sample
MicroblinkSDK.SetLicenseKey("sRwAAAAVY29tLm1pY3JvYmxpbmsuc2FtcGxlU9kJdZhZkGlTu9XHOXtHZ5sEIxfrd8QRtGUpBIN4H59/oIIoDep7g5k2bvWM6ectjLQNLLa+8uVtpNekwbLYvD+HFqTXNY78QiDkaZovM6KnsU9ChZhZOoM1du4CJwbGrzRq/+xBNsI6hPWP0PCJyWPADScdvMzlS7hJd4l4IsuPJ2wuUprFFdE9woA68ojyCeu0jj9/5mBfZMqLZ7tZPGzji5q+ZTjqL9ZHHI7ZNHj/SFoOOdH29/fGqW0efquPs+QLtF75pbJ0+rUpTVQH5apFVM9BOvMprs0gE975dNfpgjfBSBtahqqW9XTMlp79Th1/Ht6+w+rYKZUi7O24WBHqUA==", this);
// Since we plan to transfer large data between activities, we need to enable
// PersistedOptimised intent data transfer mode.
// for more information about transfer mode, check android documentation: https://github.com/blinkid/blinkid-android#-passing-recognizer-objects-between-activities
MicroblinkSDK.IntentDataTransferMode = IntentDataTransferMode.PersistedOptimised;
// create recognizers and bundle them into RecognizerBundle
blinkidRecognizer = new BlinkIdCombinedRecognizer();
blinkidRecognizer.SetReturnFullDocumentImage(true);
blinkidRecognizer.SetReturnFaceImage(true);
recognizerBundle = new RecognizerBundle(blinkidRecognizer);
}
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == ACTIVITY_REQUEST_ID && resultCode == Result.Ok)
{
// obtain image view that will display image of the document
ImageView documentImageFrontView = this.FindViewById<ImageView>(Resource.Id.documentImageFrontView);
ImageView documentImageBackView = this.FindViewById<ImageView>(Resource.Id.documentImageBackView);
// unfortunately, C# does not support covariant return types, so binding
// of AAR loses the return type of the Java's GetResult method. Therefore, a cast is required.
// This is always a safe cast, since the original object in Java is of correct type - type
// information was lost during conversion to C# due to https://github.com/xamarin/java.interop/pull/216
var blinkidResult = (BlinkIdCombinedRecognizer.Result)blinkidRecognizer.GetResult();
// var mrtdResult = (MrtdRecognizer.Result)mrtdRecognizer.GetResult();
var message = "";
// we can check ResultState property of the Result to see if the result contains scanned information
if (blinkidResult.ResultState == Recognizer.Result.State.Valid)
{
message += "BlinkID recognizer result:\n" +
"FirstName: " + blinkidResult.FirstName + "\n" +
"LastName: " + blinkidResult.LastName + "\n" +
"Address: " + blinkidResult.Address + "\n" +
"DocumentNumber: " + blinkidResult.DocumentNumber + "\n" +
"Sex: " + blinkidResult.Sex + "\n";
var dob = blinkidResult.DateOfBirth.Date;
if (dob != null)
{
message +=
"DateOfBirth: " + dob.Day + "." +
dob.Month + "." +
dob.Year + ".\n";
}
var doi = blinkidResult.DateOfIssue.Date;
if (doi != null)
{
message +=
"DateOfIssue: " + doi.Day + "." +
doi.Month + "." +
doi.Year + ".\n";
}
var doe = blinkidResult.DateOfExpiry.Date;
if (doe != null)
{
message +=
"DateOfExpiry: " + doe.Day + "." +
doe.Month + "." +
doe.Year + ".\n";
}
// there are other fields to extract
// show full document images
if (blinkidResult.FullDocumentFrontImage != null)
{
documentImageFrontView.SetImageBitmap(blinkidResult.FullDocumentFrontImage.ConvertToBitmap());
}
else
{
documentImageFrontView.SetImageResource(0);
}
if (blinkidResult.FullDocumentBackImage != null)
{
documentImageBackView.SetImageBitmap(blinkidResult.FullDocumentBackImage.ConvertToBitmap());
}
else
{
documentImageBackView.SetImageResource(0);
}
}
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.SetTitle("BlinkID Results");
alert.SetPositiveButton("OK", (senderAlert, args) => { });
alert.SetMessage(message);
alert.Show();
}
}
}
}