一、引入jar包
<!--百度ocr依赖-->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.4.1</version>
</dependency>
二、创建一个ocr客户端
AipOcr client = new AipOcr("21991***","Lvm6itXL5AMVBajPRP***","Kh80i1Ds0ZBaIDGFbwjv6IEhfN2***");
AipOcr参数说明(记住:这里要换成自己的应用信息):
appid:21991***
apiKey:Lvm6itXL5AMVBajPRP***
secretKey:Kh80i1Ds0ZBaIDGFbwjv6****Bs
三、调用客户端里封装的识别身份证接口(idcard)
// url为传入的一个网络图片地址
//new一个URL对象
HttpsUrlValidator.retrieveResponseFromServer(url);
URL realUrl= new URL(url);
//打开链接
HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
//设置请求方式为"GET"
conn.setRequestMethod("GET");
//超时响应时间为5秒
conn.setConnectTimeout(5 * 1000);
//通过输入流获取图片数据
InputStream imageStream = conn.getInputStream();
byte[] file = IOUtils.toByteArray(imageStream);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
options.put("detect_direction", "true");
options.put("detect_risk", "false");
// 参数为本地图片二进制数组,idCardSide为身份证的正反面,具体看下面参数说明
org.json.JSONObject ress = client.idcard(file, idCardSide, options);
参数说明:
image:图片流
idCardSide:身份证正面或者反面(front-人像面,back-国徽面)
options:传入可选参数调用集合
options参数说明:
参数 | 是否必选 | 类型 | 可选值范围 | 说明 |
detect_direction | 否 | string | - | 此参数新版本无需传,支持自动检测图像旋转角度 |
detect_risk | 否 | string | true/false | 是否开启身份证风险类型(身份证复印件、临时身份证、身份证翻拍、修改过的身份证)功能,默认不开启,即:false。 -true:开启,请查看返回参数risk_type; -false:不开启 |
detect_photo | 否 | string | true/false | 是否检测头像内容,默认不检测。可选值:true-检测头像并返回头像的 base64 编码及位置信息 |
detect_rectify | 否 | string | - | 此参数新版本无需传,支持自动校验图片完整性,可识别完整和切片身份证 |
返回说明
返回参数
字段 | 是否必选 | 类型 | 说明 |
direction | 否 | int32 | 图像方向,当图像旋转时,返回该参数。 - -1:未定义, - 0:正向, - 1: 逆时针90度, - 2:逆时针180度, - 3:逆时针270度 |
image_status | 是 | string | normal-识别正常 reversed_side-身份证正反面颠倒 non_idcard-上传的图片中不包含身份证 blurred-身份证模糊 other_type_card-其他类型证照 over_exposure-身份证关键字段反光或过曝 over_dark-身份证欠曝(亮度过低) unknown-未知状态 |
risk_type | 否 | string | 输入参数 detect_risk = true 时,则返回该字段识别身份证类型: normal-正常身份证;copy-复印件;temporary-临时身份证;screen-翻拍;unknown-其他未知情况 |
edit_tool | 否 | string | 如果参数 detect_risk = true 时,则返回此字段。如果检测身份证被编辑过,该字段指定编辑软件名称,如:Adobe Photoshop CC 2014 (Macintosh),如果没有被编辑过则返回值无此参数 |
log_id | 是 | uint64 | 唯一的log id,用于问题定位 |
photo | 否 | string | 当请求参数 detect_photo = true时返回,头像切图的 base64 编码(无编码头,需自行处理) |
photo_location | 否 | array() | 当请求参数 detect_photo = true时返回,头像的位置信息(坐标0点为左上角) |
idcard_number_type | 是 | string | 用于校验身份证号码、性别、出生是否一致,输出结果及其对应关系如下: -1: 身份证正面所有字段全为空 0: 身份证证号不合法,此情况下不返回身份证证号 1: 身份证证号和性别、出生信息一致 2: 身份证证号和性别、出生信息都不一致 3: 身份证证号和出生信息不一致 4: 身份证证号和性别信息不一致 |
words_result | 是 | array() | 定位和识别结果数组 |
words_result_num | 是 | uint32 | 识别结果数,表示words_result的元素个数 |
+location | 是 | array() | 位置数组(坐标0点为左上角) |
++left | 是 | uint32 | 表示定位位置的长方形左上顶点的水平坐标 |
++top | 是 | uint32 | 表示定位位置的长方形左上顶点的垂直坐标 |
++width | 是 | uint32 | 表示定位位置的长方形的宽度 |
++height | 是 | uint32 | 表示定位位置的长方形的高度 |
+words | 否 | string | 识别结果字符串 |
返回示例
{
"log_id": 2648325511,
"direction": 0,
"image_status": "normal",
"photo": "/9j/4AAQSkZJRgABA......",
"photo_location": {
"width": 1189,
"top": 638,
"left": 2248,
"height": 1483
},
"words_result": {
"住址": {
"location": {
"left": 267,
"top": 453,
"width": 459,
"height": 99
},
"words": "南京市江宁区弘景大道3889号"
},
"公民身份号码": {
"location": {
"left": 443,
"top": 681,
"width": 589,
"height": 45
},
"words": "330881199904173914"
},
"出生": {
"location": {
"left": 270,
"top": 355,
"width": 357,
"height": 45
},
"words": "19990417"
},
"姓名": {
"location": {
"left": 267,
"top": 176,
"width": 152,
"height": 50
},
"words": "伍云龙"
},
"性别": {
"location": {
"left": 269,
"top": 262,
"width": 33,
"height": 52
},
"words": "男"
},
"民族": {
"location": {
"left": 492,
"top": 279,
"width": 30,
"height": 37
},
"words": "汉"
}
},
"words_result_num": 6
}