集成指南

1.获取SDK压缩包

请联系您的Linkface专属商务获取SDK Demo压缩包;

商务邮箱:market@linkface.cn

2.SDK目录结构

1.LFLivenessDemo:活体检测demo主项目

2.LFLivenessUILib: 活体检测Module,包含姿态检测UI层和底层检测aar包

3.libs: 底层检测aar包

目录1

3.Demo示例

打开Demo 1.使用Android Studio打开SDK压缩包中的项目,活体检测示例Demo。

2.然后在LFLivenessSample/build.gradle中applicationId的值修改成apiId绑定的包名。

3.在MyApplication中设置包名对应的apiId和apiSecret:

    private static final String LF_API_ID = "#######";
    private static final String LF_API_SECRET = "#######";

    public void onCreate() {
        super.onCreate();
        //初始化活体检测 SDK
        LFLivenessSDK.getInstance(this).init(LF_API_ID, LF_API_SECRET);
    }

完成以上步骤,即可运行体验Demo功能了。

4.将SDK集成到开发环境

使用SDK前,首先需要将其集成到您的开发环境中。

4.1 将SDK集成到开发环境

4.1.1 module集成方式

1.将LFLiveness下的module模块LFLivenessUILib以module的方式导入工程,通过File->New>importModule->选中LFLivenessUILib->连续点击Next完成导入。 导入module

最后,点击finish,等待导入module完成。

2.在新建应用的builde.gradle下添加 如下

  repositories {
    flatDir {
        dirs '../../LFLivenessUILib/LFLivenessUI/libs'
    }
  }

  dependencies {
        compile project(path: ':LFLivenessUILib:LFLivenessUI')
  }

3.确保 app/build.gradle中applicationId的值与apiId绑定的包名一致。

4.1.2 aar集成方式

更灵活的集成方式,获取aar文件内容进行集成

如果您的开发环境是Android Studio,只需要将aar包导入到app/libs中,在app/build.gradle中添加:

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile(name: 'LFLivenessSDK', ext: 'aar')
}

另外,将assets文件夹下的模型文件拷贝到 app/src/main/assets 目录中。

如果您的开发环境是Eclipse,则按照如下步骤进行集成:

1.将LFLivenessUILib/LivenessUI/libs下LFLivenessSDK.aar文件后缀重命名为.zip文件,解压。目录结构如图 目录

2.将解压目录中assets文件夹下的模型文件拷贝到 app/src/main/assets 目录中

3.将解压目录中jni文件夹下的so文件夹拷贝到 app/src/main/jniLibs 目录中

4.将 classes.jar 文件拷贝 app/libs 目录下。在builde.gradle添加依赖。注:可重命名该文件。

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

4.2 配置 AndroidManifest.xml 文件

在 AndroidManifest.xml 中增加权限,代码如下:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

权限说明:

名称 用途
CAMERA 调用相机进行扫描或拍照
VIBRATE 调用手机震动器再识别成功时发出震动
WRITE_EXTERNAL_STORAGE 拍摄照片时的临时存储
INTERNET 访问网络权限,用于online版本解密识别结果或License下载时
ACCESS_NETWORK_STATE 获取网络状态

动态申请系统权限。Android6.0之后需要动态申请权限。在相应权限使用之前,请添加相应权限的申请。此处给出开始活体检测时,CAMERA及写权限的判断和动态申请示例。位于MainTestActivity.java中,代码如下:

//常量定义
    private static final int PERMISSION_REQUEST_CAMERA = 0;
    private static final int PERMISSION_REQUEST_SD_WRITE = 1;
    private static final int PERMISSION_REQUEST_WRITE = 3;
        /**
     * 开始检测
     */
    private void onClickStartDetectLiveness() {
        if (isMarshmallow()) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                // Permission has not been granted and must be
                // requested.
                if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                    // Provide an additional rationale to the user if
                    // the permission was not granted
                    // and the user would benefit from additional
                    // context for the use of the permission.
                }
                // Request the permission. The result will be received
                // in onRequestPermissionResult()
                requestPermissions(
                        new String[]{Manifest.permission.CAMERA},
                        PERMISSION_REQUEST_CAMERA);
            } else {
                // Permission is already available, start camera preview
                requestWriteSdPermission();
            }
        } else {
            requestWriteSdPermission();
        }
    }

        private void requestWriteSdPermission() {
        if (isMarshmallow()) {

            if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                // Permission has not been granted and must be
                // requested.

                if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    // Provide an additional rationale to the user if
                    // the permission was not granted
                    // and the user would benefit from additional
                    // context for the use of the permission.
                }
                // Request the permission. The result will be received
                // in onRequestPermissionResult()
                requestPermissions(
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        PERMISSION_REQUEST_SD_WRITE);
            } else {
                // Permission is already available, start camera preview
                startLiveness();
            }
        } else {
            startLiveness();
        }
    }

    public void onRequestPermissionsResult(int requestCode,
                                           String[] permissions, int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CAMERA) {
            // Request for camera permission.
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission has been granted. Start camera preview Activity.
                requestWriteSdPermission();
            } else {
                // Permission request was denied.
                Toast.makeText(this, Constants.ERROR_CAMERA_REFUSE,
                        Toast.LENGTH_SHORT).show();
            }
        }
        if (requestCode == PERMISSION_REQUEST_SD_WRITE) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission has been granted. Start camera preview Activity.
                startLiveness();
            } else {
                // Permission request was denied.
                Toast.makeText(this, Constants.ERROR_SD_REFUSE,
                        Toast.LENGTH_SHORT).show();
            }
        }

        if (requestCode == PERMISSION_REQUEST_WRITE) {
            // Request for camera permission.
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission has been granted. Start camera preview Activity.
                initSDK();
            } else {
                // Permission request was denied.
                Toast.makeText(this, Constants.ERROR_STORAGE_REFUSE,
                        Toast.LENGTH_SHORT).show();
            }
        }
    }

详细可参考Demo中MainTestActivity。

如需申请其他权限,原理相同,可自行修改添加。

4.4 解决代码混淆

如果不开启代码混淆则跳过这一步; 由于代码混淆后可能会发生crash,所以建议先在混淆配置中解决代码混淆问题。

具体操作为:在proguard文件中加入下面的代码解决:

-keep class cn.linkface.** { *; }

4.5 SDK初始化及使用说明

1.初始化SDK识别模型。在开始活体检测前执行以下代码,完成LFLivenessSDK单例模式的初始化。Demo中是在MyApplication.java,代码如下:

    private static final String LF_API_ID = "#######";
    private static final String LF_API_SECRET = "#######";

    public void onCreate() {
        super.onCreate();
        //初始化活体检测 SDK
        LFLivenessSDK.getInstance(this).init(LF_API_ID, LF_API_SECRET);
    }

2.您可以设置动作序列,返回单图或多图,设置难易程度,以及是否打开语音提示。SDK会根据您的设置,来进行活体检测。

LFLivenessSample中启动活体检测配置如下:

private void startLiveness() {
        try {
            Bundle bundle = new Bundle();
            /**
             * OUTPUT_TYPE 配置, 传入的outputType类型为singleImg (单图),multiImg (多图)
             */
            bundle.putString(LivenessActivity.OUTTYPE, getOutputType());
            /**
             * EXTRA_MOTION_SEQUENCE 动作检测序列配置,支持四种检测动作, MOUTH(张嘴), NOD(点头), YAW(摇头), 各个动作以空格隔开。
             * 默认配置为"MOUTH NOD YAW"
             */
            bundle.putString(LivenessActivity.EXTRA_MOTION_SEQUENCE, getActionSequence());
            /**
             * SOUND_NOTICE 配置, 传入的soundNotice为boolean值,true为打开, false为关闭。
             */
            bundle.putBoolean(LivenessActivity.SOUND_NOTICE, true);
            /**
             * COMPLEXITY 配置, 传入的complexity类型为normal,支持四种难度,easy, normal, hard, hell.
             */
            bundle.putString(LivenessActivity.COMPLEXITY, Constants.NORMAL);

            Intent intent = new Intent();
            intent.setClass(this, LivenessActivity.class);
            intent.putExtras(bundle);
            //设置返回图片结果
            intent.putExtra(LivenessActivity.KEY_DETECT_IMAGE_RESULT, true);
            startActivityForResult(intent, KEY_TO_DETECT_REQUEST_CODE);
        } catch (Exception e) {
            e.printStackTrace();
}

我们建议将 MOUTH 放第一个检测,后面可以随意组合。

推荐的配置为:

动作序列为:"MOUTH NOD YAW",多图multiImg,难易程度为normal, 语音提示true。

代码详情参见Demo。

results matching ""

    No results matching ""