Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat applet OCR idcard


May 20, 2021 WeChat Mini Program Development Document


Table of contents


ocr.idcard

This interface should be called on the server side, as detailed in the Service Side API.
This interface supports cloud calls. Developer tool version required . . . 1.02.1904090 (latest stable download)
wx-server-sdk >= 0.4.0

This interface provides small program-based ID OCR identification

How to call:

  • HTTPS call
  • Cloud calls
  • Incremental call (enhanced version)

HTTPS call

The request address

POST https://api.weixin.qq.com/cv/ocr/idcard?type=MODE&img_url=ENCODE_URL&access_token=ACCESS_TOCKEN

Request parameters

Property Type The default Required Description
access_token string Yes Interface call credential
img_url string Yes The image URL to be detected, this is not transmitted with the IMG parameter.
img FormData Yes The media file identifier in Form-Data, FileName, FileLength, Content-Type, etc., this is not transmitted to IMG_URL.

return value

Object

Returned JSON packet

Attributes type illustrate
errcode string error code
errmsg string Error message
type string Front or back, Front / Back
valid_date string Validity period

Instructions for use

Interface limits the certified subscription number, service number, enterprise number, and applet during the internal test, and the number of times is limited to 500 times / day.Stronger ability demand, you can take service market calls.

Using TIPS This interface is a background interface, you can use the self-service bearer case, with a small program, the photo album selection, etc. can complete the collection, upload, identification, information return and other processes, for needBusiness scenes such as identity cards, bank cards or other physical cards or certificates, collect photos or text messages, etc.

Image Description File size limit: less than 2m

The picture supports the use of the IMG parameter real-time upload, but also supports the use of the IMG_URL parameter to transfer the image address, and download the image by WeChat background.Type has two types

Photographing picture

Photo: Photo model, picture with background (example below)

WeChat applet OCR idcard

Scan: Scan mode, no background picture (example below)

WeChat applet OCR idcard

Request data example

Example 1

curl https://api.weixin.qq.com/cv/ocr/idcard?type=photo&img_url= ENCODE_URL&access_token=ACCESS_TOCKEN

Example 2:

curl -F ‘[email protected]’“https://api.weixin.qq.com/cv/ocr/idcard?type=photo&access_token=ACCESS_TOCKEN”

Return an example of the data

The front returns

{
  "errcode": "0",
  "errmsg": "ok",
  "type": "Front",
  "name": "张三",
  "id": "123456789012345678",
  "addr": "广东省广州市",
  "gender": "男",
  "nationality": "汉"
}

Back to back

{
 "errcode": 0,
 "errmsg": "ok",
 "type": "Back",
 "valid_date": "20070105-20270105"
}

Common error codes

Error code errmsg Description
-1 system error System error, please try again later
101000 invalid image url Picture URL error or pull URL image error
101001 certificate not found The document could not be found in the picture
101002 invalid image data The picture data is not valid

Cloud calls

Cloud call is the ability provided by the small program Cloud Development to call WeChat open interfaces in cloud functions and need to be used in cloud functions via wx-server-sdk.

The interface method

openapi.ocr.idcard
Permissions for the ocr.idcard API need to be configured in config.json, details

Request parameters

Property Type The default Required Description
imgUrl string Is To detect the picture url, pass this without passing the img parameter.
Img FormData Is Form-data media file identification, there is fileame, filelength, content-type and other information, pass this without passing img_url.

The structure of img

Property Type The default Required Description
contentType string Is Data type, incoming MIME Type
value Buffer Is File Buffer

Returns a value

Object

The returned JSON packet

Property Type Description
errCode string Error code
errMsg string The error message
type string Front or back, Front / Back
validDate string Validity

Abnormal

Object

The exception thrown

Property Type Description
errCode string Error code
errMsg string The error message

The legal value of errCode

Value Description The lowest version

Instructions for use

Interface Limits Subscription numbers, service numbers, enterprise numbers, and small programs that have been certified during the internal test can be called directly, with a limit of 500 times per day. Stronger capacity requirements that can go to the service market call.

Using Tips this interface for the background interface, can be based on its own business hosting situation, with small programs of photography, photo selection, etc. together, you can complete the ID photo collection, upload, identification, information return and other processes, for the need to be based on identity cards, bank cards and other physical cards or certificates, collection of photos or text information and other business scenarios.

Image caption File size limit: less than 2M

Images support the use of img parameters for real-time upload, but also img_url parameters to transmit picture addresses, by WeChat background download pictures to identify. There are two types of type

Take a picture sample

Photo: Photo model with background (example below)

WeChat applet OCR idcard

scan: Scan mode, picture without background (example below)

WeChat applet OCR idcard

An example of requesting data

const cloud = require('wx-server-sdk')
cloud.init()
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.ocr.idcard({
        type: 'photo',
        imgUrl: 'ENCODE_URL'
      })
    return result
  } catch (err) {
    return err
  }
}

Or

// cloud = require('wx-server-sdk')
// ...
// 方法返回 Promise
cloud.openapi.ocr.idcard({
  type: 'photo',
  img: {
    contentType: 'image/png',
    value: Buffer
  }
})

Return an example of the data

The front returns

{
  "errCode": 0,
  "errMsg": "openapi.ocr.idcard:ok",
  "type": "Front",
  "name": "张三",
  "id": "123456789012345678",
  "addr": "广东省广州市",
  "gender": "男",
  "nationality": "汉"
}

Back to back

{
  "errCode": 0,
  "errMsg": "openapi.ocr.idcard:ok",
  "type": "Back",
  "validDate": "20070105-20270105"
}