文档

Java SDK调用示例

更新时间:

本文介绍了人脸静默活体检测Java SDK调用示例。

前提条件

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

使用Maven引入SDK

如果您需要查看SDK源代码,请参见Java SDK源码

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>cloudauth20201112</artifactId>
    <version>1.0.2</version>
</dependency>

示例

  • 方式一:使用检测图片的URL示例

    import java.util.Arrays;
    import java.util.List;
    
    import com.aliyun.cloudauth20201112.Client;
    import com.aliyun.cloudauth20201112.models.LivenessDetectRequest;
    import com.aliyun.cloudauth20201112.models.LivenessDetectResponse;
    import com.aliyun.tea.TeaException;
    import com.aliyun.tea.TeaUnretryableException;
    import com.aliyun.tearpc.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class LivenessDetect {
    
        public static void main(String[] args) throws Exception {
    
            // 通过以下代码创建API请求并设置参数。
            LivenessDetectRequest request = new LivenessDetectRequest();
            request.setBizId("认证ID");// 由接入方指定, 发起不同的认证任务需要更换不同的认证ID。
            request.setBizType("业务场景标识");// 在实人认证控制台上创建的业务场景对应的场景标识。
            // 认证的资源类型。
            request.setMediaCategory("IMAGE");
            request.setMediaUrl("<https://xxx.jpeg>");
    
            // 推荐,支持服务路由。
            LivenessDetectResponse response = livenessDetectRequestAutoRoute(request);
    
            // 不支持服务自动路由。
            //LivenessDetectResponse response = livenessDetect("cloudauth.cn-shanghai.aliyuncs.com", request);
    
            System.out.println(response.getRequestId());
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getPassed());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getScore());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getFrameUrl());
        }
    
        private static LivenessDetectResponse livenessDetectRequestAutoRoute(LivenessDetectRequest request) {
            // 第一个为主区域Endpoint,第二个为备区域Endpoint。
            List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com");
            LivenessDetectResponse lastResponse = null;
            for (String endpoint : endpoints) {
                try {
                    LivenessDetectResponse response = livenessDetect(endpoint, request);
                    lastResponse = response;
    
                    // 服务端错误,切换到下个区域调用。
                    if ("500".equals(response.getCode())) {
                        continue;
                    }
    
                    return response;
                } catch (Exception e) {
                    // 网络异常,切换到下个区域调用。
                    if (e.getCause() instanceof TeaException) {
                        TeaException teaException = ((TeaException)e.getCause());
                        if (teaException.getData() != null && "ServiceUnavailable".equals(
                            teaException.getData().get("Code"))) {
                            continue;
                        }
                    }
    
                    if (e.getCause() instanceof TeaUnretryableException) {
                        continue;
                    }
                }
            }
    
            return lastResponse;
        }
    
        private static LivenessDetectResponse livenessDetect(String endpoint, LivenessDetectRequest request)
            throws Exception {
            // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/378657.html。
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            Config config = new Config();
            config.setCredential(credentialClient);
            config.setEndpoint(endpoint);
            // 设置HTTP代理。
            //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
            // 设置HTTPS代理。
            //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
            Client client = new Client(config);
    
            // 创建RuntimeObject实例并设置运行参数。
            RuntimeOptions runtime = new RuntimeOptions();
            runtime.readTimeout = 10000;
            runtime.connectTimeout = 10000;
    
            return client.livenessDetect(request, runtime);
        }
    }
  • 方式二:使用检测视频的URL示例

    import java.util.Arrays;
    import java.util.List;
    
    import com.aliyun.cloudauth20201112.Client;
    import com.aliyun.cloudauth20201112.models.LivenessDetectRequest;
    import com.aliyun.cloudauth20201112.models.LivenessDetectResponse;
    import com.aliyun.tea.TeaException;
    import com.aliyun.tea.TeaUnretryableException;
    import com.aliyun.tearpc.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class LivenessDetect {
    
        public static void main(String[] args) throws Exception {
    
            // 通过以下代码创建API请求并设置参数。
            LivenessDetectRequest request = new LivenessDetectRequest();
            request.setBizId("认证ID");// 由接入方指定, 发起不同的认证任务需要更换不同的认证ID。
            request.setBizType("业务场景标识");// 在实人认证控制台上创建的业务场景对应的场景标识。
            // 认证的资源类型。
            request.setMediaCategory("VIDEO");
            request.setMediaFile("<https://xxx.mp4>");
    
            // 推荐,支持服务路由。
            LivenessDetectResponse response = livenessDetectRequestAutoRoute(request);
    
            // 不支持服务自动路由。
            //LivenessDetectResponse response = livenessDetect("cloudauth.cn-shanghai.aliyuncs.com", request);
    
            System.out.println(response.getRequestId());
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getPassed());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getScore());
            System.out.println(response.getResultObject() == null ? null
                : response.getResultObject().getFrameUrl());
        }
    
        private static LivenessDetectResponse livenessDetectRequestAutoRoute(LivenessDetectRequest request) {
            // 第一个为主区域Endpoint,第二个为备区域Endpoint。
            List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com");
            LivenessDetectResponse lastResponse = null;
            for (String endpoint : endpoints) {
                try {
                    LivenessDetectResponse response = livenessDetect(endpoint, request);
                    lastResponse = response;
    
                    // 服务端错误,切换到下个区域调用。
                    if ("500".equals(response.getCode())) {
                        continue;
                    }
    
                    return response;
                } catch (Exception e) {
                    // 网络异常,切换到下个区域调用。
                    if (e.getCause() instanceof TeaException) {
                        TeaException teaException = ((TeaException)e.getCause());
                        if (teaException.getData() != null && "ServiceUnavailable".equals(
                            teaException.getData().get("Code"))) {
                            continue;
                        }
                    }
    
                    if (e.getCause() instanceof TeaUnretryableException) {
                        continue;
                    }
                }
            }
    
            return lastResponse;
        }
    
        private static LivenessDetectResponse livenessDetect(String endpoint, LivenessDetectRequest request)
            throws Exception {
            // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/378657.html。
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            Config config = new Config();
            config.setCredential(credentialClient);
            config.setEndpoint(endpoint);
            // 设置HTTP代理。
            //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
            // 设置HTTPS代理。
            //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
            Client client = new Client(config);
    
            // 创建RuntimeObject实例并设置运行参数。
            RuntimeOptions runtime = new RuntimeOptions();
            runtime.readTimeout = 10000;
            runtime.connectTimeout = 10000;
    
            return client.livenessDetect(request, runtime);
        }
    }
  • 方式三:使用本地文件示例

    import java.io.FileInputStream;
    import java.util.Arrays;
    import java.util.List;
    
    import com.aliyun.cloudauth20201112.Client;
    import com.aliyun.cloudauth20201112.models.LivenessDetectAdvanceRequest;
    import com.aliyun.cloudauth20201112.models.LivenessDetectResponse;
    import com.aliyun.tea.TeaException;
    import com.aliyun.tea.TeaUnretryableException;
    import com.aliyun.tearpc.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class LivenessDetect {
    
        public static void main(String[] args) throws Exception {
    
            // 通过以下代码创建API请求并设置参数。
            LivenessDetectAdvanceRequest request = new LivenessDetectAdvanceRequest();
            request.setBizId("认证ID");// 由接入方指定, 发起不同的认证任务需要更换不同的认证ID。
            request.setBizType("业务场景标识");// 在实人认证控制台上创建的业务场景对应的场景标识。
            // 认证的资源类型。取值:IMAGE:图片。VIDEO:视频。
            request.setMediaCategory("IMAGE");
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream("<xxx.JPG>");
                request.setMediaFileObject(fileInputStream);
    
                // 推荐,支持服务路由。
                LivenessDetectResponse response = livenessDetectRequestAutoRoute(request);
    
                // 不支持服务自动路由。
                //LivenessDetectResponse response = livenessDetect("cloudauth.cn-shanghai.aliyuncs.com", request);
    
                System.out.println(response.getRequestId());
                System.out.println(response.getCode());
                System.out.println(response.getMessage());
                System.out.println(response.getResultObject() == null ? null
                    : response.getResultObject().getPassed());
                System.out.println(response.getResultObject() == null ? null
                    : response.getResultObject().getScore());
                System.out.println(response.getResultObject() == null ? null
                    : response.getResultObject().getFrameUrl());
            } finally {
                if(fileInputStream != null){
                    fileInputStream.close();
                }
            }
        }
    
        private static LivenessDetectResponse livenessDetectRequestAutoRoute(LivenessDetectAdvanceRequest request) {
            // 第一个为主区域Endpoint,第二个为备区域Endpoint。
            List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com");
            LivenessDetectResponse lastResponse = null;
            for (String endpoint : endpoints) {
                try {
                    LivenessDetectResponse response = livenessDetect(endpoint, request);
                    lastResponse = response;
    
                    // 服务端错误,切换到下个区域调用。
                    if ("500".equals(response.getCode())) {
                        continue;
                    }
    
                    return response;
                } catch (Exception e) {
                    // 网络异常,切换到下个区域调用。
                    if (e.getCause() instanceof TeaException) {
                        TeaException teaException = ((TeaException)e.getCause());
                        if (teaException.getData() != null && "ServiceUnavailable".equals(
                            teaException.getData().get("Code"))) {
                            continue;
                        }
                    }
    
                    if (e.getCause() instanceof TeaUnretryableException) {
                        continue;
                    }
                }
            }
    
            return lastResponse;
        }
    
        private static LivenessDetectResponse livenessDetect(String endpoint, LivenessDetectAdvanceRequest request)
            throws Exception {
            // 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
            // 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
            //本示例通过阿里云Credentials工具从环境变量中读取AccessKey,来实现API访问的身份验证。如何配置环境变量,请参见https://help.aliyun.com/document_detail/378657.html。
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            Config config = new Config();
            config.setCredential(credentialClient);
            config.setEndpoint(endpoint);
            // 设置HTTP代理。
            //config.setHttpProxy("http://xx.xx.xx.xx:xxxx");
            // 设置HTTPS代理。
            //config.setHttpsProxy("https://xx.xx.xx.xx:xxxx");
            Client client = new Client(config);
    
            // 创建RuntimeObject实例并设置运行参数。
            RuntimeOptions runtime = new RuntimeOptions();
            runtime.readTimeout = 10000;
            runtime.connectTimeout = 10000;
    
            return client.livenessDetectAdvance(request, runtime);
        }
    }
  • 本页导读
文档反馈