公司项目需要上传省份证,营业执照等图片,需要获取里面的信息。所以使用了百度文字识别,方便快捷 主要是免费的,虽然一天是有限数的;本来百度上的API文档很清楚了,但是现在还是稍微了的整理下。
首先添加一个百度的jar包
com.baidu.aip
java-sdk
4.11.3
其次随便创建一个类,然后配置必要的数据
上面的都是开通百度识别可以看到的配置数据,所以现在就不做描述了。
public static AipOcr aipOcr(){
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
return client;
}
为了方便所以我写了一个方法来实例化AipOcr类,下面就是具体的方法体了。
/**
* 身份证验证
*
* @param client
* @param url
* @param idCarSide front - 身份证含照片的一面 back - 身份证带国徽的一面
* @return
*/
public static Dict idCard(AipOcr client, String url, String idCarSide) {
// 传入可选参数调用接口
HashMap options = new HashMap();
options.put("detect_direction", "true");
options.put("detect_risk", "false");
String idCardSide = idCarSide;
byte[] bytes = GetImage(url);
JSONObject res = client.idcard(bytes, idCardSide, options);
JSONObject words_result = (JSONObject) res.get("words_result");
try {
if(idCarSide.equals("front")){//正面
JSONObject name = (JSONObject) words_result.get("姓名");
dict.set("name", name.get("words"));
JSONObject nation = (JSONObject) words_result.get("民族");
dict.set("nation", nation.get("words"));
JSONObject address = (JSONObject) words_result.get("住址");
dict.set("address", address.get("words"));
JSONObject idCard = (JSONObject) words_result.get("公民身份号码");
dict.set("idCard", idCard.get("words"));
JSONObject birthTime = (JSONObject) words_result.get("出生");
dict.set("birthTime", birthTime.get("words"));
JSONObject gender = (JSONObject) words_result.get("性别");
dict.set("gender", String.valueOf(gender.get("words")).equals("女")?0:1);
}else {
JSONObject endTime = (JSONObject) words_result.get("失效日期");
dict.set("endTime", endTime.get("words"));
JSONObject issuingOrgan = (JSONObject) words_result.get("签发机关");
dict.set("issuingOrgan", issuingOrgan.get("words"));
JSONObject stateTime = (JSONObject) words_result.get("签发日期");
dict.set("stateTime", stateTime.get("words"));
}
} catch (Exception e) {
e.getMessage();
}
System.out.println(res.toString(2));
return dict;
}
/**
* 驾驶证
*
* @param client
*/
public static Dict drivingLicense(AipOcr client, String url) {
// 传入可选参数调用接口
HashMap options = new HashMap();
options.put("detect_direction", "true");
// 参数为路径
byte[] bytes = GetImage(url);
try {
JSONObject res = client.drivingLicense(bytes, options);
JSONObject words_result = (JSONObject) res.get("words_result");
JSONObject name = (JSONObject) words_result.get("姓名");
dict.set("name", name.get("words"));
JSONObject idNum = (JSONObject) words_result.get("证号");
dict.set("idNum", idNum.get("words"));
JSONObject dataBirth = (JSONObject) words_result.get("出生日期");
dict.set("dataBirth", dataBirth.get("words"));
JSONObject address = (JSONObject) words_result.get("住址");
dict.set("address", address.get("words"));
JSONObject nationality = (JSONObject) words_result.get("国籍");
dict.set("nationality", nationality.get("words"));
JSONObject vehicleType = (JSONObject) words_result.get("准驾车型");
dict.set("vehicleType", vehicleType.get("words"));
JSONObject gender = (JSONObject) words_result.get("性别");
dict.set("gender", gender.get("words").equals("女")?0:1);
JSONObject startTime = (JSONObject) words_result.get("有效期限");
dict.set("startTime", startTime.get("words"));
JSONObject endTime = (JSONObject) words_result.get("至");
dict.set("endTime", endTime.get("words"));
System.out.println(dict);
} catch (Exception e) {
System.out.println(e.getMessage());
}
return dict;
}
/**
* 行驶证
*
* @param client
*/
public static Dict vehicleLicense(AipOcr client, String url) {
// 传入可选参数调用接口
HashMap options = new HashMap();
options.put("detect_direction", "true");
options.put("accuracy", "normal");
byte[] file = GetImage(url);
try {
JSONObject res = client.vehicleLicense(file, options);
System.out.println(res);
JSONObject words_result = (JSONObject) res.get("words_result");
JSONObject vehicleIdentificationNumber = (JSONObject) words_result.get("车辆识别代号");
dict.set("vehicleIdentificationNumber", vehicleIdentificationNumber.get("words"));
JSONObject dateOfRegistration = (JSONObject) words_result.get("注册登记日期");
dict.set("dateOfRegistration", dateOfRegistration.get("words"));
JSONObject address = (JSONObject) words_result.get("住址");
dict.set("address", address.get("words"));
JSONObject brandModel = (JSONObject) words_result.get("品牌型号");
dict.set("brandModel", brandModel.get("words"));
JSONObject issueTime = (JSONObject) words_result.get("发证日期");
dict.set("issueTime", issueTime.get("words"));
JSONObject carType = (JSONObject) words_result.get("车辆类型");
dict.set("carType", carType.get("words"));
JSONObject natureOfUsage = (JSONObject) words_result.get("使用性质");
dict.set("natureOfUsage", String.valueOf(natureOfUsage.get("words")).equals("非运营")?0:1);
JSONObject licenseNumber = (JSONObject) words_result.get("号牌号码");
dict.set("licenseNumber", licenseNumber.get("words"));
} catch (Exception e) {
e.getMessage();
}
return dict;
}
/**
* 营业执照
*
* @param client
*/
public static Dict businessLicense(AipOcr client, String url) {
try {
// 传入可选参数调用接口
HashMap options = new HashMap();
// 参数为本地路径
byte[] bytes = GetImage(url);
JSONObject res = client.businessLicense(bytes, options);
JSONObject words_result = (JSONObject) res.get("words_result");
JSONObject socialCreditCode = (JSONObject) words_result.get("社会信用代码");
dict.set("socialCreditCode", socialCreditCode.get("words"));
JSONObject compositionForm = (JSONObject) words_result.get("组成形式");
dict.set("compositionForm", compositionForm.get("words"));
JSONObject businessScope = (JSONObject) words_result.get("经营范围");
dict.set("businessScope", businessScope.get("words"));
JSONObject legalPerson = (JSONObject) words_result.get("法人");
dict.set("legalPerson", legalPerson.get("words"));
JSONObject createTime = (JSONObject) words_result.get("成立日期");
dict.set("createTime", createTime.get("words"));
JSONObject registeredCapital = (JSONObject) words_result.get("注册资本");
dict.set("registeredCapital", registeredCapital.get("words"));
JSONObject idNum = (JSONObject) words_result.get("证件编号");
dict.set("idNum", idNum.get("words"));
JSONObject address = (JSONObject) words_result.get("地址");
dict.set("address", address.get("words"));
JSONObject unitName = (JSONObject) words_result.get("单位名称");
dict.set("unitName", unitName.get("words"));
JSONObject type = (JSONObject) words_result.get("类型");
dict.set("type", type.get("words"));
JSONObject termOfValidity = (JSONObject) words_result.get("有效期");
dict.set("termOfValidity", termOfValidity.get("words"));
} catch (Exception e) {
System.out.println(e.getMessage());
}
return dict;
}
需要注意的是,百度文字识别开始我写的时候只能识别本地的,所以如果需要识别url图片的话就会出错,所以现在有两个方法把url图片转成byt[]
public static byte[] GetImage(String imgs) {
try {
URL url = new URL(imgs);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通过输入流获取图片数据
byte data[] = readInputStream(inStream);
return data;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
这样直接用测试类调用方法就可以了
public static void main(String[] args) {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
Dict dict = idCard(client, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1574325838156&di=c8be68c0a31ea3891301e3b818211c4d&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20161021%2F719894bc6f594beb8dfa000a359aa345_th.jpeg", "back");
System.out.println(dict);
}
具体的运行结果就不贴出来了,还有就是Dict是hutool的封装方法实现是map,要用的话需要导hutool的maven包