腾讯云人脸核身Android 端接入(二)

紧跟上一篇服务端的实现,以下实现Android 端接入

如需查看服务端配置请查看 腾讯云人脸核身服务端实现(一)

  1. 引入相关依赖
  1. build.gradle 配置
less 复制代码
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
  1. 混淆规则
arduino 复制代码
#不混淆内部类
-keepattributes InnerClasses

#不混淆jni调用类
-keepclasseswithmembers class *{
    native <methods>;
}

###################### faceverify-BEGIN ###########################
-ignorewarnings
-keep public class com.tencent.ytcommon.**{*;}
-keep class com.tencent.turingface.sdk.*.TNative$aa { public *; }
-keep class com.tencent.turingface.sdk.*.TNative$aa$bb { public *; }
-keep class com.tencent.turingcam.** {*;}

-keep public class com.tencent.youtu.ytagreflectlivecheck.jni.**{*;}
-keep public class com.tencent.youtu.ytagreflectlivecheck.YTAGReflectLiveCheckInterface{
    public <methods>;
}
-keep public class com.tencent.youtu.ytposedetect.jni.**{*;}
-keep public class com.tencent.youtu.ytposedetect.data.**{*;}
-keep public class com.tencent.youtu.liveness.YTDeviceInfo{*;}
-keep public class com.tencent.youtu.liveness.YTFaceTracker{*;}
-keep public class com.tencent.youtu.liveness.YTFaceTracker$*{*;}
-keep public class com.tencent.youtu.sdkkitframework.liveness.framework.YtSDKKitFrameworkTool{
   public *;
}
-keep public class com.tencent.youtu.sdkkitframework.liveness.common.YTImageData{
   *;
}
-keep public class com.tencent.cloud.huiyansdkface.facelight.net.*$*{
    *;
}
-keep public class com.tencent.cloud.huiyansdkface.facelight.net.**{
    *;
}
-keep public class com.tencent.cloud.huiyansdkface.facelight.provider.WbDeviceRiskInfo{
    public <fields>;
}
-keep public class com.tencent.cloud.huiyansdkface.facelight.provider.WbUiTips{
    *;
}

#================数据上报混淆规则 start===========================
#实体类
-keep class com.tencent.cloud.huiyansdkface.analytics.EventSender{
    *;
}
-keep class com.tencent.cloud.huiyansdkface.analytics.EventSender$*{
    *;
}
-keep class com.tencent.cloud.huiyansdkface.analytics.WBSAEvent{
     *;
}
-keep class com.tencent.cloud.huiyansdkface.analytics.WBSAParam{
     *;
}
#================数据上报混淆规则 end===========================

#######################faceverify-END#############################

####################### normal混淆规则-BEGIN#############################
#不混淆内部类
-keepattributes InnerClasses
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exceptions

-keep public class com.tencent.cloud.huiyansdkface.normal.net.*$*{
    *;
}
-keep public class com.tencent.cloud.huiyansdkface.normal.net.*{
    *;
}
#bugly
-keep class com.tencent.bugly.idasc.**{
    *;
}
#wehttp混淆规则
-dontwarn com.tencent.cloud.huiyansdkface.okio.**
-keep class com.tencent.cloud.huiyansdkface.okio.**{
    *;
}
-dontwarn com.tencent.cloud.huiyansdkface.okhttp3.OkHttpClient$Builder
#for R8
-keep,allowobfuscation,allowshrinking class * implements com.tencent.cloud.huiyansdkface.wehttp2.WeReq$Callback
-keep,allowobfuscation,allowshrinking interface com.tencent.cloud.huiyansdkface.wehttp2.WeReq$Callback

####################### normal混淆规则-END#############################
  1. 定义页面
kotlin 复制代码
package com.alan.music.modules.verify

import android.Manifest
import android.content.pm.PackageManager
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import com.extensions.clickableWithoutIndication
import com.utils.toast
import com.widgets.BasicButton
import com.widgets.BasicScaffold
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.rememberPermissionState
import kotlinx.coroutines.launch
import retrofit2.Response

@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun VerifyPage(vm: VerifyViewModel= hiltViewModel<VerifyViewModel>()) {
    BasicScaffold {
        val context = LocalContext.current
        val scope = rememberCoroutineScope()
        val permissionState= rememberPermissionState(Manifest.permission.CAMERA, onPermissionResult = {

            if (it) {
                toast("授权相机权限 $it,请重新验证")
            }
        })
        Column(modifier = Modifier.fillMaxSize()) {
            BasicButton(text = "验证", modifier = Modifier.clickableWithoutIndication {
                if (context.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                    scope.launch {
                        val response:Response<NeedParams> =  vm.getNeedParams();
                        response.body()?.apply {

                            VerifyUtil.openCloudFaceService(context,this)
                        }


                    }

                }else{
                    permissionState.launchPermissionRequest()
                }




            })

        }
    }


}
  1. 定义ApiService,ViewModel,响应体请求上一节中定义的服务端接口
kotlin 复制代码
package com.modules.verify

 data class NeedParams(
    val appId: String,
    val faceId: String,
    val nonce: String,
    val orderNo: String,
    val sign: String,
    val userId: String,
    val version: String
)
kotlin 复制代码
@GET("http://192.168.0.115:10000/tencent-cloud/get-sign")
suspend fun getSign(): Response<NeedParams>
kotlin 复制代码
package com.modules.verify

import androidx.lifecycle.ViewModel
import com.alan.music.net.MusicApiService
import com.alan.music.net.executeHttp
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import retrofit2.Response
import javax.inject.Inject


@HiltViewModel
class VerifyViewModel @Inject constructor() : ViewModel() {
    @Inject
    lateinit var apiService: MusicApiService
    val isLoading = MutableStateFlow(false)

    /**
     * 注册获取短信验证码
     */
    suspend fun getNeedParams(): Response<NeedParams> {
        return executeHttp {
            apiService.getSign()
        }


    }
}
  1. 定义认证util
typescript 复制代码
package com.modules.verify;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import com.tencent.cloud.huiyansdkface.facelight.api.WbCloudFaceContant;
import com.tencent.cloud.huiyansdkface.facelight.api.WbCloudFaceVerifySdk;
import com.tencent.cloud.huiyansdkface.facelight.api.listeners.WbCloudFaceVerifyLoginListener;
import com.tencent.cloud.huiyansdkface.facelight.api.listeners.WbCloudFaceVerifyResultListener;
import com.tencent.cloud.huiyansdkface.facelight.api.result.WbFaceError;
import com.tencent.cloud.huiyansdkface.facelight.api.result.WbFaceVerifyResult;
import com.tencent.cloud.huiyansdkface.facelight.process.FaceVerifyStatus;

import static com.alan.music.utils.ToastUtilKt.toast;

public class VerifyUtil {

    //拉起刷脸sdk
    public static void openCloudFaceService(Context context, NeedParams needParams) {
        Log.d("测试", "openCloudFaceService");
        Bundle data = new Bundle();
        WbCloudFaceVerifySdk.InputData inputData = new WbCloudFaceVerifySdk.InputData(
               needParams.getFaceId(),
                needParams.getOrderNo(),
                needParams.getAppId(),
                "1.0.0",
                needParams.getNonce(),
                needParams.getUserId(),
                needParams.getSign(),
                FaceVerifyStatus.Mode.GRADE,
                "Q5sBOcmVygfQJjdMcItosSpbz9ivBiY4RiMPpoogd9xpOjUQk9uqkxE/w9h8yTPR8pklsNZjOXT3ikedc9UEDNHKmP2qFVc8oFQotqOu5fCTC3iFTodmJ5TQ6Mh6hjqgD+rIXBQcztoQb8EpEQc0msh9oCpFVsMp1EcU+j4OcJwR0yFkqvU+aLnXQ7mhhWX2779bTejo59+V2SeM6xrE27gdrC7J4BCgu21vCIsiX/4VmMSJp0QkQzPDHK6EeMaLsZ2Hjn5u6r05YoTR/ROh8ie5E0B0vv3Qk9Sc0J8vgnkzdEW+HUtfR/1+twbHUcwWtoPZ+cM/h5ht3xC8bD4d5Q=="
        );
        data.putSerializable(WbCloudFaceContant.INPUT_DATA, inputData);
        //【特别注意】请使用activity context拉起sdk
        //【特别注意】请在主线程拉起sdk!
        WbCloudFaceVerifySdk.getInstance().initSdk(context, data, new WbCloudFaceVerifyLoginListener() {
            @Override
            public void onLoginSuccess() {
                //登录sdk成功
                toast("登录成功");
                //拉起刷脸页面
                //【特别注意】请使用activity context拉起sdk
                //【特别注意】请在主线程拉起sdk!
                WbCloudFaceVerifySdk.getInstance().startWbFaceVerifySdk(context, new WbCloudFaceVerifyResultListener() {
                    @Override
                    public void onFinish(WbFaceVerifyResult result) {
                        //刷脸结束后,释放资源
                        WbCloudFaceVerifySdk.getInstance().release();
                    }
                });
            }

            @Override
            public void onLoginFailed(WbFaceError error) {
                toast("登录失败");

                //刷脸结束后,释放资源
                WbCloudFaceVerifySdk.getInstance().release();
            }
        });
    }
}
  1. 拉起效果
  1. 特别注意

由于sdk内部使用的是Android support 依赖

如果你的项目使用的是support依赖,需要在build.gradle 中添加以下依赖

arduino 复制代码
     //0. appcompat-v7
 compile 'com.android.support:appcompat-v7:23.0.1'

如果已经完全切换到了Androidx,需要在gradle.properties中添加以下配置

ini 复制代码
# 将三方依赖中的support 支持自动调整为AndroidX
android.enableJetifier=true
相关推荐
砖厂小工4 小时前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
张拭心4 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能
张拭心4 小时前
Android 17 来了!新特性介绍与适配建议
android·前端
Kapaseker7 小时前
Compose 进阶—巧用 GraphicsLayer
android·kotlin
黄林晴7 小时前
Android17 为什么重写 MessageQueue
android
阿巴斯甜1 天前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker1 天前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq95271 天前
Andorid Google 登录接入文档
android
黄林晴1 天前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab2 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读