阿里云OCR-身份证/营业执照识别
在进行具体的服务调用之前,请参见以下步骤,完成准备工作:
创建阿里云AccessKeyId和AccessKeySecret。具体请参见创建AccessKey。
安装Java依赖。具体请参见安装Java依赖。
下载并在项目工程中引入Extension.Uploader工具类。
官方文档:
身份证信息
@RequestMapping(value = "/idCardInfo")@ResponseBodypublic Object idCardInfo(MultipartFile idcardFront) throws ClientException {IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");IAcsClient client = new DefaultAcsClient(profile);ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();// 指定api返回格式imageSyncScanRequest.setAcceptFormat(FormatType.JSON);// 指定请求方法imageSyncScanRequest.setMethod(MethodType.POST);imageSyncScanRequest.setEncoding("utf-8");//支持http和httpsimageSyncScanRequest.setProtocol(ProtocolType.HTTP);JSONObject httpBody = new JSONObject();/*** 设置要检测的场景* ocr: ocr或者ocr卡证识别*/httpBody.put("scenes", Arrays.asList("ocr"));/*** 设置待检测图片, 一张图片一个task,最多支持100张图片同时检测,即需要构建100个task* 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。因此通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高* 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task* 图片二进制数据检测相对于互联网图片链接来说,多了一个上传步骤,上传后取返回的链接进行检测*/ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);byte[] imageBytes = null;String url = null;try{//这里读取本地文件作为二进制数据,当做输入做为示例, 实际使用中请直接替换成您的图片二进制数据//imageBytes = FileUtils.readFileToByteArray();//上传到服务端url = clientUploader.uploadBytes(idcardFront.getBytes());}catch (Exception e){System.out.println("upload file to server fail.");}JSONObject task = new JSONObject();task.put("dataId", UUID.randomUUID().toString());task.put("url", url);task.put("time", new Date());httpBody.put("tasks", Arrays.asList(task));//ocr卡证识别,设置识别卡证类型JSONObject cardExtras = new JSONObject();//身份证正面识别cardExtras.put("card", "id-card-front");//身份证反面//cardExtras.put("card", "id-card-back");httpBody.put("extras", cardExtras);imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);/*** 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置* 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout 异常*/imageSyncScanRequest.setConnectTimeout(3000);imageSyncScanRequest.setReadTimeout(10000);HttpResponse httpResponse = null;try {httpResponse = client.doAction(imageSyncScanRequest);} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();} catch (Exception e){e.printStackTrace();}Map<String, Object> map = new HashMap<String, Object>();//服务端接收到请求,并完成处理返回的结果if(httpResponse != null && httpResponse.isSuccess()){JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));//System.out.println(JSON.toJSONString(scrResponse));int requestCode = scrResponse.getIntValue("code");//每一张图片的检测结果JSONArray taskResults = scrResponse.getJSONArray("data");if (200 == requestCode) {for (Object taskResult : taskResults) {//单张图片的处理结果int taskCode = ((JSONObject)taskResult).getIntValue("code");//图片要检测的场景的处理结果, 如果是多个场景,则会有每个场景的结果JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");if(200 == taskCode){for (Object sceneResult : sceneResults) {String scene = ((JSONObject)sceneResult).getString("scene");String suggestion = ((JSONObject)sceneResult).getString("suggestion");//do something//有识别出卡证信息if("review" .equals(suggestion) && "ocr".equals(scene)){JSONObject idCardInfo = ((JSONObject) sceneResult).getJSONObject("idCardInfo");// System.out.println(idCardInfo.toJSONString());map.put("name", idCardInfo.get("name"));map.put("number", idCardInfo.get("number"));}}}else{//单张图片处理失败, 原因视具体的情况详细分析//System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));map.put("error","task process fail. task response:" + JSON.toJSONString(taskResult));}}} else {/*** 表明请求整体处理失败,原因视具体的情况详细分析*///System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));map.put("error", "the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));}}return map;}
营业执照
@RequestMapping(value = "/businessLicenseInfo")@ResponseBodypublic Object businessLicenseInfo(MultipartFile file) throws ClientException {IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", "请填写您的accessKeyId", "请填写您的accessKeySecret");DefaultProfile.addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");IAcsClient client = new DefaultAcsClient(profile);ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();// 指定api返回格式imageSyncScanRequest.setAcceptFormat(FormatType.JSON);// 指定请求方法imageSyncScanRequest.setMethod(MethodType.POST);imageSyncScanRequest.setEncoding("utf-8");//支持http和httpsimageSyncScanRequest.setProtocol(ProtocolType.HTTP);JSONObject httpBody = new JSONObject();/*** 设置要检测的场景* ocr: ocr或者ocr卡证识别*/httpBody.put("scenes", Arrays.asList("ocr"));/*** 设置待检测图片, 一张图片一个task,最多支持100张图片同时检测,即需要构建100个task* 多张图片同时检测时,处理的时间由最后一个处理完的图片决定。因此通常情况下批量检测的平均rt比单张检测的要长, 一次批量提交的图片数越多,rt被拉长的概率越高* 这里以单张图片检测作为示例, 如果是批量图片检测,请自行构建多个task* 图片二进制数据检测相对于互联网图片链接来说,多了一个上传步骤,上传后取返回的链接进行检测*/ClientUploader clientUploader = ClientUploader.getImageClientUploader(profile, false);byte[] imageBytes = null;String url = null;try{//这里读取本地文件作为二进制数据,当做输入做为示例, 实际使用中请直接替换成您的图片二进制数据//imageBytes = FileUtils.readFileToByteArray((File) file);//上传到服务端url = clientUploader.uploadBytes(file.getBytes());}catch (Exception e){System.out.println("upload file to server fail.");}JSONObject task = new JSONObject();task.put("dataId", UUID.randomUUID().toString());task.put("url", url);task.put("time", new Date());httpBody.put("tasks", Arrays.asList(task));//ocr卡证识别,设置识别卡证类型JSONObject cardExtras = new JSONObject();// 图片类型:营业执照cardExtras.put("card", "business-license");httpBody.put("extras", cardExtras);imageSyncScanRequest.setHttpContent(org.apache.commons.codec.binary.StringUtils.getBytesUtf8(httpBody.toJSONString()), "UTF-8", FormatType.JSON);/*** 请设置超时时间, 服务端全链路处理超时时间为10秒,请做相应设置* 如果您设置的ReadTimeout小于服务端处理的时间,程序中会获得一个read timeout 异常*/imageSyncScanRequest.setConnectTimeout(3000);imageSyncScanRequest.setReadTimeout(10000);HttpResponse httpResponse = null;try {httpResponse = client.doAction(imageSyncScanRequest);} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();} catch (Exception e){e.printStackTrace();}Map<String, Object> map = new HashMap<String, Object>();//服务端接收到请求,并完成处理返回的结果if(httpResponse != null && httpResponse.isSuccess()){JSONObject scrResponse = JSON.parseObject(org.apache.commons.codec.binary.StringUtils.newStringUtf8(httpResponse.getHttpContent()));//System.out.println(JSON.toJSONString(scrResponse));int requestCode = scrResponse.getIntValue("code");//每一张图片的检测结果JSONArray taskResults = scrResponse.getJSONArray("data");if (200 == requestCode) {for (Object taskResult : taskResults) {//单张图片的处理结果int taskCode = ((JSONObject)taskResult).getIntValue("code");//图片要检测的场景的处理结果, 如果是多个场景,则会有每个场景的结果JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");if(200 == taskCode){for (Object sceneResult : sceneResults) {String scene = ((JSONObject)sceneResult).getString("scene");String suggestion = ((JSONObject)sceneResult).getString("suggestion");//do something//有识别出卡证信息if("review" .equals(suggestion) && "ocr".equals(scene)){JSONObject businessLicenseInfo = ((JSONObject) sceneResult).getJSONObject("businessLicenseInfo");System.out.println(businessLicenseInfo.toJSONString());map.put("regNum", businessLicenseInfo.get("regNum"));}}}else{//单张图片处理失败, 原因视具体的情况详细分析//System.out.println("task process fail. task response:" + JSON.toJSONString(taskResult));map.put("error","task process fail. task response:" + JSON.toJSONString(taskResult));}}} else {/*** 表明请求整体处理失败,原因视具体的情况详细分析*///System.out.println("the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));map.put("error", "the whole image scan request failed. response:" + JSON.toJSONString(scrResponse));}}return map;}
阿里云OCR-身份证/营业执照识别相关推荐
- 阿里云OCR身份证信息识别
阿里云OCR身份证信息识别 这里使用的是base64 public JSONObject getCard(MultipartFile file) {String host = "https: ...
- 阿里云 OCR 图片文字识别接口使用案例(java)
阿里云 OCR 图片文字识别接口使用案例(java) 阿里云官方接口文档 前期需要完成 购买阿里云服务 购买服务 可以购买测试服务.每个阿里云用户可以购买1次免费的500次接口请求进行测试 购买完成之 ...
- 阿里云 OCR身份证识别Api 使用(一)
最近项目更新使用拍照识别身份证功能,为了省去用户手动输入身份证号码信息等. 从网上搜了一些方案,发现阿里云的非常便宜,果断采用. 但是没有android 的官方案例 ,只能自己搞了. 首选OCR 识别 ...
- 阿里云OCR通用文字识别和自定义模板OCR识别Python代码及一站式教程
阿里云OCR 一.通用文字识别 1.开通 2.PY3代码 3.测试结果 二.自定义模板识别 1.开通 2.进行自定义模板 3.代码 三.最后 一.通用文字识别 1.开通 开通链接:https://ma ...
- 阿里云ocr身份证识别接口调用
1.阿里云界面点击搜索ocr,选择文字识别,开通服务,默认已登录阿里云账号 2.选择个人证件识别,身份证识别(每个月赠送200次体验) 3.点击产品文档 4.下拉到最后,选择RecognizeIdca ...
- 阿里云OCR图片文字识别(精确识别)
根据需求通过composer下载SDK包 备注:composer 安装SDK包有问题可以指定版本进行升级 (去掉版本号前的^符可以指定版本升级) 代码实现: /*** 图片文字识别* @return ...
- springboot整合阿里云ocr对身份证或通用文字进行识别提取
学习目标: 十分钟学会使用阿里云ocr识别.身份证信息.通用文字.等 环境准备: 创建阿里云账户 开通ocr服务 配置appcode 第一步: 购买对应服务:ocr服务链接 第二步: 导入依赖 < ...
- 阿里云OCR证件识别商业化发布,减少人工审核误差节省成本
随着互联网的发展,利用现代信息技术开展高效快捷便民服务工作,已经势在必行.证件识别算法,极大地提高了办公效率,现已在互联网金融.银行保险.电信通讯运营商.智能交通.政府.航空.社保局等行业领域被广泛运 ...
- 阿里云OCR图片识别
阿里云OCR图片识别 请求参数(Body) 请求示例(java) 正常返回示例 错误码定义 阿里云OCR图片识别: 单字识别,表格识别,旋转功能 准备条件:阿里云OCR图片识别API购买,初次购买1分 ...
- Android平台上实现身份证识别(通过阿里云Api-印刷文字识别_身份证识别)
Android平台上实现身份证识别(通过阿里云Api-印刷文字识别_身份证识别) 一: 前言 继上一篇文章有段时间了,上一篇文章的身份证和银行卡的识别时通过本地的opencv库,tess-two库识别 ...
最新文章
- 通过ID在JavaScript对象数组中查找对象
- “不一样”的真实渗透测试案例分析
- Eclipse中新建SpringBoot项目完成对json、pojo、map、list的请求
- Linux rm命令:删除文件或目录
- vue data变量之间相互赋值或进行数据联动
- 2017-2018-2 20155203《网络对抗技术》 Exp7:网络欺诈防范
- jsp水果商城系统毕业设计网站成品论文
- python画3d图-Python 绘制酷炫的三维图步骤详解
- mysql 5.5多实例部署
- 9.2 定义和浏览翻译
- python3 collections模块 tree_python3上的ete3模块无法导入TreeStyle、faces、AttrFace、NodeSty...
- MinIO-linux-amd64下载
- python“~”符号的用法
- Pyecharts 1.7.0制作图表,运行生成的html文件用浏览器打开空白问题(以桑基图为例)
- 项目启动报错No appropriate protocol (protocol is disabled or cipher suites are inappropriate) 解决办法
- c++数组指针和指针数组详解
- Kafka之Controller(Broker的领导者)
- React-native:FlatList基本使用
- 导航机器测试软件,【图】DSA所有版本+数据升级工具+端口检测+导航游戏+一机多图教程...
- 妖精的尾巴等待服务器响应,妖精的尾巴手游排队掉线、服务器断开、登录失败问题解决方法[多图]...