https://market.aliyun.com/products/57124001/cmapi010401.html?#sku=yuncode440100000
登录购买服务
https://netmarket.oss-cn-hangzhou.aliyuncs.com/b91e66f821a84a029f62e5e9f7ca4fb6.pdf?spm=5176.730006-56956004-57124001-cmapi010401.content.13.77b542ef0wkWfc&file=b91e66f821a84a029f62e5e9f7ca4fb6.pdf
该路径为中文文档
配置appcode
工具类
httputils
/**
* @author 作者 Your-Name: jianyuhong
* @version 创建时间:2021年4月28日 下午5:03:15
* 类说明 复制路径 复制util
*/
https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
身份证识别
package cn.zhiquan.app.tvstation.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
public class ocr_idcard {
public static String img_base64(String path) {
/**
* 对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
*/
String imgBase64="";
if (path.startsWith("http")){
imgBase64 = path;
}else {
try {
File file = new File(path);
byte[] content = new byte[(int) file.length()];
FileInputStream finputstream = new FileInputStream(file);
finputstream.read(content);
finputstream.close();
imgBase64 = new String(Base64.encodeBase64(content));
} catch (IOException e) {
e.printStackTrace();
return imgBase64;
}
}
return imgBase64;
}
public static void main(String[] args) {
String tt = ocr_idcard.tt("");
}
public static String tt(String imgFile) {
// imgFile="C:\\Users\\admin\\Pictures\\Camera Roll\\微信图片_20210428172045.jpg";
JSONObject res_obj = null;
String host = "http://dm-51.data.aliyun.com";//固定
String path = "/rest/160601/ocr/ocr_idcard.json";//固定
String appcode = "自己的code";
// String imgFile = "本地图片路径或者图片的url";
String method = "POST";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
//根据API的要求,定义相对应的Content-Type
headers.put("Content-Type", "application/json; charset=UTF-8");
Map<String, String> querys = new HashMap<String, String>();
// 对图像进行base64编码
String imgBase64 = img_base64(imgFile);
//configure配置
JSONObject configObj = new JSONObject();
configObj.put("side", "face");
String config_str = configObj.toString();
// 拼装请求body的json字符串
JSONObject requestObj = new JSONObject();
requestObj.put("image", imgBase64);
if(configObj.size() > 0) {
requestObj.put("configure", config_str);
}
String bodys = requestObj.toString();
try {
/**
* 重要提示如下:
* HttpUtils请从
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
* 下载
*
* 相应的依赖请参照
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
*/
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
int stat = response.getStatusLine().getStatusCode();
if(stat != 200){
System.out.println("Http code: " + stat);
System.out.println("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));
System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));
return "";
}
String res = EntityUtils.toString(response.getEntity());
res_obj = JSON.parseObject(res);
System.out.println(res_obj.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
return res_obj.toJSONString();
}
}
注意
如果报
Http code: 462
http header error msg: null
Http body error msg:Invalid Input - image data error - download image from url error
图片不为身份证