• Console

›OCR

FaceID service introduction

  • FaceID
  • FaceID-Pro

Identity Verification Basic

  • App-GetBizToken
  • App-Verify
  • App-Authentication
  • H5-Raw-UploadVideo
  • H5-Raw-Verify

Identity Verification Premium

  • APP_Pro-Access-Guide
  • APP_Pro-Authentication
  • APP_Pro-Encrption-Guide
  • APP_Pro-GetBiztoken
  • APP_Pro-Liveness
  • APP_Pro-Verify
  • H5-Lite_Pro-Access-Guide
  • H5-Lite_Pro-GetToken
  • H5-Lite_Pro-Redirect-To-Liveness-URL
  • H5-Lite_Pro-Callbacks
  • H5-Lite_Pro-GetResult
  • H5-Lite_Pro-UsinginAnApp
  • H5-Lite_Pro-UI-Customization
  • H5-Lite_Pro-RTC-Compatibility

OCR

  • Indonesian_ID_Card
Indonesian_ID_Card

Description

Detect and recogize Indonisian ID Cards

Invoke URL

https://api-idn.megvii.com/faceid/v3/indonesia_ocr

WARNING:Please use HTTPS protocol for connections in production environments. HTTP protocol has safety risk due to lack of encryption; therefore, using HTTP may not guarantee service reliability.

Invoke Method

POST  WARNING:please use "form-data" format to request.

API description

This API generates an encrypted signature "sign" using the API key and secret, along with a photo of an ID card, to obtain the recognition result of the ID card. This API does not check image quality. Please ensure the quality of the image provided. Poor image quality may result in the inability to recognize the ID card or certain fields on the ID card.

Parameters

Required/OptionalParameterTypeDescription
RequiredsignStringThe signature of the caller to this API. For more information about generation of a signature, please refer to API authentication.
Requiredsign_versionStringThe version of the signature algorithm. Please use hmac_sha1 as parameter.
RequiredimageFileA photo of the ID card taken by the user.(Please ensure the quality of the photo. This API does not check photo quality.)
Optionalbiz_noStringThe business serial number.
Optionalreturn_photoStringThis specifies whether to return the portrait. Valid values::
  • "1":Return
  • "0":Not return
The default value is "0".

Return values

ParameterTypeDescription
request_idStringThe unique string to identify each request. This field is always returned unless a 404 (API_NOT_FOUND) error or a 403 (AUTHORIZATION_ERROR) error occurs
time_usedIntThe time taken by the entire request in milliseconds. This field is always returned.
biz_noStringThe business serial number that is provided as an input parameter.
resultIntThe result code for ID card recognition. Valid values:
(If multiple ID cards are present in the image, only the information of the ID card with the highest confidence is returned.)
  • 1001: This indicates that an ID card is recognized (normally) without issues and the ID card information is returned.
  • If the recognition is unsuccessful, the issue will be returned with an error code.
idcard_face_imageStringThe photo on the ID card in Base64.
If no face is detected or the face image is of poor quality, null is returned.
issued_provinceDictThe province where the card is issued. This is returned if the recognition is successful.
issued_cityDictThe city where the card is issued. This is returned if the recognition is successful.
idcard_numberDictThe number of the ID card. This is returned if the recognition is successful.
nameDictThe name on the ID card. This is returned if the recognition is successful.
birth_placeDictThe birthplace on the ID card. This is returned if the recognition is successful.
birthdayDictThe date of birth on the ID card. This is returned if the recognition is successful.
genderDictThe gender on the ID card. This is returned if the recognition is successful.
blood_typeDictThe blood type on the ID card. This is returned if the recognition is successful.
address_areaDictThe address on the ID card. This is returned if the recognition is successful.
address_buildingDictThe unit and building number on the ID card. This is returned if the recognition is successful.
address_quartersDictThe residence community on the ID card. This is returned if the recognition is successful.
address_districtsDictThe subdistrict on the ID card. This is returned if the recognition is successful.
faithDictThe religion on the ID card. This is returned if the recognition is successful.
marriage_typeDictThe marital status on the ID card. This is returned if the recognition is successful.
professionDictThe occupation on the ID card. This is returned if the recognition is successful.
nationalityDictThe nationality on the ID card. This is returned if the recognition is successful.
valid_endDictThe expiration date on the ID card. This is returned if the recognition is successful.
issued_areaDictThe place where the card is issued (the location below the holder's photo). This is returned if the recognition is successful.
valid_startDictThe date when the card is issued. This is returned if the recognition is successful.
card_rectDictThis dictionary contains the coordinates of the four corners of the card. For example:
"rt"{ "x":324, "y":578 }// "lt"{ "x":324, "y":226 }// "lb"{ "x":398, "y":226 }// "rb"{ "x":398, "y":578 }//
ERRORStringThe error code returned if an error occurs. For more information, see the "Error message" section below.

Structure of the recognition fields

FieldTypeDescription
+resultStringThe content recognized.
+condifenceFloatThe confidence score. This is a float ranging from 0 to 100 with 3 decimal places.
+rectDictThis dictionary contains the coordinates of the four corners of the content area. For example:
"rt"{ "x":324, "y":578 }// "lt"{ "x":324, "y":226 }// "lb"{ "x":398, "y":226 }// "rb"{ "x":398, "y":578 }//

Error messages

HTTP status code
Error messageDescription
400IMAGE_ERROR_UNSUPPORTED_FORMATNo ID card was found in the image, or the image quality is too poor to recognize.
400BAD_ARGUMENTS:<key>An error occurred while parsing a parameter (for example, a parameter is a number but a string is used, the value is too long, or the photo cannot be parsed).
400MISSING_ARGUMENTS: <key>A required parameter is missing.
403AUTHORIZATION_ERRORThe signature is invalid.
403AUTHORIZATION_ERROR:The api_key is disabled, call limit is reached, no permission to call this API, or no permission to call this API with the current method.
value:
  • 5001:No permission to call this API with the api_key or the api_key is disabled;
  • 5002:Unauthorized IP. (reserved)
  • 5007:The limit to call this API with the api_key is reached (only when the api_key is a test key).
403CONCURRENCY_LIMIT_EXCEEDEDThe concurrency limit is exceeded.
404API_NOT_FOUNDThe API does not exist.
413Request Entity Too LargeThe size of a photo in the request exceeds 10 MB. This error is returned in text instead of JSON.
500INTERNAL_ERRORInternal server error. If this error occurs, send the request again. If this error persists, contact FaceID customer service or business support.

API authentication

Description

To protect the api_secret when the API is called, a signature mechanism is used to secure communication between servers.

Signature algorithm

raw strings: a=[api_key]&b=[expire_time]&c=[current_time]&d=[random]

Field

FieldValueDescription
aapi_keyThis serves as the username and can be obtained in the FaceID console.
bexpire_timeThe expiration time of the signature. This is a Unix epoch timestamp in seconds
ccurrent_timeThe timestamp when the signature is generated, in seconds. The current_time must be earlier than expire_time.
drandomA custom unsigned decimal integer with a maximum length of 10 digits.

Note: the design of expire_time is for users to use the signature multiple times in a short period.

Generate a signature

Use the HMAC-SHA1 algorithm to encrypt the request.

Signature generation process:

  • Concatenate each fields to create a raw string
  • Use api_secret to sign the raw string with HMAC-SHA1 algorithm.
  • Join the generated signature and the raw and encode it in Base64 to generate a sign signature.

calculation code:

raw = "a={}&b={}&c={}&d={}".format(api_key, expire_time, current_time, random) 
sign_tmp = HMAC-SHA1(api_secret, raw)
sign = Base64(''.join(sign_tmp, raw))

Note:

  • The standard Base64 encoding is used here, instead of a url-safe Base64 encoding.
  • You must use the same set of api_secret and api_key, which can be obtained in the FaceID console.

Demo code

Python demo code

import time
import hashlib
import base64
import random
import hmac
api_key = "your api_key"
api_secret = "your api_secret"
valid_durtion = 100 # valid for 100s
current_time = int(time.time())
expire_time = current_time + valid_durtion
rdm = ''.join(random.choice("0123456789") for i in range(10))
raw = "a={}&b={}&c={}&d={}".format(api_key, expire_time, current_time, rdm)
sign_tmp = hmac.new(api_secret, raw, hashlib.sha1).digest()
sign = base64.b64encode(sign_tmp + raw)

Java demo code

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Random;
 
public class HmacSha1Sign {
 
    /**
     * generate signature
     *
     * @param apiKey
     * @param secretKey
     * @param expired
     * @return
     * @throws Exception
     */
    public static String genSign(String apiKey,  String secretKey, long expired) throws Exception {
        long now = System.currentTimeMillis() / 1000;
        int rdm = Math.abs(new Random().nextInt());
        String plainText = String.format("a=%s&b=%d&c=%d&d=%d", apiKey, now + expired, now, rdm);
        byte[] hmacDigest = HmacSha1(plainText, secretKey);
        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,
                plainText.getBytes().length);
        return encodeToBase64(signContent).replaceAll("[\\s*\t\n\r]", "");
    }
 
    /**
     * generate base64 encoding
     *
     * @param binaryData
     * @return
     */
    public static String encodeToBase64(byte[] binaryData) {
        String encodedStr = Base64.getEncoder().encodeToString(binaryData);
        return encodedStr;
    }
 
    /**
     * generate hmacsha1 sign
     *
     * @param binaryData
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(byte[] binaryData, String key) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA1");
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        mac.init(secretKey);
        byte[] HmacSha1Digest = mac.doFinal(binaryData);
        return HmacSha1Digest;
    }
 
    /**
     * generate hmacsha1 sign
     *
     * @param plainText
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] HmacSha1(String plainText, String key) throws Exception {
        return HmacSha1(plainText.getBytes(), key);
    }
}

Objective-C demo code

#import "ViewController.h"
#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>
#import <math.h>
#define api_key @"你的api_key"
#define api_secret @"你的api_secret"

@interface ViewController ()
@end

@implementation ViewController
- (void)viewDidLoad {
 [super viewDidLoad];
 NSString* sign = [self getSignStr];
 NSLog(@"sign = %@",sign);
}

- (NSString *)getSignStr {
    int valid_durtion = 10000;
    long int current_time = [[NSDate date] timeIntervalSince1970];
    long int expire_time = current_time + valid_durtion;
    long random = abs(arc4random() % 100000000000);
    NSString* str = [NSString stringWithFormat:@"a=%@&b=%ld&c=%ld&d=%ld", api_key, expire_time, current_time, random];
    NSData* sign_tmp = [self hmac_sha1:api_secret text:str];
    NSData* sign_raw_data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSMutableData* data = [[NSMutableData alloc] initWithData:sign_tmp];
    [data appendData:sign_raw_data];
    NSString* sign = [data base64EncodedStringWithOptions:0];
    return sign;
}
- (NSData *)hmac_sha1:(NSString *)key text:(NSString *)text{
    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
    const char *cData = [text cStringUsingEncoding:NSUTF8StringEncoding];
    char cHMAC[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
    
    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
    return HMAC;
}

@end

PHP demo code

<?php
 
function gen_sign($apiKey, $apiSecret, $expired){
    $rdm = rand();
    $current_time = time();
    $expired_time = $current_time + $expired;
    $srcStr = "a=%s&b=%d&c=%d&d=%d";
    $srcStr = sprintf($srcStr, $apiKey, $expired_time, $current_time, $rdm);
    $sign = base64_encode(hash_hmac('SHA1', $srcStr, $apiSecret, true).$srcStr);
    return $sign;
}

© 2012-2021 北京旷视科技有限公司 版权所有 | 京ICP备12036813号-6

京公网安备 11010802025957号