Java实现人脸识别和指纹认证

我们在开发中经常会有人脸识别的需求,今天就实现一个简单的人脸识别,调用的第三方SDK服务

0.先去注册服务

登录网址 虹软视觉开放平台—以免费人脸识别技术为核心的人脸识别算法开放平台

93d98274195049d3acbde2c300269103.png

点击进行注册 

进入之后新增我的服务

0bc788a0096c4f25b83ceaa6d9adfd39.png

成功之后点击首页人脸识别添加服务 

之后填写如下信息

967f960e7ade4db9bfdf38d9243b87f1.png

 下载SDK

b70b3c7ce4f845c4ba9e114f743c7029.png

 之后的话去拉项目(项目现在如果有的话不需要加,没有的话如下)

在IDEA直接拉版本控制即可:GitHub - chengxy-nds/ArcSoftFaceDemo: ArcSoft基于虹软人脸识别2.0 Java服务端Demo代码,最完整的服务端Demo。

(1)把包放到lib文件夹

aa0d43d450ef4cf7b9940732e3f3a8d0.png

(2)加载包

在xml加入以下配置

       <dependency>
            <groupId>com.arcsoft.face</groupId>
            <artifactId>arcsoft-sdk-face</artifactId>
            <version>2.2.0.1</version>
            <scope>system</scope>
            <systemPath>${basedir}/lib/arcsoft-sdk-face-2.2.0.1.jar</systemPath>
        </dependency>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

 之后的话根据表生成sql脚本

官方提供了如图

9cfd2d1f07be4e3da49eee36bb24ea76.png

我用的是 

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user_face_info
-- ----------------------------
DROP TABLE IF EXISTS `user_face_info`;
CREATE TABLE `user_face_info`  (
  `id` int(10) NOT NULL,
  `group_id` int(10) NULL DEFAULT NULL,
  `gace_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `age` int(10) NULL DEFAULT NULL,
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `gender` smallint(10) NULL DEFAULT NULL,
  `phone_number` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `face_feature` blob NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  `fpath` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user_face_info
-- ----------------------------

SET FOREIGN_KEY_CHECKS = 1;

执行生成表之后再项目application.properties修改数据库,用户名和密码

点击启动类运行

之后访问端口http://127.0.0.1:8089/demo就可以看到

fea02c1b7db04d7e9afcdeae3d2932c3.png

如果出现Can't load library: d:/arcsoft_lib\libarcsoft_face.dll这个错误说明你缺少这个dll文件,添加即可

0334ae8a7a504ed793b9e5449a77389d.png

解压到D盘 

有两种一种是摄像头识别,另一种事照片识别,这样就可以实现人脸识别(开发环境使用第三方SDK比较方便)

接下来是指纹认证,这边只写了一个简单的模版实现指纹认证,如下

public class CustomFingerprintComparison {

    // 模拟指纹模板
    private static final byte[] fingerprintTemplate1 = new byte[] { 1, 2, 3, 4, 5 ,1};
    private static final byte[] fingerprintTemplate2 = new byte[] { 5, 24, 34, 6, 5 ,3 }; // 改变一个像素值

    // 设置比对阈值
    private static final int threshold = 3;

    public static void main(String[] args) {
        // 模拟指纹比对
        boolean isMatch = matchFingerprints(fingerprintTemplate1, fingerprintTemplate2);
        double similarity = calculateSimilarity(fingerprintTemplate1, fingerprintTemplate2);

        if (isMatch) {
            System.out.print("指纹匹配,认证通过 ");
        } else {
            System.out.print("指纹不匹配,认证失败 ");
        }

        System.out.println("指纹相似度: " + similarity + "%");
    }

    // 模拟指纹比对函数
    private static boolean matchFingerprints(byte[] template1, byte[] template2) {
        // 比对两个指纹模板,计算差异值
        int difference = calculateDifference(template1, template2);

        // 如果差异值低于阈值,认为指纹匹配
        return difference <= threshold;
    }

    // 计算指纹相似度函数
    private static double calculateSimilarity(byte[] template1, byte[] template2) {
        // 计算相似度百分比
        int difference = calculateDifference(template1, template2);
        int maxPossibleDifference = template1.length * 255; // 假设最大可能的差异值
        int similarityPercentage = ((maxPossibleDifference - difference) * 100) / maxPossibleDifference;
        return similarityPercentage;
    }

    // 模拟计算差异值函数
    private static int calculateDifference(byte[] template1, byte[] template2) {
        // 模拟计算两个指纹模板的差异值,实际情况下需要使用专业库或API
        int difference = 0;
        for (int i = 0; i < template1.length; i++) {
            difference += Math.abs(template1[i] - template2[i]);
        }
        return difference;
    }
}

结果如下

9ef4c41af7c54b0481711188d91d71d7.png

这样就可以了

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大白猫~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值