新聞中心
ocr.idcard
本接口應在服務器端調(diào)用,詳細說明參見 服務端API。
本接口支持 云調(diào)用。需開發(fā)者工具版本 >= 1.02.1904090(最新 穩(wěn)定版下載)
wx-server-sdk >= 0.4.0
本接口提供基于小程序的身份證 OCR 識別
調(diào)用方式:
- HTTPS 調(diào)用
- 云調(diào)用
- 增量調(diào)用(加強版)
HTTPS 調(diào)用
請求地址
POST https://api.weixin.qq.com/cv/ocr/idcard?type=MODE&img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
請求參數(shù)
| 屬性 | 類型 | 默認值 | 必填 | 說明 |
|---|---|---|---|---|
| access_token | string | 是 | 接口調(diào)用憑證 | |
| img_url | string | 是 | 要檢測的圖片 url,傳這個則不用傳 img 參數(shù)。 | |
| img | FormData | 是 | form-data 中媒體文件標識,有filename、filelength、content-type等信息,傳這個則不用傳 img_url。 |
返回值
Object
返回的 JSON 數(shù)據(jù)包
| 屬性 | 類型 | 說明 |
|---|---|---|
| errcode | string | 錯誤碼 |
| errmsg | string | 錯誤信息 |
| type | string | 正面或背面,F(xiàn)ront / Back |
| valid_date | string | 有效期 |
使用說明
接口限制 內(nèi)測期間已認證的訂閱號、服務號、企業(yè)號、小程序可直接調(diào)用,次數(shù)限制為500次/天。更強的能力需求,可以走服務市場調(diào)用。
使用 Tips 此接口為后臺接口,可基于自有業(yè)務承載情況,搭配小程序的拍照、相冊選照等一起使用,即可完成身份證照片的采集、上傳、識別、信息返回等流程,用于需要基于身份證、銀行卡等實體卡或證,采集照片或文字信息等的業(yè)務場景。
圖片說明 文件大小限制:小于2M
圖片支持使用img參數(shù)實時上傳,也支持使用img_url參數(shù)傳送圖片地址,由微信后臺下載圖片進行識別。type 有兩種類型
拍攝圖片樣例
photo:拍照模型,帶背景的圖片(示例如下)
scan:掃描模式,不帶背景的圖片(示例如下)
請求數(shù)據(jù)示例
示例1:
curl https://api.weixin.qq.com/cv/ocr/idcard?type=photo&img_url= ENCODE_URL&access_token=ACCESS_TOCKEN
示例2:
curl -F ‘[email protected]’“https://api.weixin.qq.com/cv/ocr/idcard?type=photo&access_token=ACCESS_TOCKEN”
返回數(shù)據(jù)示例
正面返回
{
"errcode": "0",
"errmsg": "ok",
"type": "Front",
"name": "張三",
"id": "123456789012345678",
"addr": "廣東省廣州市",
"gender": "男",
"nationality": "漢"
}
背面返回
{
"errcode": 0,
"errmsg": "ok",
"type": "Back",
"valid_date": "20070105-20270105"
}
常見錯誤碼
| 錯誤碼 | errmsg | 說明 |
|---|---|---|
| -1 | system error | 系統(tǒng)錯誤,請稍后重試 |
| 101000 | invalid image url | 圖片URL錯誤或拉取URL圖像錯誤 |
| 101001 | certificate not found | 圖片中無法找到證件 |
| 101002 | invalid image data | 圖片數(shù)據(jù)無效 |
云調(diào)用
云調(diào)用是小程序·云開發(fā)提供的在云函數(shù)中調(diào)用微信開放接口的能力,需要在云函數(shù)中通過 wx-server-sdk 使用。
接口方法
openapi.ocr.idcard
需在 config.json 中配置 ocr.idcard API 的權限, 詳情
請求參數(shù)
| 屬性 | 類型 | 默認值 | 必填 | 說明 |
|---|---|---|---|---|
| imgUrl | string | 是 | 要檢測的圖片 url,傳這個則不用傳 img 參數(shù)。 | |
| img | FormData | 是 | form-data 中媒體文件標識,有filename、filelength、content-type等信息,傳這個則不用傳 img_url。 |
img 的結構
| 屬性 | 類型 | 默認值 | 必填 | 說明 |
|---|---|---|---|---|
| contentType | string | 是 | 數(shù)據(jù)類型,傳入 MIME Type | |
| value | Buffer | 是 | 文件 Buffer |
返回值
Object
返回的 JSON 數(shù)據(jù)包
| 屬性 | 類型 | 說明 |
|---|---|---|
| errCode | string | 錯誤碼 |
| errMsg | string | 錯誤信息 |
| type | string | 正面或背面,F(xiàn)ront / Back |
| validDate | string | 有效期 |
異常
Object
拋出的異常
| 屬性 | 類型 | 說明 |
|---|---|---|
| errCode | string | 錯誤碼 |
| errMsg | string | 錯誤信息 |
errCode 的合法值
| 值 | 說明 | 最低版本 |
|---|
使用說明
接口限制 內(nèi)測期間已認證的訂閱號、服務號、企業(yè)號、小程序可直接調(diào)用,次數(shù)限制為500次/天。更強的能力需求,可以走服務市場調(diào)用。
使用 Tips 此接口為后臺接口,可基于自有業(yè)務承載情況,搭配小程序的拍照、相冊選照等一起使用,即可完成身份證照片的采集、上傳、識別、信息返回等流程,用于需要基于身份證、銀行卡等實體卡或證,采集照片或文字信息等的業(yè)務場景。
圖片說明 文件大小限制:小于2M
圖片支持使用img參數(shù)實時上傳,也支持使用img_url參數(shù)傳送圖片地址,由微信后臺下載圖片進行識別。type 有兩種類型
拍攝圖片樣例
photo:拍照模型,帶背景的圖片(示例如下)
scan:掃描模式,不帶背景的圖片(示例如下)
請求數(shù)據(jù)示例
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
}
}
或
// cloud = require('wx-server-sdk')
// ...
// 方法返回 Promise
cloud.openapi.ocr.idcard({
type: 'photo',
img: {
contentType: 'image/png',
value: Buffer
}
})
返回數(shù)據(jù)示例
正面返回
{
"errCode": 0,
"errMsg": "openapi.ocr.idcard:ok",
"type": "Front",
"name": "張三",
"id": "123456789012345678",
"addr": "廣東省廣州市",
"gender": "男",
"nationality": "漢"
}
背面返回
{
"errCode": 0,
"errMsg": "openapi.ocr.idcard:ok",
"type": "Back",
"validDate": "20070105-20270105"
}
網(wǎng)站題目:創(chuàng)新互聯(lián)小程序教程:微信小程序OCR·idcard
URL網(wǎng)址:http://fisionsoft.com.cn/article/djhoeec.html


咨詢
建站咨詢

