@@ -77,14 +77,16 @@ public class MyActivity extends AppCompatActivity {
77
77
boolean showGuide = true ;
78
78
// If you want to show the result of the scan to the user set this to true
79
79
boolean showResult = true ;
80
+ // To extract a face that's suitable for face recognition
81
+ boolean detectFaceForRecognition = true ;
80
82
// Set the region where the requested card was issued.
81
83
// If you want the API to guess use the RegionUtil utility class.
82
84
// The utility class will base the region on the device's SIM card provider
83
85
// or on the device's locale. If you want the user to choose the region
84
86
// set the variable to Region.GENERAL.
85
87
Region region = RegionUtil . getUserRegion(this );
86
88
// Construct ID capture settings
87
- VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings (region, showGuide, showResult);
89
+ VerIDIDCaptureSettings settings = new VerIDIDCaptureSettings (region, showGuide, showResult, detectFaceForRecognition );
88
90
// Construct an intent
89
91
VerIDIDCaptureIntent intent = new VerIDIDCaptureIntent (this , settings);
90
92
// Start the ID capture activity
@@ -172,8 +174,12 @@ public class MyActivity extends AppCompatActivity {
172
174
* (for example in response to a button click).
173
175
*/
174
176
void runLivenessDetection () {
177
+ // Create liveness detection settings
178
+ VerIDLivenessDetectionSessionSettings settings = new VerIDLivenessDetectionSessionSettings ();
179
+ // Ask Ver-ID to extract face templates needed for face recognition
180
+ settings. includeFaceTemplatesInResult = true ;
175
181
// Construct the liveness detection intent
176
- VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent (this );
182
+ VerIDLivenessDetectionIntent intent = new VerIDLivenessDetectionIntent (this , settings );
177
183
// Start the liveness detection activity
178
184
startActivityForResult(intent, REQUEST_CODE_LIVENESS_DETECTION );
179
185
}
@@ -220,136 +226,47 @@ public class MyActivity extends AppCompatActivity {
220
226
221
227
Building on example 1 and 2, you can use the results of the ID capture and liveness detection sessions and compare their faces. The activity below contains two ` AsyncTaskLoader ` classes that extract the bitmaps from the results and run the face comparison.
222
228
223
- ** Important:** When requesting the live face you must set ` includeFaceTemplatesInResult ` of the ` VerIDLivenessDetectionSessionSettings ` object to ` true ` to extract the captured face template and make it available for recognition.
229
+ ** Important:** When requesting the live face you must set ` includeFaceTemplatesInResult ` of the ` VerIDLivenessDetectionSessionSettings ` and ` detectFaceForRecognition ` of the ` VerIDIDCaptureSettings ` object to ` true ` to extract the captured face template and make it available for recognition.
224
230
225
231
~~~ java
226
- public class CaptureResultActivity extends AppCompatActivity implements LoaderManager .LoaderCallbacks , FaceTemplateExtraction .FaceTemplateExtractionListener {
227
-
228
- // Loader IDs
229
- private static final int LOADER_ID_SCORE = 458 ;
232
+ public class CaptureResultActivity extends AppCompatActivity {
230
233
231
234
// Live face and ID capture results
232
235
private VerIDSessionResult livenessDetectionResult;
233
236
private IDCaptureResult idCaptureResult;
234
237
235
- /**
236
- * Loader that compares the face from the card with the live face(s)
237
- */
238
- private static class ScoreLoader extends AsyncTaskLoader<Float > {
239
-
240
- private IFace cardFace;
241
- private IFace [] liveFaces;
242
-
243
- public ScoreLoader (Context context , IFace cardFace , IFace [] liveFaces ) {
244
- super (context);
245
- this . cardFace = cardFace;
246
- this . liveFaces = liveFaces;
247
- }
248
-
249
- @Override
250
- public Float loadInBackground () {
251
- Float score = null ;
252
- for (IFace face : liveFaces) {
253
- try {
254
- float faceScore = FaceUtil . compareFaces(cardFace, face);
255
- if (score == null ) {
256
- score = faceScore;
257
- } else {
258
- score = Math . max(faceScore, score);
259
- }
260
- } catch (Exception e) {
261
- e. printStackTrace();
262
- }
263
- }
264
- return score;
265
- }
266
- }
267
-
268
238
@Override
269
239
protected void onCreate (Bundle savedInstanceState ) {
270
240
super . onCreate(savedInstanceState);
271
241
// Set up views
272
242
// Obtain idCaptureResult and livenessDetectionResult, e.g. from intent parcelable
273
243
// ...
274
- if (idCaptureResult != null && idCaptureResult. getFace() != null && livenessDetectionResult != null && livenessDetectionResult. getRecognitionFaces(VerID . Bearing . STRAIGHT ). length > 0 ) {
275
- if (idCaptureResult. getFace(). isSuitableForRecognition()) {
276
- // The card capture result has a suitable face
277
- compareFaceTemplates();
278
- return ;
279
- } else if (idCaptureResult. getFace(). isBackgroundProcessing()) {
280
- // The ID card face is being processed. Listen for face template extraction events
281
- VerID . shared. getFaceTemplateExtraction(). addListener(idCaptureResult. getFace(). getId(), this );
282
- return ;
283
- }
244
+ if (idCaptureResult != null && idCaptureResult. getFace() != null && idCaptureResult. getFace(). isSuitableForRecognition() && livenessDetectionResult != null && livenessDetectionResult. getRecognitionFaces(VerID . Bearing . STRAIGHT ). length > 0 ) {
245
+ compareFaceTemplates();
246
+ return ;
284
247
}
285
248
updateScore(null );
286
249
}
287
250
288
- @Override
289
- protected void onDestroy () {
290
- super . onDestroy();
291
- getLoaderManager(). destroyLoader(LOADER_ID_SCORE );
292
- if (idCaptureResult != null && idCaptureResult. getFace() != null && idCaptureResult. getFace(). isBackgroundProcessing()) {
293
- VerID . shared. getFaceTemplateExtraction(). removeListener(idCaptureResult. getFace(). getId(), this );
294
- }
295
- }
296
-
297
251
private void compareFaceTemplates () {
298
- if (idCaptureResult. getFace(). isSuitableForRecognition()) {
299
- // The face on the ID card is registered, calculate the similarity score
300
- Loader loader = getSupportLoaderManager(). initLoader(LOADER_ID_SCORE , null , this );
301
- if (loader != null ) {
302
- loader. forceLoad();
303
- }
304
- }
305
- }
306
-
307
- @Override
308
- public void onFaceTemplateExtracted (long faceId , FBFace face ) {
309
- VerID . shared. getFaceTemplateExtraction(). removeListener(faceId, this );
310
- if (idCaptureResult. getFace() != null && idCaptureResult. getFace(). isBackgroundProcessing() && idCaptureResult. getFace(). getId() == faceId) {
311
- // Face template has been extracted from the face in the ID card. The face is now ready to be registered
312
- idCaptureResult. setFace(face);
313
- if (idCaptureResult. getFace(). isSuitableForRecognition()) {
314
- compareFaceTemplates();
315
- } else {
316
- updateScore(null );
252
+ Float score = null ;
253
+ IFace [] cardFace = idCaptureResult. getFace();
254
+ IFace [] liveFaces = livenessDetectionResult. getRecognitionFaces(VerID . Bearing . STRAIGHT );
255
+ for (IFace face : liveFaces) {
256
+ try {
257
+ float faceScore = FaceUtil . compareFaces(cardFace, face);
258
+ if (score == null ) {
259
+ score = faceScore;
260
+ } else {
261
+ score = Math . max(faceScore, score);
262
+ }
263
+ } catch (Exception e) {
264
+ e. printStackTrace();
317
265
}
318
266
}
319
- }
320
-
321
- @Override
322
- public void onFaceTemplateExtractionProgress (long faceId , double progress ) {
323
-
324
- }
325
-
326
- @Override
327
- public void onFaceTemplateExtractionFailed (long faceId , Exception exception ) {
328
- VerID . shared. getFaceTemplateExtraction(). removeListener(faceId, this );
329
- updateScore(null );
330
- }
331
-
332
- @Override
333
- public Loader onCreateLoader (int id , Bundle args ) {
334
- if (idCaptureResult. getFace() != null ) {
335
- return new ScoreLoader (this , idCaptureResult. getFace(), livenessDetectionResult. getRecognitionFaces(VerID . Bearing . STRAIGHT ));
336
- } else {
337
- updateScore(null );
338
- return null ;
339
- }
340
- }
341
-
342
- @Override
343
- public void onLoadFinished (Loader loader , Object data ) {
344
- Float score = (Float ) data;
345
267
updateScore(score);
346
268
}
347
269
348
- @Override
349
- public void onLoaderReset (Loader loader ) {
350
-
351
- }
352
-
353
270
@UiThread
354
271
private void updateScore (Float score ) {
355
272
if (score != null ) {
0 commit comments