23

我想使用人脸解锁作为我的应用程序的第二个因素,因为我的大多数用户不会用密码锁定他们的手机。

是否有可用于在 Android 应用中集成人脸解锁的 Android API?

有用于照片识别的人脸检测 API,但我找不到可以在离线场景中使用的 API,特别是对于应用程序中的附加因素。

如果你需要一个真实世界的例子,假设这是一个密码管理器,或者手机将被借给一个孩子......并且所有者永远不会锁定手机。面部解锁将保护他们需要私密的东西。

4

2 回答 2

11

对于那些不想阅读的人: OpenCV可以完成您在面部识别方面寻找的所有内容。(这里的文档)

这是一个很难回答的问题,因为现有的安卓应用程序很少使用您要求的面部识别技术。但是,您可能需要查看这些网站:

人脸检测软件的好清单

一些可用的主要 api 的体面演练

另一个更高质量的教程...

默认 android FaceDetector 类的文档

看看这个使用 OpenCV 库的示例可能会很有用

我了解无法离线完成此任务的问题。要解决此问题,您始终可以添加“备份”,例如普通密码,该密码仅在发现用户无法访问 Internet 后才会生效。另一种解决方案是只需要一个稳定的互联网/蜂窝连接来让您的应用程序运行。

编辑:不幸的是,

人脸解锁是google的闭源专有代码,所以我们没有机会修改它。来源: http: //forum.xda-developers.com/showthread.php?t=1367610

您可能正在寻找以下信息:

最流行的图像处理库等似乎是 OpenCV,它有一个 Java 包装器,可以在这里找到

您还需要在后台运行它,定期检查用户的面部而没有迹象表明正在发生这种情况,因此在选择库/方法时应牢记这一点

资料来源:不久前,我实施了人脸识别技术作为用户登录我的一个应用程序的一种方式,所以我只是在讲述我在寻找相同问题的答案时所记得的内容

你的场景:

如果你需要一个真实世界的例子,假设这是一个密码管理器,或者手机将被借给一个孩子......并且所有者永远不会锁定手机。面部解锁将保护他们需要私密的东西。

至于实现这一点,如果这就是您所说的“保护他们需要私有的东西”的意思,我会阅读 android 加密。否则,如果您只想使用面部识别而不是密码来创建各种“应用程序锁定”,这会更简单,并且可以使用意图/基本 if 语句等来完成(我假设您已经完成了爪哇)

请随时提出问题。目前我正在寻找我的旧源代码,在那里我做了与你想要的类似的事情,但我怀疑我仍然拥有它......

更新:检查一下……是的,OpenCV 可以离线使用,所以我认为这就是你们正在寻找的

于 2015-08-09T16:03:00.533 回答
1

目前,您可以使用 Biometric API,它在后台检查设备上可用的生物识别类型(面部解锁或指纹),并将完成所有工作,包括处理许多特定于硬件的问题。

因此,从添加依赖项开始:

implementation 'androidx.biometric:biometric:1.0.1'

您可以使用以下方法检查可用性:

val biometricManager = BiometricManager.from(this)
when (biometricManager.canAuthenticate()) {
    BiometricManager.BIOMETRIC_SUCCESS ->
       // App can authenticate using biometrics
    BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE ->
       // No biometric features available on this device
    BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE ->
       // Biometric features are currently unavailable
    BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED ->
       // The user hasn't associated any biometric credentials with their account
}

使用为您提供的系统对话框:

private lateinit var executor: Executor
private lateinit var biometricPrompt: BiometricPrompt
private lateinit var promptInfo: BiometricPrompt.PromptInfo

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    executor = ContextCompat.getMainExecutor(this)
    biometricPrompt = BiometricPrompt(this, executor,
            object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int,
                errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            // Authentication error
        }

        override fun onAuthenticationSucceeded(
                result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            // Authentication succeeded!
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            // Authentication failed
        }
    })

    promptInfo = 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.
    biometricLoginButton.setOnClickListener {
        biometricPrompt.authenticate(promptInfo)
    }
}

如果您希望您的应用程序解锁,请在面部解锁后按确认(例如,当用户执行购买时) - 这是默认行为。如果您想在不确认的情况下立即解锁应用程序:

// 允许用户在其生物识别凭证被接受后无需执行任何操作(例如按下按钮)即可进行身份验证。

promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        .setNegativeButtonText("Use account password")
        .setConfirmationRequired(false)
        .build()

在此处输入图像描述

此外,您可能需要为用户设置后备以使用设备密码/密码/模式解锁。它是通过以下方式完成的:

promptInfo = BiometricPrompt.PromptInfo.Builder()
        .setTitle("Biometric login for my app")
        .setSubtitle("Log in using your biometric credential")
        // Cannot call setNegativeButtonText() and
        // setDeviceCredentialAllowed() at the same time.
        // .setNegativeButtonText("Use account password")
        .setDeviceCredentialAllowed(true)
        .build()

可以在此处找到有关密码学的其他信息和详细信息:https ://developer.android.com/training/sign-in/biometric-auth

于 2020-07-02T10:54:33.483 回答