Develop the function according to the example./*Facial recognition signal cancellation initialization*/
CancellationSignal signal = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
signal = new CancellationSignal();
}
/*Facial recognition callback*/
FaceManager.AuthenticationCallback callback = new FaceManager.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errMsgId, CharSequence errString) {
/* Callback for returning error messages during facial recognition*/
}
@Override
public void onAuthenticationHelp(int helpMsgId, CharSequence helpString) {
/*Callback function called to send a notification during the facial recognition*/
}
@Override
public void onAuthenticationSucceeded(FaceManager.AuthenticationResult result) {
/* Callback for successful facial recognition */
}
@Override
public void onAuthenticationFailed() {
/* Callback for the facial recognition error, indicating the current facial recognition fails */
}
};
/*Launch a facial recognition*/
if (faceManager != null) {
boolean isSupport = false;
boolean isEnrolled = false;
boolean is3D = false;
try {
/* Obtains the facial recognition ability */
FaceManager.FaceRecognitionCapability capability = faceManager.getFaceRecognitionCapability();
if (capability == null) {
Log.e(TAG, "capability is null");
return;
}
isSupport = capability.isSupport;
isEnrolled = capability.isEnrolled;
is3D = capability.is3D;
} catch (NoSuchMethodError e) {
/* In EMUI 11.0.1 and earlier versions, the getFaceRecognitionCapability method does not exist. Use the old API to obtain the facial recognition capability */
isSupport = faceManager.isHardwareDetected();
isEnrolled = faceManager.hasEnrolledTemplates();
/* In versions earlier than EMUI 11.0.1, only 3D facial recognition is supported */
is3D = true;
}
/*Check whether the current device supports facial recognition */
if (isSupport) {
/* Obtain information on whether the facial profile data has been enrolled.*/
if (isEnrolled) {
/* Obtain information on whether the 3D facial recognition is supported with the current device*/
modality.setText("Face Recognition Modality: " + (is3D ? "3D" : "2D"));
/*Call the facial recognition feature* /
faceManager.authenticate(null, signal, 0, callback, null);
} else {
ShowToastUtils.showToast(MainActivity.this, "No enrolled face", Toast.LENGTH_SHORT);
}
} else {
ShowToastUtils.showToast(MainActivity.this, "This device does not support face recognition",
Toast.LENGTH_SHORT);
}
}