@@ -17,29 +17,15 @@ The Ver-ID Credentials SDK allows your app to capture an image of the user's ID
17
17
1. Under `dependencies` add
18
18
19
19
```
20
- implementation 'com.appliedrec:id-capture:5 .0.4 '
20
+ implementation 'com.appliedrec:id-capture:6 .0.0 '
21
21
```
22
- 1. Under `android/defaultConfig` add
23
-
24
- ```
25
- renderscriptTargetApi 18
26
- renderscriptSupportModeEnabled true
27
- ```
28
22
1. Open your app's **AndroidManifest.xml** file and add the following tag in `<application>` replacing `[your API secret]` with the API secret your received in step 1:
29
23
30
24
~~~xml
31
25
<meta-data
32
26
android:name="com.appliedrec.verid.apiSecret"
33
27
android:value="[your API secret]" />
34
28
~~~
35
- 1. [Download resources archive](https://ver-id.s3.amazonaws.com/resources/models/v1/VerIDModels.zip) and put it in your app's **assets** folder.
36
- 1. As an alternative to the previous step, specify a URL from which to download the resources. This will reduce the download size of your app. In the app's manifest file:
37
-
38
- ~~~xml
39
- <meta-data
40
- android:name="com.appliedrec.verid.resourcesURL"
41
- android:value="http://my.domain.com/path/to/resources.zip" />
42
- ~~~
43
29
44
30
## Getting Started with the Ver-ID Credentials API
45
31
To scan an ID card your app will start an activity with a Ver-ID ID Capture intent and receive the result in `onActivityResult`.
@@ -57,29 +43,26 @@ To scan an ID card your app will start an activity with a Ver-ID ID Capture inte
57
43
public class MyActivity extends AppCompatActivity {
58
44
59
45
private static final int REQUEST_CODE_ID_CAPTURE = 0;
46
+ private VerID verID;
60
47
61
48
@Override
62
49
protected void onCreate(Bundle savedInstanceState) {
63
50
super.onCreate(savedInstanceState);
64
51
setContentView(R.layout.my_activity);
65
- VerID.shared.load(this, "myApiSecret", new VerID.LoadCallback() {
66
- @Override
67
- public void onLoad() {
52
+ new VerIDFactory(this, new VerIDFactoryDelegate() {
53
+ @Override
54
+ public void veridFactoryDidCreateEnvironment(VerIDFactory verIDFactory, VerID verID) {
55
+ MyActivity.this.verID = verID;
68
56
// Ver-ID is now loaded. Here you may do things
69
57
// like enable buttons that control the ID capture.
70
- }
71
- @Override
72
- public void onError(Exception exception) {
73
- // Ver-ID failed to load. Check your API secret.
74
- }
75
- });
76
- }
77
-
78
- @Override
79
- protected void onDestroy() {
80
- super.onDestroy();
81
- // Unload Ver-ID when your activity gets destroyed
82
- VerID.shared.unload();
58
+ }
59
+
60
+ @Override
61
+ public void veridFactoryDidFailWithException(VerIDFactory verIDFactory, Exception e) {
62
+ loadingIndicatorView.setVisibility(View.GONE);
63
+ Toast.makeText(MainActivity.this, R.string.verid_failed_to_load, Toast.LENGTH_SHORT).show();
64
+ }
65
+ }).createVerID();
83
66
}
84
67
85
68
/**
@@ -91,18 +74,16 @@ public class MyActivity extends AppCompatActivity {
91
74
boolean showGuide = true;
92
75
// If you want to show the result of the scan to the user set this to true
93
76
boolean showResult = true;
94
- // To extract a face that's suitable for face recognition
95
- boolean detectFaceForRecognition = true;
96
77
// Set the region where the requested card was issued.
97
78
// If you want the API to guess use the RegionUtil utility class.
98
79
// The utility class will base the region on the device's SIM card provider
99
80
// or on the device's locale. If you want the user to choose the region
100
81
// set the variable to Region.GENERAL.
101
- Region region = RegionUtil.getUserRegion (this);
82
+ IDDocument document = RegionUtil.getDefaultDocumentForUserRegion (this);
102
83
// Construct ID capture settings
103
- VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings(region , showGuide, showResult, detectFaceForRecognition );
84
+ VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings(document , showGuide, showResult);
104
85
// Construct an intent
105
- VerIDIDCaptureIntent intent = new VerIDIDCaptureIntent(this, settings);
86
+ VerIDIDCaptureIntent intent = new VerIDIDCaptureIntent(this, verID, settings);
106
87
// Start the ID capture activity
107
88
startActivityForResult(intent, REQUEST_CODE_ID_CAPTURE);
108
89
}
@@ -164,42 +145,37 @@ public class MyActivity extends AppCompatActivity {
164
145
public class MyActivity extends AppCompatActivity {
165
146
166
147
private static final int REQUEST_CODE_LIVENESS_DETECTION = 1;
148
+ private VerID verID;
167
149
168
150
@Override
169
151
protected void onCreate(Bundle savedInstanceState) {
170
152
super.onCreate(savedInstanceState);
171
153
setContentView(R.layout.my_activity);
172
- VerID.shared.load(this, "myApiSecret", new VerID.LoadCallback() {
173
- @Override
174
- public void onLoad() {
154
+ new VerIDFactory(this, new VerIDFactoryDelegate() {
155
+ @Override
156
+ public void veridFactoryDidCreateEnvironment(VerIDFactory verIDFactory, VerID verID) {
157
+ MyActivity.this.verID = verID;
175
158
// Ver-ID is now loaded. Here you may do things
176
159
// like enable buttons that control the ID capture.
177
- }
178
- @Override
179
- public void onError(Exception exception) {
180
- // Ver-ID failed to load. Check your API secret.
181
- }
182
- });
183
- }
184
-
185
- @Override
186
- protected void onDestroy() {
187
- super.onDestroy();
188
- // Unload Ver-ID when your activity gets destroyed
189
- VerID.shared.unload();
190
- }
160
+ }
161
+
162
+ @Override
163
+ public void veridFactoryDidFailWithException(VerIDFactory verIDFactory, Exception e) {
164
+ loadingIndicatorView.setVisibility(View.GONE);
165
+ Toast.makeText(MainActivity.this, R.string.verid_failed_to_load, Toast.LENGTH_SHORT).show();
166
+ }
167
+ }).createVerID();
168
+ }
191
169
192
170
/**
193
171
* Call this method to start the liveness detection session
194
172
* (for example in response to a button click).
195
173
*/
196
174
void runLivenessDetection() {
197
175
// Create liveness detection settings
198
- VerIDLivenessDetectionSessionSettings settings = new VerIDLivenessDetectionSessionSettings();
199
- // Ask Ver-ID to extract face templates needed for face recognition
200
- settings.includeFaceTemplatesInResult = true;
176
+ LivenessDetectionSessionSettings settings = new LivenessDetectionSessionSettings();
201
177
// Construct the liveness detection intent
202
- VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent(this, settings);
178
+ VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent(this, verID, settings);
203
179
// Start the liveness detection activity
204
180
startActivityForResult(intent, REQUEST_CODE_LIVENESS_DETECTION);
205
181
}
@@ -212,16 +188,16 @@ public class MyActivity extends AppCompatActivity {
212
188
super.onActivityResult(requestCode, resultCode, data);
213
189
if (requestCode == REQUEST_CODE_LIVENESS_DETECTION && resultCode == RESULT_OK && data != null) {
214
190
// Extract the liveness detection session result from the data intent
215
- VerIDSessionResult verIDSessionResult = data.getParcelableExtra(VerIDActivity.EXTRA_SESSION_RESULT );
216
- if (verIDSessionResult == null || ! verIDSessionResult.isPositive() ) {
191
+ VerIDSessionResult verIDSessionResult = data.getParcelableExtra(VerIDSessionActivity.EXTRA_RESULT );
192
+ if (verIDSessionResult == null || verIDSessionResult.getError() != null ) {
217
193
return;
218
194
}
219
195
// Get the face and image URI of the first captured selfie
220
- HashMap<VerIDFace ,Uri> faceImages = verIDSessionResult.getFaceImages(VerID. Bearing.STRAIGHT);
196
+ HashMap<Face ,Uri> faceImages = verIDSessionResult.getFaceImages(Bearing.STRAIGHT);
221
197
if (faceImages.isEmpty()) {
222
198
return;
223
199
}
224
- Map.Entry<VerIDFace ,Uri> faceImage = faceImages.entrySet().iterator().next();
200
+ Map.Entry<Face ,Uri> faceImage = faceImages.entrySet().iterator().next();
225
201
// Get the bounds of the face detected in the first selfie
226
202
Rect faceBounds = new Rect();
227
203
faceImage.getKey().getBoundingBox().round(faceBounds);
@@ -264,51 +240,58 @@ public class CaptureResultActivity extends AppCompatActivity {
264
240
// Obtain idCaptureResult and livenessDetectionResult, e.g. from intent parcelable
265
241
// ...
266
242
if (document == null || livenessDetectionResult == null) {
267
- updateScore(null);
268
- }
269
-
270
- if (document.getFaceSuitableForRecognition() != null && livenessDetectionResult.getFacesSuitableForRecognition(VerID.Bearing.STRAIGHT).length > 0) {
271
- compareFaceTemplates();
243
+ setAuthenticated(null);
244
+ }
245
+ if (document.getFaceSuitableForRecognition() != null && livenessDetectionResult.getFacesSuitableForRecognition(Bearing.STRAIGHT).length > 0) {
246
+ new VerIDFactory(this, new VerIDFactoryDelegate() {
247
+ @Override
248
+ public void veridFactoryDidCreateEnvironment(VerIDFactory verIDFactory, VerID verID) {
249
+ compareFaceTemplates(verID);
250
+ }
251
+
252
+ @Override
253
+ public void veridFactoryDidFailWithException(VerIDFactory verIDFactory, Exception e) {
254
+ Toast.makeText(MainActivity.this, R.string.verid_failed_to_load, Toast.LENGTH_SHORT).show();
255
+ }
256
+ }).createVerID();
272
257
return;
273
258
}
274
- updateScore (null);
259
+ setAuthenticated (null);
275
260
}
276
261
277
- private void compareFaceTemplates() {
262
+ private void compareFaceTemplates(VerID verId ) {
278
263
AsyncTask.execute(new Runnable() {
279
264
@Override
280
265
public void run() {
281
266
Float score = null;
282
- VerIDFace cardFace = document.getFaceSuitableForRecognition();
283
- VerIDFace[] liveFaces = livenessDetectionResult.getFacesSuitableForRecognition(VerID.Bearing.STRAIGHT);
284
- for (VerIDFace face : liveFaces) {
285
- try {
286
- float faceScore = FaceUtil.compareFaces(cardFace, face);
287
- if (score == null) {
288
- score = faceScore;
289
- } else {
290
- score = Math.max(faceScore, score);
291
- }
292
- } catch (Exception e) {
293
- e.printStackTrace();
294
- }
267
+ RecognizableFace cardFace = document.getFaceSuitableForRecognition();
268
+ RecognizableFace[] liveFaces = livenessDetectionResult.getFacesSuitableForRecognition(Bearing.STRAIGHT);
269
+ try {
270
+ float score = verID.getFaceRecognition().compareSubjectFacesToFaces(new RecognizableFace[]{cardFace}, liveFaces);
271
+ float threshold = verID.getFaceRecognition().getAuthenticationThreshold();
272
+ // If score >= threshold the user can be considered authenticated against the ID card
273
+ final boolean authenticated =
274
+ runOnUiThread(new Runnable() {
275
+ @Override
276
+ public void run() {
277
+ updateScore(score);
278
+ }
279
+ });
280
+ } catch (Exception e) {
281
+ runOnUiThread(new Runnable() {
282
+ @Override
283
+ public void run() {
284
+ setAuthenticated(null);
285
+ }
286
+ });
295
287
}
296
- final Float finalScore = score;
297
- runOnUiThread(new Runnable() {
298
- @Override
299
- public void run() {
300
- updateScore(finalScore);
301
- }
302
- });
303
288
}
304
289
});
305
290
}
306
291
307
292
@UiThread
308
- private void updateScore(Float score) {
309
- if (score != null) {
310
- // Display the score
311
- } else {
293
+ private void setAuthenticated(Boolean authenticated) {
294
+ if (authenticated == null) {
312
295
// Display error
313
296
}
314
297
}
0 commit comments