springboot整合阿里云OCR实现身份证和营业执照识别
2020-08-15 18:01
120 查看
整合开始之前需要自己去阿里云注册一个号
主要还是在阿里云的演示里把代码拿了过来做了一些修改,现在可以直接使用,HttpUtils包要在
https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java 这里下载
这个是营业执照的代码
import com.alibaba.fastjson.JSONObject; import com.example.ocr.model.BusinessLicense; import com.example.ocr.utils.HttpUtils; import org.apache.http.HttpResponse; import org.apache.http.util.EntityUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import sun.misc.BASE64Encoder; import java.io.IOException; import java.util.HashMap; import java.util.Map; @RestController public class OCRCharterController { @PostMapping("cccc") public Object picOCRBack(@RequestParam("pic") MultipartFile file) throws Exception { String fileStr = ""; if (!file.isEmpty()) { BASE64Encoder encoder = new BASE64Encoder(); // 通过base64来转化图片 try { fileStr = (encoder.encode(file.getBytes())).replaceAll("\r\n", ""); } catch (IOException e) { e.printStackTrace(); } } String host = "https://dm-58.data.aliyun.com"; String path = "/rest/160601/ocr/ocr_business_license.json"; String method = "POST"; String appcode = "自己的appcode号"; Map<String, String> headers = new HashMap<>(); //最后在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 bodys = "{\"image\":\""+fileStr+"\"}"; Object object = null; try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys); System.out.println(response.toString()); String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8"); //转json对象到定义的实体类 object = JSONObject.toJavaObject(JSONObject.parseObject(msg), BusinessLicense.class); System.out.println(object); return object; } catch (Exception e) { e.printStackTrace(); } return object; } }
会输出的对象是
下边这个就是身份证正面的认证
@RequestMapping(value = "faceCode",method = RequestMethod.POST) public Object OCRFace(@RequestParam("code") MultipartFile file) throws Exception{ String fileStr = ""; if (!file.isEmpty()) { BASE64Encoder encoder = new BASE64Encoder(); // 通过base64来转化图片 try { fileStr = (encoder.encode(file.getBytes())).replaceAll("\r\n", ""); } catch (IOException e) { e.printStackTrace(); } } String host = "http://dm-51.data.aliyun.com"; String path = "/rest/160601/ocr/ocr_idcard.json"; String method = "POST"; String appcode = "d23a1eaf2ed642a18f455b907aa190e5"; 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/x-www-form-urlencoded; charset=UTF-8"); Map<String, String> querys = new HashMap<String, String>(); JSONObject requestObj = new JSONObject(); JSONObject configObj = new JSONObject(); configObj.put("side", "face"); requestObj.put("image",fileStr); requestObj.put("configure",configObj.toString()); IdCard object = null; try { HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, requestObj.toString()); String msg = new String(EntityUtils.toString(response.getEntity()).getBytes(),"UTF-8"); object = JSONObject.toJavaObject(JSONObject.parseObject(msg),IdCard.class); System.out.println(object); return object; } catch (Exception e) { e.printStackTrace(); return object; } }
反面认证只是需要吧configObj.put(“side”, “face”);里的face参数改为back就ok了
下面是实体类:
这是身份证的
import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import lombok.ToString; @Data @ToString public class IdCard { //姓名 private String name; //地址 private String address; //性别 private String sex; //身份证号 private String num; //出生年月 private String birth; //民族 private String nationality; //签发机关 private String issue; //开始时间 @JSONField(name = "start_date") private String startDate; //结束时间 @JSONField(name = "end_date") private String endDate; //是否识别成功 private boolean success; //是否复印件 private boolean fake; }
import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import lombok.ToString; @Data @ToString public class BusinessLicense { //地址 private String address; //经营范围 private String business; //注册资本 private String capital; //名称 private String name; //法人 private String person; //注册资金 @JSONField(name = "reg_num") private String regNum; //是否识别成功 private boolean success; //公司类型 private String type; //营业终止日期 @JSONField(name = "valid_period") private String validPeriod; //注册日期 @JSONField(name = "establish_date") private String establishDate; //是否复印件 private boolean fake; }
ok,搞定。
相关文章推荐
- SpringBoot 2.0整合阿里云OSS,实现动静分离架构
- 阿里云--印刷文字识别-身份证识别/OCR文字识别--java代码实现
- SpringBoot 2.0整合阿里云OSS,实现动静分离架构
- SpringBoot整合阿里云SMS短信服务 并实现发送短信验证码的功能 Redis Java 短信验证码
- springboot 整合 gridfs 、webUploader实现大文件分块上传、断点续传、秒传
- Springboot整合MybatisPlus的实现过程解析
- SpringBoot2.X 整合RedisTemplate 简单实现消息队列
- Spring Boot整合JWT实现用户认证
- SpringBoot入门——整合mybatis的增删改简单实现
- SpringBoot 整合 Lettuce Redis的实现方法
- SpringBoot 2.x整合Redis实现RedisTemplate以及缓存
- Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
- docker安装redis连接redis可视化工具Redis Desktop Manager+springboot项目使用Cache缓存+springboot项目整合 Redis 实现缓存
- SpringBoot整合MongoBD实现文件的上传下载
- springboot整合Quartz实现动态配置定时任务
- Kafka和SpringBoot整合实现消息发送与消费
- Springboot整合mybatis实现分页
- Springboot+TKmybatis 整合shior安全框架并实现jwt权限登录以及接口的访问权限
- Spring Boot整合FTPClient线程池的实现示例
- SpringBoot2.0 整合 SpringSecurity 框架实现用户权限安全管理方法