栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

***环境复杂度***:指纹识别

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

***环境复杂度***:指纹识别

***环境复杂度***:指纹识别
    • 一.使用第三方代码
      • 1.第一版
      • 2.第二版:判断
    • 二.原生代码

一.使用第三方代码 1.第一版

依赖:implementation “androidx.biometric:biometric:1.1.0”

//TODO 1:得到用户指纹
 BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                        .setTitle("指纹验证")
                        .setDescription("正在指纹验证中")
                        .setNegativeButtonText("取消")
                        .build();
//TODO 2:校验指纹是否合格
               new BiometricPrompt(this, ContextCompat.getMainExecutor(this), new BiometricPrompt.AuthenticationCallback() {
                   @Override
                   public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                       super.onAuthenticationError(errorCode, errString);
                   }

                   @Override
                   public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                       super.onAuthenticationSucceeded(result);
                       //指纹验证成功
                       ARouter.getInstance().build("/huanxin/HaunXinMainActivity").navigation();


                   }

                   @Override
                   public void onAuthenticationFailed() {
                       super.onAuthenticationFailed();
                   }
               })
                       .authenticate(promptInfo);
2.第二版:判断
public class Main6Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        //几个问题
        //TODO 1:判断手机是否支持:安卓6。0以后支持
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            Toast.makeText(this, "当前手机不支持指纹解锁", Toast.LENGTH_SHORT).show();
            return;
        }
        //TODO 2:判断手机是否有指纹感应区
        FingerprintManager fingerprintManager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
        if(!fingerprintManager.isHardwareDetected()){
            Toast.makeText(this, "当前手机不支持指纹感应区", Toast.LENGTH_SHORT).show();
            return;
        }
        //TODO 3:判断手机是否录入指纹
        if(!fingerprintManager.hasEnrolledFingerprints()){
            Toast.makeText(this, "当前手机没有录入指纹", Toast.LENGTH_SHORT).show();
            return;
        }

        //指纹解锁
        //TODO 1:弹出窗体,提示用户录入指纹
        BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
                .setTitle("指纹验证")
                .setNegativeButtonText("取消")
                .build();
        //TODO 2:校验此指纹是否合格
        new BiometricPrompt(this, new BiometricPrompt.AuthenticationCallback() {
            @Override
            public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
                super.onAuthenticationError(errorCode, errString);
                Toast.makeText(Main6Activity.this, "校验失败", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
                super.onAuthenticationSucceeded(result);
                Toast.makeText(Main6Activity.this, "校验成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAuthenticationFailed() {
                super.onAuthenticationFailed();
            }
        })
                .authenticate(promptInfo);//校验


    }
}

二.原生代码

权限:


代码:

package com.bawei.day9day10;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Build;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.security.keystore.KeyGenParameterSpec;
import android.security.keystore.KeyProperties;
import android.util.Log;
import android.widget.Toast;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Main5Activity extends AppCompatActivity {
 private FingerprintManager manager;
 private KeyStore keyStore;
 private static final String DEFAULT_KEY_NAME = "default_key";
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 init();
 }
 private void init() {
 manager = (FingerprintManager) getSystemService(FINGERPRINT_SERVICE);
 // 判断是否⽀持指纹
 if (isSupportFingerPrint()) {
 initKey();
 initCipher();
 }
 }
 private boolean isSupportFingerPrint() {
 if (Build.VERSION.SDK_INT >= 23) {
 // 权限判断
 if (ActivityCompat.checkSelfPermission(this, Manifest.permission.USE_FINGERPRINT) != PackageManager.PERMISSION_GRANTED) {
 Toast.makeText(this, "没有指纹识别权限", Toast.LENGTH_SHORT).show();
 return false;
 }
 // 硬件判断
 if (!manager.isHardwareDetected()) {
 Toast.makeText(this, "没有指纹识别模块", Toast.LENGTH_SHORT).show();
 return false;
 }
 if (!manager.hasEnrolledFingerprints()) {
 Toast.makeText(this, "您⾄少需要在系统设置中添加⼀个指纹", Toast.LENGTH_SHORT).show();
 return false;
 }
 } else {
 Toast.makeText(this, "您的⼿机暂不⽀持指纹识别", Toast.LENGTH_SHORT).show();
 return false;
 }
 return true;
 }
 private void initKey() {
 try {
 keyStore = KeyStore.getInstance("AndroidKeyStore");
 keyStore.load(null);
 KeyGenerator keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
 KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder(DEFAULT_KEY_NAME,
 KeyProperties.PURPOSE_ENCRYPT |
 KeyProperties.PURPOSE_DECRYPT)
 .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
 .setUserAuthenticationRequired(true)
 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_PKCS7);
 keyGenerator.init(builder.build());
 keyGenerator.generateKey();
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }
 private void initCipher() {
 try {
 SecretKey key = (SecretKey) keyStore.getKey(DEFAULT_KEY_NAME, null);
 Cipher cipher = Cipher.getInstance(KeyProperties.KEY_ALGORITHM_AES + "/"
 + KeyProperties.BLOCK_MODE_CBC + "/"
 + KeyProperties.ENCRYPTION_PADDING_PKCS7);
 cipher.init(Cipher.ENCRYPT_MODE, key);
 showFingerPrintDialog(cipher);
 } catch (Exception e) {
 throw new RuntimeException(e);
 }
 }
 private void showFingerPrintDialog(Cipher cipher) {
 CancellationSignal cancel = new CancellationSignal();
 manager.authenticate(new FingerprintManager.CryptoObject(cipher), cancel, 0, new FingerprintManager.AuthenticationCallback() {
 @Override
 public void onAuthenticationError(int errorCode, CharSequence errString) {
 }
 @Override
 public void onAuthenticationHelp(int helpCode, CharSequence helpString) {
 }
 @Override
 public void onAuthenticationSucceeded(FingerprintManager.AuthenticationResult result) {
 Toast.makeText(Main5Activity.this, "ok", Toast.LENGTH_SHORT).show();
 }
 @Override
 public void onAuthenticationFailed() {
 }
 }, null);
 }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/863426.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号