百度文字识别API
注册成为百度云管理中心会员
创建一个文字识别的新应用(AppID/APIKey/Secret Key)
后台调用api时需要用到这几个参数。
后台调用照片识别api的方法:
public class picToWord {
//设置APPID/AK/SK
public static final String APP_ID = "123456789";
public static final String API_KEY = "gdsjgiodfjgiofioffjgidfj";
public static final String SECRET_KEY = "fasdfdsfsdfdsfsdfdfdff";
public static String getBussinessLicenseMsg(String image) {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 参数为本地路径.businessLicense,使用营业执照的接口
JSONObject res = client.businessLicense(image, options);
return res.toString(2);
}
}
前端HTML:
直接调用拍照接口或从相册取出照片需要调用微信的拍照接口(jssdk),这里不做描述
<input type="button" name="Image" title="请选择图片" id="Image">上传
后台代码:
//识别营业执照返回执照信息
@RequestMapping(value = "getBLiscenseMsg")
@ResponseBody
public String getBLiscenseMsg(HttpServletRequest request) {
//获取前端传来的图片信息
String pic = request.getParameter("pic");
if (!StringUtils.isBlank(pic)) {
pic = DloadImgUtil.downloadMedias(TokenSchUtil.accessToken_x, pic, "", "");
pic = pic.split("/")[1]; //解析成.jpg格式
}
//调用api得到照片解析后的信息,返回前端ajax
String picMsg = picToWord.getBussinessLicenseMsg(pic);
System.out.println("提取营业执照结果:"+picMsg);
return picMsg;
}
js获取后台返回的信息:
function getPicMsg(){
var pic = $("#imageUrl").val(); //图片信息
alert(pic);
$.ajax({
url: "getBLiscenseMsg",
type: "POST",
data: "pic="+pic,
dataType: "json",
success: function (result) {
//根据返回值的结构提取对应所需的值
var list = result.words_result;
var enterpriseName = list["单位名称"].words;
var chargePerson = list["法人"].words;
var addr = list["地址"].words;
//传到前端input文本框
$('#enterpriseName').val(enterpriseName); //单位名称
$('#chargePerson').val(chargePerson); //法人代表
$('#managerPerson').val(chargePerson);
$('#addr').val(addr); //地址
}
});
}
解析图片返回的内容:
解析图片返回的值:
{
"log_id": 1234567899874563214,
"words_result": {
"社会信用代码": {
"words": "1234567899",
"location": {
"top": 365,
"left": 671,
"width": 187,
"height": 16
}
},
"成立日期": {
"words": "2017年02月30日",
"location": {
"top": 701,
"left": 443,
"width": 155,
"height": 31
}
},
"法人": {
"words": "某某某",
"location": {
"top": 0,
"left": 0,
"width": 0,
"height": 0
}
},
"注册资本": {
"words": "1000万元",
"location": {
"top": 644,
"left": 450,
"width": 37,
"height": 31
}
},
"证件编号": {
"words": "无",
"location": {
"top": 0,
"left": 0,
"width": 0,
"height": 0
}
},
"地址": {
"words": "****路110号",
"location": {
"top": 549,
"left": 448,
"width": 52,
"height": 16
}
},
"单位名称": {
"words": "******有限公司",
"location": {
"top": 429,
"left": 452,
"width": 298,
"height": 26
}
},
"有效期": {
"words": "2041年02月16日",
"location": {
"top": 767,
"left": 642,
"width": 148,
"height": 24
}
}
},
"words_result_num": 8,
"direction": 0
}