微信ocr身份证识别


现在人工智能崛起,很多都需要AI智能识别某些东西.
最近就接触了需要调用微信ocr智能识别身份证信息的需求.
就在这里记录一下,微信ocr接口就是 普通的表格文件上传,调用腾讯的接口
下面是代码展示:
首先需要在代码中 模拟文件上传

/*** 身份证识别实现类*/
@Service
public class WxOcrIdcardServiceImpl implements OcrIdcardService {@Resourceprivate WxProperties wxProperties;@Resourceprivate RestTemplate restTemplate;@Resourceprivate IWxTokenService wxTokenServiceImpl;//临时文件后缀private static final String IMG_SUFFIX = ".jpg";private String tempFileName = "";/*** 获取token* @return*/@Overridepublic WxResultData verifyIdcard(byte[] file) {//获取token(腾讯官方文档有详细获取验证码文档)//生成微信ocr验证 url//获取项目路径,把文件存入项目根路径String relativelyPath=System.getProperty("user.dir");//根据项目路径创建文件夹String separator = File.separator;String dir = relativelyPath+separator+"faceImage";//生成文件名称String generate = GuidUtil.generate();//获取文件路径 临时存储路径String path = getFile(file, dir, generate);//设置file路径Map<String, String> fileMap = new HashMap<String, String>();fileMap.put("upfile", path);try{//文件上传,发送到微信端String response = formUpload(ocrUrl, null, fileMap, "");}// 省略一些操作...}/*** 上传图片* @param urlStr* @param textMap* @param fileMap* @param contentType 没有传入文件类型默认采用application/octet-stream* contentType非空采用filename匹配默认的图片类型* @return 返回response数据*/@SuppressWarnings("rawtypes")public  String formUpload(String urlStr, Map<String, String> textMap,Map<String, String> fileMap,String contentType) {String res = "";HttpURLConnection conn = null;// boundary就是request头和上传文件内容的分隔符String BOUNDARY = "---------------------------123821742118716";try {URL url = new URL(urlStr);conn = (HttpURLConnection) url.openConnection();conn.setConnectTimeout(5000);conn.setReadTimeout(30000);conn.setDoOutput(true);conn.setDoInput(true);conn.setUseCaches(false);conn.setRequestMethod("POST");conn.setRequestProperty("Connection", "Keep-Alive");conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");conn.setRequestProperty("Content-Type","multipart/form-data; boundary=" + BOUNDARY);OutputStream out = new DataOutputStream(conn.getOutputStream());// textif (textMap != null) {StringBuffer strBuf = new StringBuffer();Iterator iter = textMap.entrySet().iterator();while (iter.hasNext()) {Map.Entry entry = (Map.Entry) iter.next();String inputName = (String) entry.getKey();String inputValue = (String) entry.getValue();if (inputValue == null) {continue;}strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\"\r\n\r\n");strBuf.append(inputValue);}out.write(strBuf.toString().getBytes());}// fileif (fileMap != null) {Iterator iter = fileMap.entrySet().iterator();while (iter.hasNext()) {Map.Entry entry = (Map.Entry) iter.next();String inputName = (String) entry.getKey();String inputValue = (String) entry.getValue();if (inputValue == null) {continue;}File file = new File(inputValue);String filename = file.getName();//没有传入文件类型,同时根据文件获取不到类型,默认采用application/octet-streamcontentType = new MimetypesFileTypeMap().getContentType(file);//contentType非空采用filename匹配默认的图片类型if(!"".equals(contentType)){if (filename.endsWith(".png")) {contentType = "image/png";}else if (filename.endsWith(".jpg") || filename.endsWith(".jpeg") || filename.endsWith(".jpe")) {contentType = "image/jpeg";}else if (filename.endsWith(".gif")) {contentType = "image/gif";}else if (filename.endsWith(".ico")) {contentType = "image/image/x-icon";}}if (contentType == null || "".equals(contentType)) {contentType = "application/octet-stream";}StringBuffer strBuf = new StringBuffer();strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");strBuf.append("Content-Disposition: form-data; name=\""+ inputName + "\"; filename=\"" + filename+ "\"\r\n");strBuf.append("Content-Type:" + contentType + "\r\n\r\n");out.write(strBuf.toString().getBytes());DataInputStream in = new DataInputStream(new FileInputStream(file));int bytes = 0;byte[] bufferOut = new byte[1024];while ((bytes = in.read(bufferOut)) != -1) {out.write(bufferOut, 0, bytes);}in.close();}}byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();out.write(endData);out.flush();out.close();// 读取返回数据StringBuffer strBuf = new StringBuffer();BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));String line = null;while ((line = reader.readLine()) != null) {strBuf.append(line).append("\n");}res = strBuf.toString();reader.close();reader = null;} catch (Exception e) {System.out.println("发送POST请求出错。" + urlStr);e.printStackTrace();} finally {if (conn != null) {conn.disconnect();conn = null;}}return res;}/***  根据byte数组,生成文件* @param bfile byte字节流* @param filePath 文件存放目录* @param fileName 文件名称(不带后缀的)* @return*/public String getFile(byte[] bfile, String filePath,String fileName) {BufferedOutputStream bos = null;FileOutputStream fos = null;File file = null;String path = "";try {File dir = new File(filePath);if(!dir.exists()&&!dir.isDirectory()){//判断文件目录是否存在dir.mkdirs();}file = File.createTempFile(fileName, IMG_SUFFIX, dir);tempFileName = file.getName();fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos);bos.write(bfile);path = file.getPath();return path;} catch (Exception e) {e.printStackTrace();System.out.println("创建临时文件失败!" + e.getMessage());return null;} finally {if (bos != null) {try {bos.close();} catch (IOException e1) {e1.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e1) {e1.printStackTrace();}}}}
}

springboot ---微信ocr身份证识别相关推荐

  1. springboot 集成 腾讯云ocr身份证识别

    //控制层 @GetMapping("/getCertification")@ApiOperation(value = "ocr身份证识别接口 positiveImg:正 ...

  2. 关于调用百度云OCR身份证识别接口,用Java语言,识别结果缺少身份证号码的问题解决

    问题描述: 最近项目系统开发,使用到了相关证件的信息提取.识别,由于是学校科研使用,选择了百度云OCR文字识别的API.具体的相关识别身份等证件的代码将在另一篇文章中叙述,最近真的太忙了,草稿箱中还有 ...

  3. 国航APP接入百度大脑OCR身份证识别技术,让机票购买更便捷!

    价值成果 中国国航APP通过接入百度大脑OCR身份证识别技术,实现了旅客线上自助修正错购机票信息的功能.购票信息错误的旅客只需在中国国航APP上传身份证照片,即可进行购票身份认证,并立即修正错误信息. ...

  4. 阿里OCR身份证识别相关信息

    阿里OCR身份证识别相关信息 maven <!-- fastjson--> <dependency><groupId>com.alibaba</groupId ...

  5. 腾讯OCR身份证识别信息

    腾讯OCR身份证识别信息 maven <!-- 腾讯身份证识别 --><dependency><groupId>com.qcloud</groupId> ...

  6. Android Study 玩转百度ocr身份证识别不是梦~

    LZ-Says:最近好哥儿们因公来廊坊,在家里可谓夜夜笙歌,喝酒喝的俩眼发懵,最近状态欠佳,导致学习计划一直在滞留,今天通过简短的小例子,重新拾起学习之路,滚蛋吧,懒瘤君~ 前言 Today,由于昨晚 ...

  7. android ocr 身份证识别

    ocr opencv 想必做过程图像识别的同学们都对这两个词不陌生吧. ocr (optical character recognition ,光学字符识别) 是指电子设备(例如扫描仪或数码相机)检查 ...

  8. 阿里云ocr身份证识别接口调用

    1.阿里云界面点击搜索ocr,选择文字识别,开通服务,默认已登录阿里云账号 2.选择个人证件识别,身份证识别(每个月赠送200次体验) 3.点击产品文档 4.下拉到最后,选择RecognizeIdca ...

  9. OCR身份证识别SDK

    在很多地方都需要采集身份证信息,身份证识别OCR则能大大简化信息采集录入的过程.传统手动输入不仅慢,而且容易出错,利用OCR识别技术,则能将繁琐变得高效,将人工变得智能. 身份证识别OCR ,有着数十 ...

  10. 身份证在日常生活重要性 OCR身份证识别的作用

    如何能快速识别提取身份证号.姓名呢?结合使用ocr识别技术,一秒提取,不用手工输入: 身份证识别,识别解决方案 身份证识别OCR技术影响识别率的因素有很多:其中重要因素是图片清晰度,决定因素为字符分割 ...

最新文章

  1. 运维利器:钉钉机器人脚本告警(Linux Shell 篇)
  2. 双脑协同RSVP目标检测
  3. matlab stract结构_MATLAB 的基础知识
  4. transform插件
  5. Android Studio 1.1.0 导入eclipse android project
  6. 跑faster rcnn测试时遇到错误Attribute Error: 'NoneType' object has no attribute 'astype'
  7. 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。一些服务自动停止,如果他们没有什么可做的 ....
  8. python学习(四)--POST请求
  9. linux 网络RPS/RFS/XPS
  10. 如何自定义CSS滚动条的样式?
  11. 在ASP程序中访问Access数据库
  12. RabbitMQ镜像策略set_policy
  13. [笔记]kubernetes 无法启动问题
  14. (5)通过输入参数(测量数据)构建二维体模型(01)
  15. ActiveMQ 无法启动 提示端口被占用 解决方案
  16. SQL Server数据库查询优化【转】
  17. 【情感识别】基于matlab PNN概率神经网络语音情感识别【含Matlab源码 544期】
  18. ADB 环境变量配置教学
  19. 定义一个方法用于判断一个字符串是否是对称的字符串(StringBuilder),例如:abcba、上海自来水来自海上均为对称字符串。
  20. 2020科大讯飞iFLYTEK A.I.开发者大赛

热门文章

  1. java通达信_通达信公式转换JAVA
  2. 计算机思维游戏案例doc,国外火爆的计算机思维训练游戏,国内居然不知道!
  3. .net连接SqlServer数据库
  4. mac安装mysql devel_Mac安装Mysql
  5. Windows -- ThinkPad E470 win10-64bit显示问题和声音播放问题
  6. 微信公众号分销商城(源码+数据库+文档)
  7. cad细等线体不显示_cad字体cass cass如何修改字体
  8. 非标自动化3D选型软件三维SW合集solidworks标准件机械设计电机库
  9. c语言实现猜数字游戏
  10. java安卓开发——1.新项目搭建