文档

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.DetectFaceAttributesRequest;
    import com.aliyun.cloudauth20201112.models.DetectFaceAttributesResponse;
    import com.aliyun.tea.TeaException;
    import com.aliyun.tea.TeaUnretryableException;
    import com.aliyun.tearpc.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class DetectFaceAttributes {
    
        public static void main(String[] args) throws Exception {
    
            // 通过以下代码创建API请求并设置参数。
            DetectFaceAttributesRequest request = new DetectFaceAttributesRequest();
            request.setBizId("认证ID");// 由接入方指定, 发起不同的认证任务需要更换不同的认证ID。
            request.setBizType("业务场景标识");// 在实人认证控制台上创建的业务场景对应的场景标识。
            // 认证的资源类型。
            request.setImageUrl("https://xxx.jpeg");
    
            // 推荐,支持服务路由。
            DetectFaceAttributesResponse response = detectFaceAttributesRequestAutoRoute(request);
    
            // 不支持服务自动路由。
            //DetectFaceAttributesResponse response = detectFaceAttributes("cloudauth.cn-shanghai.aliyuncs.com", request);
    
            System.out.println(response.getRequestId());
            System.out.println(response.getCode());
            System.out.println(response.getMessage());
            if(response.getResultObject() != null){
                System.out.println(response.getResultObject().getImgHeight());
                System.out.println(response.getResultObject().getImgWidth());
            }
    
        }
    
        private static DetectFaceAttributesResponse detectFaceAttributesRequestAutoRoute(DetectFaceAttributesRequest request) {
            // 第一个为主区域Endpoint,第二个为备区域Endpoint。
            List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com");
            DetectFaceAttributesResponse lastResponse = null;
            for (String endpoint : endpoints) {
                try {
                    DetectFaceAttributesResponse response = detectFaceAttributes(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 DetectFaceAttributesResponse detectFaceAttributes(String endpoint, DetectFaceAttributesRequest 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.detectFaceAttributes(request, runtime);
        }
    }
  • 方式二:使用本地文件示例

    import java.io.FileInputStream;
    import java.util.Arrays;
    import java.util.List;
    
    import com.aliyun.cloudauth20201112.Client;
    import com.aliyun.cloudauth20201112.models.DetectFaceAttributesAdvanceRequest;
    import com.aliyun.cloudauth20201112.models.DetectFaceAttributesResponse;
    import com.aliyun.tea.TeaException;
    import com.aliyun.tea.TeaUnretryableException;
    import com.aliyun.tearpc.models.Config;
    import com.aliyun.teautil.models.RuntimeOptions;
    
    public class DetectFaceAttributes {
    
        public static void main(String[] args) throws Exception {
    
            // 通过以下代码创建API请求并设置参数。
            DetectFaceAttributesAdvanceRequest request = new DetectFaceAttributesAdvanceRequest();
            request.setBizId("认证ID");// 由接入方指定, 发起不同的认证任务需要更换不同的认证ID。
            request.setBizType("业务场景标识");// 在实人认证控制台上创建的业务场景对应的场景标识。
            // 认证的资源类型。
            FileInputStream inputstream = null;
            try {
                inputstream = new FileInputStream("<xxx.JPG>");
                request.setImageFileObject(inputstream);
    
                // 推荐,支持服务路由。
                DetectFaceAttributesResponse response = detectFaceAttributesRequestAutoRoute(request);
    
                // 不支持服务自动路由。
                //DetectFaceAttributesResponse response = detectFaceAttributes("cloudauth.cn-shanghai.aliyuncs.com",
                // request);
    
                System.out.println(response.getRequestId());
                System.out.println(response.getCode());
                System.out.println(response.getMessage());
                if (response.getResultObject() != null) {
                    System.out.println(response.getResultObject().getImgHeight());
                    System.out.println(response.getResultObject().getImgWidth());
                }
            } finally {
                if (inputstream != null) {
                    inputstream.close();
                }
            }
        }
    
        private static DetectFaceAttributesResponse detectFaceAttributesRequestAutoRoute(DetectFaceAttributesAdvanceRequest request) {
            // 第一个为主区域Endpoint,第二个为备区域Endpoint。
            List<String> endpoints = Arrays.asList("cloudauth.cn-shanghai.aliyuncs.com",
                "cloudauth.cn-beijing.aliyuncs.com");
            DetectFaceAttributesResponse lastResponse = null;
            for (String endpoint : endpoints) {
                try {
                    DetectFaceAttributesResponse response = detectFaceAttributes(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 DetectFaceAttributesResponse detectFaceAttributes(String endpoint, DetectFaceAttributesAdvanceRequest 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.detectFaceAttributesAdvance(request, runtime);
        }
    }
  • 本页导读
文档反馈