百度OCR识别身份证 营业执照接口

首先去百度申请接口

地址:https://login.bce.baidu.com/
有免费的额度

 public static String getAuth()  {
       try{
           String url = "https://aip.baidubce.com/oauth/2.0/token?";
           String getAccessTokenUrl = url
                   // 1. grant_type为固定参数
                   + "grant_type=client_credentials"
                   // 2. 官网获取的 API Key
                   + "&client_id=" + ""
                   // 3. 官网获取的 Secret Key
                   + "&client_secret=" + "";
           HttpGet httpGet = new HttpGet(getAccessTokenUrl);
           HttpClientBuilder httpClientBuilder = HttpClients.custom();
           CloseableHttpClient httpClient = httpClientBuilder.build();
           CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
           HttpEntity httpResponseEntity = httpResponse.getEntity();
           String responseEntityStr = EntityUtils.toString(httpResponseEntity);
           httpResponse.close();
           JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
           String access_token = jsonObject.getString("access_token");

           return access_token;
       }catch (Exception e){
           log.error("获取百度ORC授权失败:"+e.getMessage());
       }
        return null;
    }

    /**
     * 识别身份证
     * @param access_token 权限
     * @param imgUrl url
     * @param id_card_side 正面反面标识
     * @return
     */
    public static JSONObject idcard(String access_token,String imgUrl,String id_card_side){
       try{
           String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
           HttpPost httpPost = new HttpPost(url);
           List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
           list.add(new BasicNameValuePair("access_token", access_token));
           list.add(new BasicNameValuePair("url", imgUrl));
           list.add(new BasicNameValuePair("id_card_side", id_card_side));
           UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"UTF-8");
           httpPost.setEntity(urlEncodedFormEntity);
           httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
           HttpClientBuilder httpClientBuilder = HttpClients.custom();
           CloseableHttpClient httpClient = httpClientBuilder.build();
           CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
           HttpEntity httpResponseEntity = httpResponse.getEntity();
           String responseEntityStr = EntityUtils.toString(httpResponseEntity);
           httpResponse.close();
           JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
            return jsonObject;
       }catch (Exception e){
            log.error("识别身份证失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"+e.getMessage());
       }
       return null;
    }

    /**
     * 识别营业执照
     * @param access_token
     * @param imgUrl
     * @return
     */
    public static JSONObject business_license(String access_token,String imgUrl){
        try{
            String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/business_license";
            HttpPost httpPost = new HttpPost(url);
            List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
            list.add(new BasicNameValuePair("access_token", access_token));
            list.add(new BasicNameValuePair("url", imgUrl));
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list,"UTF-8");
            httpPost.setEntity(urlEncodedFormEntity);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            HttpClientBuilder httpClientBuilder = HttpClients.custom();
            CloseableHttpClient httpClient = httpClientBuilder.build();
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpResponseEntity = httpResponse.getEntity();
            String responseEntityStr = EntityUtils.toString(httpResponseEntity);
            httpResponse.close();
            JSONObject jsonObject = JSONObject.parseObject(responseEntityStr);
            return jsonObject;
        }catch (Exception e){
            log.error("识别营业执照失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n"+e.getMessage());
        }
        return null;
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值