API 模式

概述

ZOLOZ SaaS API 独立于语言,因此您可以将 API 与任何语言集成。

在使用 API 模式接入 ZOLOZ 之前,您需要充分了解 ZOLOZ 网关协议,以便正确创建请求并处理响应;如果您的项目是用 Java 编写的,您可以利用现有的 API SDK 来接入 API。在文中,我们以 Java API SDK 为例来演示 API 接入。

引入 API SDK

在项目的 POM 文件中添加以下依赖项,将库引入到项目中:

copy
<dependency>
    <groupId>com.zoloz.api.sdk</groupId>
    <artifactId>zoloz-api-sdk</artifactId>
    <version>0.1.0</version>
</dependency>

API 说明

实例化和配置 API SDK 客户端

copy
// initialize OpenApiClient
String clientId = "<Client ID>";
String zolozPublicKey = "<ZOLOZ's public key content encoded in base64>";
String merchantPrivateKey = "<The merchant's private key content encoded in base64>";

OpenApiClient client = new OpenApiClient();  // construct with signature and encryption by default
client.setHostUrl("https://sg-production-api.zoloz.com");
client.setClientId(clientId);
client.setMerchantPrivateKey(merchantPrivateKey);
client.setOpenApiPublicKey(zolozPublicKey);
//client.setSigned(false);     // signature (of response) validation can be turned off
//client.setEncrypted(false);  // encryption can be turned off

FaceCompare

copy
// create api client
FaceCompareAPI faceCompareApi = new FaceCompareAPI(client);

// prepare api request
String face1ImgPath = "<file path of 1st face image>";
String face2ImgPath = "<file path of 2nd face image>";

FaceCompareRequest request = new FaceCompareRequest();
request.setBizId("biz-id-12345");  // for tracing purpose, it is recommended to use a global unique id
request.getFace1().setContent(getBase64ImageContent(face1ImgPath));
request.getFace2().setContent(getBase64ImageContent(face2ImgPath));

// call api
FaceCompareResponse response = faceCompareApi.compare(request);

if ("S".equals(response.getResult().getResultStatus())) {
    System.out.println(String.format(
            "Two faces are from %s, the similarity score is %2f",
            response.getSamePerson() ? "same person" : "different persons",
            response.getScore()
    ));
}
else {
    System.out.println(String.format(
            "[Error] %s: %s",
            response.getResult().getResultCode(),
            response.getResult().getResultMessage()
    ));
}

IdRecognize

copy
// create api client
DocRecognitionAPI docRecognitionAPI = new DocRecognitionAPI(client);

// prepare api request
String imagePath = cmd.getOptionValue("f");

DocRecognitionRequest request=new DocRecognitionRequest();
request.setBizId("biz-id-12345");  // for tracing purpose, it is recommended to use a global unique id
request.setDocType("00000001003");
request.setFrontPageImage(getBase64ImageContent(imagePath));

// call api
DocRecognitionResponse response = docRecognitionAPI.recognition(request);
if ("S".equals(response.getResult().getResultStatus())) {
    if ("Y".equals(response.getRecognitionResult())) {
        System.out.println("ID detected.\n");

        if (response.getSpoofResult() != null && !response.getSpoofResult().isEmpty()) {
            System.out.println("Spoofing Detection:");
            response.getOcrResult().forEach((key, value) -> {
                System.out.println(String.format(" -%s: %s", key, value));
            });

        }

        System.out.println("OCR Result:");
        response.getOcrResult().forEach((key, value) -> {
            System.out.println(String.format(" -%s: %s", key, value));
        });
        System.out.println(String.format(
                "[Error] %s: %s",
                response.getResult().getResultCode(),
                response.getResult().getResultMessage()
        ));
    }
    else {
        System.out.println(String.format(
                "Cannot recognize image: %s",
                response.getRecognitionErrorCode()
        ));
    }
}
else {
    System.out.println(String.format(
            "[Error] %s: %s",
            response.getResult().getResultCode(),
            response.getResult().getResultMessage()
    ));
}

代码示例

Github 存储库 中检查开源示例:

参考

对应的API规范: