资讯专栏INFORMATION COLUMN

身份证件识别接口编写的JAVA调用示例

Batkid / 285人阅读

摘要:此文章是基本聚合数据证件识别接口来演示,基本请求上传图片并接收数据来处理。

此Java文章是基本聚合数据(https://www.juhe.cn/)-----证件识别接口来演示,基本HTTP POST请求上传图片并接收JSON数据来处理。

使用前你需要通过https://www.juhe.cn/docs/api/...申请一个名片识别的appkey
1.支持的证件类型清单
请求地址:http://api2.juheapi.com/cardr...您申请的appkey
此接口可通过GET请求得到结果,java网络请求有HttpClient相关工具包及HttpURLConnection相关的包等,这里用的是HttpClient,需要先导包,如果用maven话会更方便直接把:

org.apache.httpcomponents  
httpmime  
4.3.6  
  
  
org.apache.httpcomponents  
httpclient  
4.4.1  
  

复制到配置文件 pom.xml
请求代码如下:

public static String get() throws IOException {  
        // 创建HttpClient对象  
        CloseableHttpClient httpClient = HttpClients.createDefault();  
        CloseableHttpResponse response = null;  
        String result = null;  
        try {  
            HttpGet httpGet = new HttpGet("http://api2.juheapi.com/cardrecon/supportlist?key="+ appKey);  
            // 执行网络请求  
            response = httpClient.execute(httpGet);  
            // 获取请求实体  
            HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {  
                // ConverStreamToString是下面写的一个方法是把网络请求的字节流转换为utf8的字符串  
                result = ConvertStreamToString(resEntity.getContent(), "UTF-8");  
            }  
            EntityUtils.consume(resEntity);  
        } catch (Exception e) {  
        } finally {  
            // 关闭请求  
            response.close();  
            httpClient.close();  
        }  
        // 得到的是JSON类型的数据需要第三方解析JSON的jar包来解析  
        return result;  
    }  

2.证件图片识别
请求地址:http://api2.juheapi.com/cardr...

// 此方法是POST请求上传的参数中包含本地图片信息File类型  
    public static String post(String type, File file) throws Exception {  
        CloseableHttpClient httpClient = HttpClients.createDefault();  
        CloseableHttpResponse response = null;  
        String result = null;  
        // HttpClient请求的相关设置,可以不用配置,用默认的参数,这里设置连接和超时时长(毫秒)  
        RequestConfig config = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();  
        try {  
            HttpPost httppost = new HttpPost("http://api2.juheapi.com/cardrecon/upload");  
            // FileBody封装File类型的参数  
            FileBody bin = new FileBody(file);  
            // StringBody封装String类型的参数  
            StringBody keyBody = new StringBody(key, ContentType.TEXT_PLAIN);  
            StringBody typeBody = new StringBody(type, ContentType.TEXT_PLAIN);  
            // addPart将参数传入,并指定参数名称  
            HttpEntity reqEntity = MultipartEntityBuilder.create()  
                    .addPart("pic", bin).addPart("key", keyBody)  
                    .addPart("cardType", typeBody).build();  
            httppost.setEntity(reqEntity);  
            httppost.setConfig(config);  
            // 执行网络请求并返回结果  
            response = httpClient.execute(httppost);  
            HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {  
                result = ConvertStreamToString(resEntity.getContent(), "UTF-8");  
            }  
            EntityUtils.consume(resEntity);  
        } finally {  
            response.close();  
            httpClient.close();  
        }  
        // 得到的是JSON类型的数据需要第三方解析JSON的jar包来解析  
        return result;  
    }  
   
    // 此方法是把传进的字节流转化为相应的字符串并返回,此方法一般在网络请求中用到  
    public static String ConvertStreamToString(InputStream is, String charset)  
            throws Exception {  
        StringBuilder sb = new StringBuilder();  
        try (InputStreamReader inputStreamReader = new InputStreamReader(is,charset)) {  
            try (BufferedReader reader = new BufferedReader(inputStreamReader)) {  
                String line = null;  
                while ((line = reader.readLine()) != null) {  
                    sb.append(line).append("
");  
                }  
            }  
        }  
        return sb.toString();  
    }  

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66134.html

相关文章

  • 用PHP调用证件识别API去识别本地图片

    摘要:前置条件在开始前,请作如下准备学会用输出去聚合数据申请证件识别专用的操作步骤配置好开发环境在相应的本地网站根目录下新建一个文件夹并命名为请准备一张格式的身份证照片本示例中的图片来自网络,并命名为,放在目录请务必确保对有读权限先用测试一下在目 前置条件 在开始前,请作如下准备: 1.学会用PHP输出Hello World 2.去聚合数据申请证件识别专用的KEY:https://www....

    wean 评论0 收藏0
  • 身份证、护照验证 身份证号码获取生日、性别信息

    摘要:功能描述录入一些客户的信息,证件验证,例如身份证护照验证等。身份证号码验证正确以后,手动录入后实现能自动显示生日性别信息,不用手动填写。 功能描述 录入一些客户的信息,证件验证,例如身份证、护照验证等。身份证号码验证正确以后,手动录入后实现能自动显示生日、性别 信息,不用手动填写。 身份证号码组合方式 中华人民共和国公民身份号码 --维基百科,自由的百科全书showImg(https...

    xiaotianyi 评论0 收藏0
  • 身份证、护照验证 身份证号码获取生日、性别信息

    摘要:功能描述录入一些客户的信息,证件验证,例如身份证护照验证等。身份证号码验证正确以后,手动录入后实现能自动显示生日性别信息,不用手动填写。 功能描述 录入一些客户的信息,证件验证,例如身份证、护照验证等。身份证号码验证正确以后,手动录入后实现能自动显示生日、性别 信息,不用手动填写。 身份证号码组合方式 中华人民共和国公民身份号码 --维基百科,自由的百科全书showImg(https...

    AlanKeene 评论0 收藏0

发表评论

0条评论

Batkid

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<