支付宝 印刷文字识别_身份证识别
上传身份证图片,自动获取身份证信息
public function verifyCard($img_url,$side= 'face'){
$url = "https://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json";
$appcode = "支付宝提供服务的AppCode";
$file = $img_url;
//如果输入带有inputs, 设置为True,否则设为False
$is_old_format = false;
//如果没有configure字段,config设为空
$config = array(
"side" => $side// $side 是face或者back
);
@$binary = file_get_contents($file);
$base64 = base64_encode($binary); // 转码
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
//根据API的要求,定义相对应的Content-Type
array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
$querys = "";
if($is_old_format == TRUE){
$request = array();
$request["image"] = array(
"dataType" => 50,
"dataValue" => "$base64"
);
if(count($config) > 0){
$request["configure"] = array(
"dataType" => 50,
"dataValue" => json_encode($config)
);
}
$body = json_encode(array("inputs" => array($request)));
}else{
$request = array(
"image" => "$base64"
);
if(count($config) > 0){
$request["configure"] = json_encode($config);
}
$body = json_encode($request);
}
$method = "POST";
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$url, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$result = curl_exec($curl);
$rbody = $result;
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$rheader = substr($result, 0, $header_size);
$httpCode = curl_getinfo($curl,CURLINFO_HTTP_CODE);
if($httpCode == 200){
if($is_old_format){
$output = json_decode($rbody, true);
$result_str = $output["outputs"][0]["outputValue"]["dataValue"];
}else{
$result_str = $rbody;
}
return $result_str;
}else{
$res = ["success"=>false];
return json_encode($res);
}
}
返回结果
正面返回结果:
{
"address" : "浙江省杭州市余杭区文一西路969号", #地址信息
"config_str" : "{\\\"side\\\":\\\"face\\\"}", #配置信息,同输入configure
"face_rect":{ #人脸位置
"angle": -90, #angle表示矩形顺时针旋转的度数
"center":{ #center表示人脸矩形中心坐标
"x" : 952,
"y" : 325.5
},
"size":{ #size表示人脸矩形长宽
"height":181.99,
"width":164.99
}
},
"card_region":[ #身份证区域位置,四个顶点表示,顺序是逆时针(左上、左下、右下、右上)
{"x":165,"y":657},
{"x":534,"y":658},
{"x":535,"y":31},
{"x":165,"y":30}
],
"face_rect_vertices":[ #人脸位置,四个顶点表示
{
"x":1024.6600341796875,
"y":336.629638671875
},
{
"x":906.66107177734375,
"y":336.14801025390625
},
{
"x":907.1590576171875,
"y":214.1490478515625
},
{
"x":1025.157958984375,
"y":214.63067626953125
}
],
"name" : "张三", #姓名
"nationality": "汉", #民族
"num" : "1234567890", #身份证号
"sex" : "男", #性别
"birth" : "20000101", #出生日期
"success" : true #识别结果,true表示成功,false表示失败
}
反面返回结果:
{
"config_str" : "{\\\"side\\\":\\\"back\\\"}", #配置信息,同输入configure
"start_date" : "19700101", #有效期起始时间
"end_date" : "19800101", #有效期结束时间
"issue" : "杭州市公安局", #签发机关
"success" : true #识别结果,true表示成功,false表示失败
}