博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OCR识别
阅读量:4980 次
发布时间:2019-06-12

本文共 8906 字,大约阅读时间需要 29 分钟。

最近作者项目中用到了身份证识别跟营业执照的OCR识别,就研究了一下百度云跟腾讯云的OCR产品接口。

1.腾讯云OCR


 

收费:身份证OCR和营业执照OCR接口,每个接口每个月各有1000次的免费调用

接口说明:

  1. https://cloud.tencent.com/document/product/866/33524

  2. https://cloud.tencent.com/document/product/866/17598

     

身份证-OCR接入

  1. 引入腾讯的SDK及JSON

com.tencentcloudapi
tencentcloud-sdk-java
3.0.58
net.sf.json-lib
json-lib
2.4
jdk15

  2.前端html代码

正面       
反面

  3.后端代码

@PostMapping("uploadFile")  @ResponseBody  public IDCardOCRResponse OCRIdCardTest(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "card_side") String cardSize,Model model){    try {      Credential cred = new Credential("AKIDGQfhYTqEs0DMvUQH93wXKsIX", "7adThzEEH6mK6zg9MMwX0");                    HttpProfile httpProfile = new HttpProfile();          httpProfile.setEndpoint("ocr.tencentcloudapi.com");          ClientProfile clientProfile = new ClientProfile();          clientProfile.setHttpProfile(httpProfile);                                OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);          Map
params = new HashMap<>(); params.put("ImageBase64", getBase64FromInputStream(file.getInputStream())); params.put("CardSide", cardSize); System.out.println(getBase64FromInputStream(file.getInputStream())); IDCardOCRRequest req = IDCardOCRRequest.fromJsonString(JSONObject.fromObject(params).toString(), IDCardOCRRequest.class); IDCardOCRResponse resp = client.IDCardOCR(req); return resp; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; }

说明:new Credential("secretId","secretKey"),这两个参数在腾讯云控制台申请

  4.getBase64FromInputStream代码,把MultipartFile 转为base64

public static String getBase64FromInputStream(InputStream in) {          // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理          byte[] data = null;          // 读取图片字节数组          try {              ByteArrayOutputStream swapStream = new ByteArrayOutputStream();              byte[] buff = new byte[100];              int rc = 0;              while ((rc = in.read(buff, 0, 100)) > 0) {                  swapStream.write(buff, 0, rc);              }              data = swapStream.toByteArray();          } catch (IOException e) {              e.printStackTrace();          } finally {              if (in != null) {                  try {                      in.close();                  } catch (IOException e) {                      e.printStackTrace();                  }              }          }          return new String(Base64.encodeBase64(data));      }

 

运行前端html码,选择身份证图片,点击提交就可以返回身份证的信息了。

营业执照-OCR

1.前端html代码

 

2.后端代码

@PostMapping("bizlicense")  @ResponseBody  public String OCRBizlicenseTest(@RequestParam(value = "file") MultipartFile file) throws Exception{      RestTemplate restTemplate = new RestTemplate();    String apiUrl="https://recognition.image.myqcloud.com/ocr/bizlicense";    HttpHeaders headers = new HttpHeaders();        headers.set("host", "recognition.image.myqcloud.com");        headers.set("content-type", "application/json");        String authorization=QQOCRSignUtils.appSign(XXXX, "AKIDGQfhYTqEs0DXXX", "7adThzEEH6mKXXX", "", 10L);        headers.set("authorization",authorization );               JSONObject params = new JSONObject();        params.put("appid", "XXX");        params.put("image", getBase64FromInputStream(file.getInputStream()));        HttpEntity
entity = new HttpEntity
(params, headers); HttpEntity
response = restTemplate.postForEntity(apiUrl, entity, String.class); return response.getBody(); }

 

3.QQOCRSignUtils.appSign

/**     * 生成 Authorization 签名字段     *      * @param appId     * @param secretId     * @param secretKey     * @param bucketName     * @param expired     * @return     * @throws Exception     */    public static String appSign(long appId, String secretId, String secretKey, String bucketName,            long expired) throws Exception {        long now = System.currentTimeMillis() / 1000;        int rdm = Math.abs(new Random().nextInt());        String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d", appId, bucketName,                secretId, now, now + expired, rdm);        byte[] hmacDigest = HmacSha1(plainText, secretKey);        byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];        System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);        System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length,                plainText.getBytes().length);        return Base64Encode(signContent);    }

运行前端html码,选择营业执照图片,点击提交就可以返回营业执照的信息了。

2.百度OCR


通过以下步骤创建OCR应用,作者当时在这一步花了很长时间

 

 

创建完之后就可以拿到appId,API Key,Secret Key,就可以调用百度提供的api了

收费:身份证OCR和营业执照OCR接口,每个接口每天各有500次的免费调用

接口说明:

  1. https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.BA.AB.E4.BB.BD.E8.AF.81.E8.AF.86.E5.88.AB

  2. https://cloud.baidu.com/doc/OCR/OCR-API.html#.E8.90.A5.E4.B8.9A.E6.89.A7.E7.85.A7.E8.AF.86.E5.88.AB

 

身份证OCR 

只列出后端的代码,前端代码跟腾讯的一样,只不过前后面身份证枚举值不一样,参考接口文档说明。

@PostMapping("ocridcard")  @ResponseBody  public String OCRIdCardTest(@RequestParam(value = "file") MultipartFile file,@RequestParam(value = "card_side") String cardSize,Model model){    try {      RestTemplate restTemplate = new RestTemplate();      HttpEntity
response = restTemplate.postForEntity("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXXXX&client_secret=XXXXXX",null,String.class); JSONObject jsonObject = JSONObject.fromObject(response.getBody()); System.out.println(response.getBody()); String accessToken = jsonObject.getString("access_token"); String apiUrl="https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token="+accessToken; HttpHeaders headers = new HttpHeaders(); headers.set("content-type", "application/x-www-form-urlencoded"); MultiValueMap
params = new LinkedMultiValueMap<>(); params.add("detect_direction", "true"); params.add("id_card_side", cardSize); params.add("image", Base64Utils.getBase64FromInputStream(file.getInputStream())); params.add("detect_risk", "true"); System.out.println(Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(URLDecoder.decode(URLEncoder.encode(Base64Utils.getBase64FromInputStream(file.getInputStream()),"UTF-8"),"UTF-8")); HttpEntity
> entity = new HttpEntity<>(params, headers); response = restTemplate.postForEntity(apiUrl, entity, String.class); return response.getBody(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; }

营业执照OCR

@PostMapping("ocrbusinesslicense")  @ResponseBody  public String OCRBusinessLicenseTest(@RequestParam(value = "file") MultipartFile file,Model model){    try {      RestTemplate restTemplate = new RestTemplate();      HttpEntity
response = restTemplate.postForEntity("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXXX&client_secret=XXXXXX",null,String.class); JSONObject jsonObject = JSONObject.fromObject(response.getBody()); System.out.println(response.getBody()); String accessToken = jsonObject.getString("access_token"); String apiUrl="https://aip.baidubce.com/rest/2.0/ocr/v1/business_license?access_token="+accessToken; HttpHeaders headers = new HttpHeaders(); headers.set("content-type", "application/x-www-form-urlencoded"); MultiValueMap
params = new LinkedMultiValueMap<>(); params.add("detect_direction", "true"); params.add("image", Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(Base64Utils.getBase64FromInputStream(file.getInputStream())); System.out.println(URLDecoder.decode(URLEncoder.encode(Base64Utils.getBase64FromInputStream(file.getInputStream()),"UTF-8"),"UTF-8")); HttpEntity
> entity = new HttpEntity<>(params, headers); response = restTemplate.postForEntity(apiUrl, entity, String.class); return response.getBody(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; }

 

作者:
出处:
如果喜欢作者的文章,请关注“写代码的猿”订阅号以便第一时间获得最新内容。本文版权归作者所有,欢迎转载

 

转载于:https://www.cnblogs.com/lc-chenlong/p/10640607.html

你可能感兴趣的文章
项目中用到的input 遇到的问题的归类
查看>>
leetcode 696. 计数二进制子串(Count Binary Substrings)
查看>>
iOS设计模式
查看>>
ipa 重新签名
查看>>
字符串分割和替换函数(常用)
查看>>
linux 文件系统解析及相关命令
查看>>
Expert Cube Development with Microsoft SQL Server 2008 Analysis Services(3) 第一章
查看>>
Light OJ 1148
查看>>
问题记录之编辑spring.xml文件时没有提示
查看>>
快速幂取模
查看>>
ajax Post方式并返回json字符串提示
查看>>
PHP print_r($_SERVER)
查看>>
原码, 反码, 补码 详解
查看>>
在Eclipse打开css文件时,会自动调用文本编辑器打开,而不是在Eclipse中打开
查看>>
uboot主Makefile之11——源码目录下mkconfig和config.mk文件的区别
查看>>
IOS 总结:NSArray,NSSet,NSDictionary
查看>>
【资料】栈的实现
查看>>
新手如何学习网站建设制作网站
查看>>
php调用外部exe文件system和exec
查看>>
Android notification详解
查看>>