文档

设置个体

更新时间:

本文介绍了如何使用Java SDK设置个体信息。

功能描述

设置个体信息用于给个体添加备注信息,属于可选步骤。如果您设置了个体信息,则在自定义检索的返回结果中会包含该信息。关于参数的详细说明,请参见设置个体API文档

您需要使用内容安全的API接入地址,调用本SDK接口。关于API接入地址的信息,请参见接入地址(Endpoint)

前提条件

  • 安装Java依赖。关于安装Java依赖的具体操作,请参见安装Java依赖

    说明

    请一定按照安装Java依赖页面中的版本安装,否则会导致调用失败。

  • 如果使用本地文件或者二进制文件检测,请下载并在项目工程中引入Extension.Uploader工具类

设置个体代码示例

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.SetPersonRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 设置个体信息。
 * @author 
 * @date 2018/06/25
 */
public class FaceSetPersonRequestSample {

    public static void main(String[] args) throws Exception {
        /**
         * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。 
         * 常见获取环境变量方式:
         * 方式一:
         *     获取RAM用户AccessKey ID:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         * 方式二:
         *     获取RAM用户AccessKey ID:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
         *     获取RAM用户AccessKey Secret:System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
         */
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-shanghai",
                "建议从环境变量中获取RAM用户AccessKey ID",
                "建议从环境变量中获取RAM用户AccessKey Secret");
        DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
        // 注意:此处实例化的client尽可能重复使用,提升检测性能。避免重复建立连接。
        IAcsClient client = new DefaultAcsClient(profile);

        SetPersonRequest setPersonRequest = new SetPersonRequest();
        setPersonRequest.setAcceptFormat(FormatType.JSON); // 指定API返回格式。
        setPersonRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法。
        setPersonRequest.setEncoding("utf-8");

        JSONObject data = new JSONObject();
        /**
         * personId: 用户自定义个体ID,必填。
         * name: 用户名称,非必填。
         * note: 备注信息,非必填。
         */
        data.put("personId", "personId_test_3");
        data.put("name", "测试");
        data.put("note", "备注信息");


        setPersonRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间。
         */
        setPersonRequest.setConnectTimeout(3000);
        setPersonRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(setPersonRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONObject resultObject = scrResponse.getJSONObject("data");
                    if (200 == resultObject.getInteger("code")) {
                        System.out.println(resultObject.getString("personId"));
                    } else {
                        System.out.println("task process fail:" + resultObject.getInteger("code"));
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 本页导读 (0)
文档反馈