android - 如何自动化生物特征识别,如人脸识别和指纹?
问题描述
所以我正在尝试端到端自动化应用程序,它涉及面部识别来存储用户的照片。用户可以使用前置摄像头/后置摄像头,并在几秒钟内检测到面部特征并将其存储。是否有任何工具可用于验证此功能,或者我应该只使用存根来验证集成?
解决方案
实现'androidx.biometric:biometric:1.0.1'
private Executor executor;
private BiometricPrompt biometricPrompt;
private BiometricPrompt.PromptInfo promptInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* BiometricManager biometricManager = BiometricManager.from(this);
switch (biometricManager.canAuthenticate()) {
case BiometricManager.BIOMETRIC_SUCCESS:
Log.d("MY_APP_TAG", "App can authenticate using biometrics.");
break;
case BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE:
Log.e("MY_APP_TAG", "No biometric features available on this device.");
break;
case BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE:
Log.e("MY_APP_TAG", "Biometric features are currently unavailable.");
break;
case BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED:
Log.e("MY_APP_TAG", "The user hasn't associated " +
"any biometric credentials with their account.");
break;
}*/
executor = ContextCompat.getMainExecutor(this);
biometricPrompt = new BiometricPrompt(MainActivity.this,
executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onAuthenticationError(int errorCode,
@NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getApplicationContext(),
"Authentication error: " + errString, Toast.LENGTH_SHORT)
.show();
}
@Override
public void onAuthenticationSucceeded(
@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(),
"Authentication succeeded!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getApplicationContext(), "Authentication failed",
Toast.LENGTH_SHORT)
.show();
}
});
promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for my app")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use account password")
.build();
// Prompt appears when user clicks "Log in".
// Consider integrating with the keystore to unlock cryptographic operations,
// if needed by your app.
Button biometricLoginButton = findViewById(R.id.biometric_login);
biometricLoginButton.setOnClickListener(view -> {
biometricPrompt.authenticate(promptInfo);
});
}
推荐阅读
- elasticsearch - 日期映射在 Kibana 和 ES 中不起作用
- r - dplyr 按字符串匹配分组
- php - Wordpress 自定义图像裁剪器小部件
- mysql - Mysql 中的 substring_index 分隔符
- openshift - 如何在 openSHIFT ORIGIN 中公开容器化的 asp.net 核心 web api 应用程序
- openssl - 从 PEM 格式证书和 PEM 格式公钥生成 DER 格式公钥
- c# - 如何使用 C# 在 IIS 中启用 HTTPS?
- python - 我怎样才能不丢失我在 Anaconda 3 python 3.6.3 上安装的所有软件包
- reactjs - 在 Typescript React 中声明可选的事件回调
- angular - 获取 Ionic 2 中的 dimensions.contentTop 属性值