在市场上识别身份证和银行卡已经有成熟的技术接口了,所以可以直接调用互联网公司官方公布的API接口就ok了。我这里就以百度官方API接口为例吧,当然你也可以调用阿里的,腾讯的。。。。
一、前期准备工作:
1.首先得注册一个百度账号吧,然后登录
2.登录进去之后就是这样喽
3. 点击产品服务--》人工智能--》文字识别
4.然后点击创建应用:
5.点击创建应用后进入这个页面
6.点击立即创建
7.创建成功后点击返回应用列表
8.主要就是为了准备好后面程序中用到的APP_ID,API_KEY,SECRET_KEY这三个参数,下面就可以先把这两组数据保存下来,然后等用的时候直接填上去
注:准备工作就完了,接下来就开始参照百度官方API文档来编写代码了
二、调用官方接口获取图片信息
1. 先从获取身份证图片上面的信息开始:
①.先点开身份证识别
②.然后进去查看文档
③.找Java-SDK文档快速入门
④.按照官方文档要求
注:官方的SDK解压之后可以解出红框框的这四个,其他两个一个日志包和一个读文件的包(这两个根据自己实际情况找)。
⑤.代码部分:
package com.yinxin.server;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
public class Sample {
//设置APPID/AK/SK
private static String APP_ID = "你的APPID"; //
private static String API_KEY ="你的AK"; //
private static String SECRET_KEY = "你的SK"; //
public static void main(String[] args) {
// 初始化一个AipOcr
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 可选:设置网络连接参数
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
options.put("detect_direction", "true");
options.put("detect_risk", "false");
// 识别身份证正面(正面图片为本地图片,即D:\front.jpg)
JSONObject frontres = client.idcard("D:\\front.jpg", "front", options);
System.out.println(frontres.toString(2));
// 识别身份证反面(反面图片为本地图片,即D:\back.jpg)
JSONObject backres = client.idcard("D:\\back.jpg", "back", options);
System.out.println(backres.toString(2));
// 可选:设置代理服务器地址, http和socket二选一,或者均不设置
//client.setHttpProxy("proxy_host", proxy_port); // 设置http代理
//client.setSocketProxy("proxy_host", proxy_port); // 设置socket代理
// 可选:设置log4j日志输出格式,若不设置,则使用默认配置
// 也可以直接通过jvm启动参数设置此环境变量
// System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");
// 调用接口
/* String path = "D:\\front.jpg";
JSONObject res = client.basicGeneral(path, new HashMap<String, String>());
System.out.println(res.toString(2));*/
}
}
运行结果:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/workspace/IdCardRecognition/lib/YinXinLib_1.0.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/workspace/IdCardRecognition/lib/slf4j-simple-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
2019-06-24 19:57:24,450 [BaseClient.java]-[INFO]-199 get access_token success. current state: STATE_AIP_AUTH_OK
身份证正面信息为{
"words_result": {
"性别": {
"words": "男",
"location": {
"height": 19,
"width": 14,
"left": 100,
"top": 83
}
},
"姓名": {
"words": "爱新觉罗·玄烨",
"location": {
"height": 22,
"width": 159,
"left": 94,
"top": 41
}
},
"住址": {
"words": "北京市东城区景山前街4号紫禁城乾清宫",
"location": {
"height": 52,
"width": 224,
"left": 93,
"top": 160
}
},
"公民身份号码": {
"words": "",
"location": {
"height": 0,
"width": 0,
"left": 0,
"top": 0
}
},
"民族": {
"words": "满",
"location": {
"height": 18,
"width": 15,
"left": 198,
"top": 81
}
}
},
"direction": 0,
"words_result_num": 5,
"image_status": "unknown",
"log_id": 7672925894603693304
}
身份证反面信息为{
"words_result": {
"失效日期": {
"words": "20210224",
"location": {
"height": 20,
"width": 110,
"left": 411,
"top": 336
}
},
"签发机关": {
"words": "保定市公安局北市分局",
"location": {
"height": 23,
"width": 225,
"left": 281,
"top": 283
}
},
"签发日期": {
"words": "20110224",
"location": {
"height": 20,
"width": 113,
"left": 279,
"top": 336
}
}
},
"direction": 0,
"words_result_num": 3,
"image_status": "normal",
"log_id": 8068917054983619160
}
照片信息:
2.获取银行卡图片的信息:
过程和获取身份证图片的信息一样:
①直接上代码:
package com.yinxin.server;
import java.util.HashMap;
import org.json.JSONObject;
import com.baidu.aip.ocr.AipOcr;
import com.yinxin.tools.FileTools;
public class Bank {
private static String APP_ID = "你的APPID"; //
private static String API_KEY ="你的AK"; //
private static String SECRET_KEY = "你的SK"; //
public static String sample(AipOcr client,String filePath) {
// 传入可选参数调用接口
HashMap<String, String> options = new HashMap<String, String>();
// 参数为本地图片路径
String image =filePath;
JSONObject res = client.bankcard(image, options);
System.out.println(res.toString(2));
// 参数为本地图片二进制数组
byte[] file = FileTools.readFile(image);
res = client.bankcard(file, options);
System.out.println(res.toString(2));
String result=res.toString(2);
return result;
}
public static void main(String[] args) {
AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
String result=sample(client,"C://Users//syp//Desktop//bank.jpg");
System.out.println("识别银行卡信息["+result+"]");
}
}
②运行效果:
总结:按照官方文档走,没问题,简单demo,如果需要复杂业务方面还需要在加属性