Android集成百度人脸识别(一)基础版SDK

首先Android Studio版本:3.2.0

1、注册百度账号并企业认证
2、创建应用生成API Key和Secret Key
3、下载对应的SDK(下载SDK的时候需要新建授权
因为下载的时候需要选择授权文件(授权文件包含包名和签名文件的MD5)
在这里插入图片描述
如下即可下载SDK
在这里插入图片描述
我们以基础版和人脸通行示例工程为例

下载下来并解压然后导入工程:
在这里插入图片描述
刚导入会弹出一个提示:
在这里插入图片描述
我这边选择update,然后gradle-wrapper.properties文件就变成了4.6的版本:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

并报错了:

Could not find com.android.tools.build:aapt2:3.2.0-4818971.
Searched in the following locations:
    file:/F:/Android/sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
    file:/F:/Android/sdk/extras/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
    file:/F:/Android/sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
    file:/F:/Android/sdk/extras/google/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
    file:/F:/Android/sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
    file:/F:/Android/sdk/extras/android/m2repository/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
    https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971.pom
    https://jcenter.bintray.com/com/android/tools/build/aapt2/3.2.0-4818971/aapt2-3.2.0-4818971-windows.jar
Required by:
    project :facesdk

因为升级了版本,新版本需要在最上级的build.gralde增加谷歌库 解决问题
(最简单的方法,重新创建一个项目,copy过来最靠谱)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

再次build发现出现了这些错误:
在这里插入图片描述
这些错误可以忽略,不影响我们运行,下面我们开始运行项目,又报错了:
在这里插入图片描述
什么?R不存在?什么鬼
再认真看看我们的包名和build.gradle中的applicationId发现

applicationId  "com.xx.facedemo" 

//我的manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xx.facedemo">

再对比下工程文件中导入的包:
在这里插入图片描述

怎么回事?两边竟然不一样。
我们就找到问题了,R文件需要重新导入才可以。
那我们每个java文件依次重新导入一下R后是这样的:

import com.xx.facedemo.R;

所有的java类都重新导入后,没问题了,不报错了。

温馨提示(一)修改Config.java中key

这个类中需要填写百度申请的key:

/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
package com.baidu.aip.fl;


public class Config {

    // 为了apiKey,secretKey为您调用百度人脸在线接口的,如注册,识别等。
    // 为了的安全,建议放在您的服务端,端把人脸传给服务器,在服务端端进行人脸注册、识别放在示例里面是为了您快速看到效果
    public static String apiKey = 替换为你的apiKey(ak);
    public static String secretKey = 替换为你的secretKey(sk);
    public static String licenseID = "facedemos-face-android";
    public static String licenseFileName = "替换为你的licenseFileName";


    /**
     * groupId,标识一组用户(由数字、字母、下划线组成),长度限制128B,可以自行定义,只要注册和识别都是同一个组。
     * 详情见 http://ai.baidu.com/docs#/Face-API/top
     * <p>
     * 人脸识别 接口 https://aip.baidubce.com/rest/2.0/face/v2/identify
     * 人脸注册 接口 https://aip.baidubce.com/rest/2.0/face/v2/faceset/user/add
     */

    public static String groupID = 替换为groupID;

}

温馨提示(二)修改build.gradle中的签名参数

/*
 * Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
 */
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId  "com.zhiao.facedemo"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    signingConfigs {


        def alias = "您的keyAlias"
        def password = "您的keyPassword"
        def filePath = "您的storeFile"  //签名文件路径 如:../faceprint.jks

        debug {
            keyAlias alias
            keyPassword password
            storeFile file(filePath)
            storePassword(password)
        }
        release {
            keyAlias alias
            keyPassword password
            storeFile file(filePath)
            storePassword(password)
        }
    }
}

repositories {
    flatDir {
        dirs 'libs', project(':facesdk').file('libs')
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile project(":facesdk")
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'
    compile 'cat.ereza:customactivityoncrash:1.5.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'


}

都配置好了以后就会看到这个提示了,哈哈。
在这里插入图片描述

发布了55 篇原创文章 · 获赞 93 · 访问量 33万+

猜你喜欢

转载自blog.csdn.net/zx_android/article/details/88740768