赞
踩
最近项目中需要用户上传身份证并且识别信息,进行实名;而身份证识别对咱来说就有点难了,只好去找大厂提供的一些api,改文章为 阿里云--印刷文字识别-身份证识别/OCR文字识别--接口的java代码实现;亲测可用
package org.jeecg.common.util;
import static org.apache.commons.codec.binary.Base64.encodeBase64;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;import lombok.extern.slf4j.Slf4j;
/**
*
* @ClassName: AliCloudORCIDCardUtils
* @Description: 阿里云身份证识别
* @author WangJing
* @date 2020年2月4日
*
*/
@Slf4j
public class AliCloudORCIDCardUtils {public final static String host = "http://dm-51.data.aliyun.com";
public final static String path = "/rest/160601/ocr/ocr_idcard.json";
public final static String appcode = "你的appcode";
/*
* 获取参数的json对象
*/
public static JSONObject getParam(int type, String dataValue) {
JSONObject obj = new JSONObject();
obj.put("dataType", type);
obj.put("dataValue", dataValue);
return obj;
}/**
*
* @Title: distinguishIDCardImgUrl
* @Description: 识别身份证照片信息
* @param @param imgFileUrl 图片url
* @param @param side 身份证正反面类型:face/back
* @param @return
* @param @throws IOException 参数
* @return Pair<Boolean,String> 返回类型
* @throws
*/
public static Pair<Boolean, String> distinguishIDCardImgUrl(String imgFileUrl, String side) {
// 对图像进行base64编码
String imgBase64 = "";
try {
File file = new File(imgFileUrl);
byte[] content = new byte[(int) file.length()];
FileInputStream finputstream = new FileInputStream(file);
finputstream.read(content);
finputstream.close();
imgBase64 = new String(encodeBase64(content));
} catch (IOException e) {
log.error("AliCloudORCIDCardUtils--distinguishIDCardImgUrl--对图像进行base64编码 失败error:{}",
e);
return new Pair<Boolean, String>(false, JSONObject.toJSONString(e));
}
return distinguishIDCardImgBase64(imgBase64, side);
}
/**
*
* @Title: distinguishIDCardImgBase64
* @Description: 识别身份证照片信息
* @param @param imgBase64 图片base64
* @param @param side 身份证正反面类型:face/back
* @param @return 参数
* @return Pair<Boolean,String> 返回类型
* @throws
*/
public static Pair<Boolean, String> distinguishIDCardImgBase64(String imgBase64, String side){
Boolean is_old_format = false;//如果文档的输入中含有inputs字段,设置为True, 否则设置为False
//请根据线上文档修改configure字段
JSONObject configObj = new JSONObject();
configObj.put("side", side);
String config_str = configObj.toString();
// configObj.put("min_size", 5);
// String config_str = "";String method = "POST";
Map<String, String> headers = new HashMap<String, String>();
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
headers.put("Authorization", "APPCODE " + appcode);
Map<String, String> querys = new HashMap<String, String>();
// 拼装请求body的json字符串
JSONObject requestObj = new JSONObject();
try {
if(is_old_format) {
JSONObject obj = new JSONObject();
obj.put("image", getParam(50, imgBase64));
if(config_str.length() > 0) {
obj.put("configure", getParam(50, config_str));
}
JSONArray inputArray = new JSONArray();
inputArray.add(obj);
requestObj.put("inputs", inputArray);
}else{
requestObj.put("image", imgBase64);
if(config_str.length() > 0) {
requestObj.put("configure", config_str);
}
}
} catch (JSONException e) {
log.error("AliCloudORCIDCardUtils--distinguishIDCardImgBase64--拼装请求body的json字符串 失败error:{}",
e);
return new Pair<Boolean, String>(false, JSONObject.toJSONString(e));
}
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){
log.info("Http code: " + stat);
log.info("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));
log.info("Http body error msg:" + EntityUtils.toString(response.getEntity()));
return new Pair<Boolean, String>(false, JSONObject.toJSONString(response));
}String res = EntityUtils.toString(response.getEntity());
JSONObject res_obj = JSON.parseObject(res);
String result;
if(is_old_format) {
JSONArray outputArray = res_obj.getJSONArray("outputs");
result = outputArray.getJSONObject(0).getJSONObject("outputValue").getString("dataValue");
}else{
result = res_obj.toJSONString();
}
return new Pair<Boolean, String>(true, result);
} catch (Exception e) {
log.error("AliCloudORCIDCardUtils--distinguishIDCardImgBase64--请求api分析结果 失败error:{}",
e);
return new Pair<Boolean, String>(false, JSONObject.toJSONString(e));
}
}
}
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。