本文介绍了活体人脸验证方案Java SDK调用示例。

SDK使用说明

如果需要其他语言的SDK示例代码,您可以通过OpenAPI 开发者门户在线调试工具调试API接口。该工具会自动生成相应API的SDK调用示例代码,详细信息,请参见:

前提条件

需要使用的Java版本在1.8版本及以上。

使用Maven引入SDK

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.3</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-cloudauth</artifactId>
    <version>2.0.20</version>
</dependency>

调用示例

InitFaceVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.HttpClientType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.regions.ProductDomain;
import com.google.gson.Gson;

public class InitFaceVerify {

    public static void main(String[] args) throws ClientException {

        InitFaceVerifyRequest request = new InitFaceVerifyRequest();
        // 请输入场景ID+L。
        request.setSceneId(xxxL);
        // 设置商户请求的唯一标识。
        request.setOuterOrderNo("xxxx");
        // 认证方案。
        request.setProductCode("PV_FV");
        request.setUserId("12345xxxx");
        // 模式。
        request.setModel("LIVENESS");
        request.setCertType("IDENTITY_CARD");
        //request.setCertName("张三");
        //request.setCertNo("xxxx");
        // MetaInfo环境参数。
        request.setMetaInfo("{\"zimVer\":\"3.0.0\",\"appVersion\": \"1\",\"bioMetaInfo\": \"4.1.0:11501568,0\","
            + "\"appName\": \"com.aliyun.antcloudauth\",\"deviceType\": \"ios\",\"osVersion\": \"iOS 10.3.2\","
            + "\"apdidToken\": \"\",\"deviceModel\": \"iPhone9,1\"}");
        //request.setMobile("130xxxxxxxx");
        //request.setIp("114.xxx.xxx.xxx");

        // 请在以下四种方式中选择其一。
        // 1.照片Base64编码。
        // 2.照片OSS bucket名和文件名。
        // 3.照片OSS的URL地址。
        // 4.已经实人认证通过的CertifyId。

        // 方式一:照片Base64。
        request.setFaceContrastPicture("人脸照片Base64编码");
        // 方式二:照片OSS bucket名和文件名。
        //request.setOssBucketName("cn-shanghai-aliyun-cloudauth-xxxxx");
        //request.setOssObjectName("verify/xxxxx/xxxxxx.jpeg");
        // 方式三:照片OSS的URL地址,公网可访问。
        //request.setFaceContrastPictureUrl("https://cn-shanghai-aliyun-cloudauth-xxxxxx.oss-cn-shanghai.aliyuncs.com/verify/xxxxx/xxxxx.jpeg")
        // 方式四:已经实人认证通过的CertifyId。
        //request.setCertifyId("0bfa7c493f850e5178b9f8613634c9xx");

        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");

        // 推荐,支持服务路由。
        //InitFaceVerifyResponse response = initFaceVerifyAutoRoute(endpoints, request);

        // 不支持服务自动路由。
        InitFaceVerifyResponse response = initFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);

        System.out.println(new Gson().toJson(response));
    }

    private static InitFaceVerifyResponse initFaceVerifyAutoRoute(List<String> endpoints,
        InitFaceVerifyRequest request) {

        InitFaceVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                InitFaceVerifyResponse response = initFaceVerify(endpoint, request);
                lastResponse = response;
                if ("500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (ClientException e) {
                if ("ServiceUnavailable".equals(e.getErrCode()) || "SDK.ServerUnreachable".equals(e.getErrCode())
                    || "SDK.ReadTimeout".equals(e.getErrCode())) {
                    continue;
                }

                System.out.println(e.getErrCode());
                System.out.println(e.getErrMsg());
                System.out.println(e.getRequestId());
            }
        }

        return lastResponse;
    }

    private static InitFaceVerifyResponse initFaceVerify(String endpoint, InitFaceVerifyRequest request)
        throws ClientException {
        // Client代理配置。
        HttpClientConfig config = new HttpClientConfig();
        config.setClientType(HttpClientType.ApacheHttpClient);
        //config.setProtocolType(ProtocolType.HTTPS);
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        //config.setNoProxy("xx.xx.xx.xx,localhost");
        config.setConnectionTimeoutMillis(10000);
        config.setReadTimeoutMillis(10000);
        DefaultProfile profile = DefaultProfile.getProfile("", "您的AccessKey ID",
            "您的AccessKey Secret");
        profile.setHttpClientConfig(config);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        request.setSysProductDomain(new ProductDomain("cloudauth", endpoint));

        return client.getAcsResponse(request);
    }
}

DescribeFaceVerify接口调用示例

import java.util.Arrays;
import java.util.List;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.HttpClientConfig;
import com.aliyuncs.http.HttpClientType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.regions.ProductDomain;
import com.google.gson.Gson;

public class DescribeFaceVerify {

    public static void main(String[] args) throws ClientException {

        DescribeFaceVerifyRequest request = new DescribeFaceVerifyRequest();
        // 请输入SceneId+L。
        request.setSceneId(xxxxL);

        request.setCertifyId("xxxxx");

        List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
            "cloudauth.cn-beijing.aliyuncs.com");

        // 推荐,支持服务路由。
        //DescribeFaceVerifyResponse response = describeFaceVerifyAutoRoute(endpoints, request);

        // 不支持服务自动路由。
        DescribeFaceVerifyResponse response = describeFaceVerify("cloudauth.cn-shanghai.aliyuncs.com", request);
        System.out.println(new Gson().toJson(response));
    }

    private static DescribeFaceVerifyResponse describeFaceVerifyAutoRoute(List<String> endpoints,
        DescribeFaceVerifyRequest request) {

        DescribeFaceVerifyResponse lastResponse = null;
        for (String endpoint : endpoints) {
            try {
                DescribeFaceVerifyResponse response = describeFaceVerify(endpoint, request);
                lastResponse = response;
                if ("500".equals(response.getCode())) {
                    continue;
                }

                return response;
            } catch (ClientException e) {
                if ("ServiceUnavailable".equals(e.getErrCode()) || "SDK.ServerUnreachable".equals(e.getErrCode())
                    || "SDK.ReadTimeout".equals(e.getErrCode())) {
                    continue;
                }

                System.out.println(e.getErrCode());
                System.out.println(e.getErrMsg());
                System.out.println(e.getRequestId());
            }
        }

        return lastResponse;
    }

    private static DescribeFaceVerifyResponse describeFaceVerify(String endpoint, DescribeFaceVerifyRequest request)
        throws ClientException {
        // Client代理配置。
        HttpClientConfig config = new HttpClientConfig();
        config.setClientType(HttpClientType.ApacheHttpClient);
        //config.setProtocolType(ProtocolType.HTTPS);
        //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
        //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
        //config.setNoProxy("xx.xx.xx.xx,localhost");
        config.setConnectionTimeoutMillis(10000);
        config.setReadTimeoutMillis(10000);
        DefaultProfile profile = DefaultProfile.getProfile("", "您的AccessKey ID",
            "您的AccessKey Secret");
        profile.setHttpClientConfig(config);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        request.setSysProductDomain(new ProductDomain("cloudauth", endpoint));

        return client.getAcsResponse(request);
    }
}