文章积累知识,如有存在问题,请大家不啬赐教
官方apl地址:
请求地址
POST https://api.weixin.qq.com/cv/ocr/idcard?type=MODE&img_url=ENCODE_URL&access_token=ACCESS_TOCKEN
请求参数
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
access_token | string | 是 | 接口调用凭证 | |
img_url | string | 是 | 要检测的图片 url,传这个则不用传 img 参数。 | |
img | FormData | 是 | form-data 中媒体文件标识,有filename、filelength、content-type等信息,传这个则不用传 img_url。 |
分析:请求参数有两种方式:
1、要检测的图片 url,传这个则不用传 img 参数。
2、form-data 中媒体文件标识,有filename、filelength、content-type等信息,传这个则不用传 img_url。
方案一:img_url
// 根据imgurl识别身份证
public static JSONObject uploadCard(String type, String imgUrl) throws Exception {
String requestUrl = "https://api.weixin.qq.com/cv/ocr/idcard";
String params = "type=" + type + "&img_url=" + imgUrl + "&access_token=" + getToken();
String url = requestUrl + "?" + params;
String data = MyHttpUtils.doPost(url, null);
JSONObject json = JSONObject.parseObject(data);
String errcode = json.getString("errcode");
if (!"0".equals(errcode)) {
throw new AppletsException("-1", "身份识别失败,请检查图片");
}
return json;
}
// 获取token
public static String getToken() throws Exception {
String strTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential" + "&appid=" + appid + "&secret=" + appsecret;
String resulstrToken = MyHttpUtils.doGet(strTokenUrl);
JSONObject object = JSONObject.parseObject(resulstrToken);
String accessToken = object.getString("access_token");
return accessToken;
}
/**
* 向指定 URL 发送POST方法的请求
*
* @param url 发送请求的 URL
* @param param 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String doPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", "multipart/form-data;");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
方案二:img
// 身份证识别
String url = SmallWxUtil.uploadCard("photo");
String postForm = MyHttpUtils.postForm(url, file);
JSONObject object = JSONObject.parseObject(postForm);
String errcode = object.getString("errcode");
if (!"0".equals(errcode)) {
throw new AppletsException("身份识别失败,请上传有效证件");
}
// 请求地址
public static String uploadCard(String type) throws Exception {
String requestUrl = "https://api.weixin.qq.com/cv/ocr/idcard";
String params = "type=" + type + "&access_token=" + getToken();
String url = requestUrl + "?" + params;
return url;
}
/**
* 表单提交参数和文件
*
* @param file
* @return
*/
public static String postForm(String urlPath, File file) {
HttpPost httpPost = new HttpPost(urlPath);
CloseableHttpClient httpClient = HttpClients.createDefault();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
// 文件参数
// 用特定的字段名,接收方用对应的字段名接收
entityBuilder.addPart("fileName", new FileBody(file));
BufferedReader bufferedReader = null;
InputStream in = null;
try {
HttpEntity httpEntity = entityBuilder.build();
httpPost.setEntity(httpEntity);
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20000)
.setSocketTimeout(20000) // read time out
.build();
httpPost.setConfig(config); // 请求配置
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
in = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(in, "utf-8"));
StringBuffer sb = new StringBuffer();
String readLine = null;
while ((readLine = bufferedReader.readLine()) != null) {
sb.append(readLine);
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("请求异常");
return null;
} finally {
try {
if (bufferedReader != null) {
bufferedReader.close();
}
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
返回数据示例
正面返回
{
"errcode": "0",
"errmsg": "ok",
"type": "Front",
"name": "张三",
"id": "123456789012345678",
"addr": "广东省广州市",
"gender": "男",
"nationality": "汉"
}
背面返回
{
"errcode": 0,
"errmsg": "ok",
"type": "Back",
"valid_date": "20070105-20270105"
}