集成微信小程序的时候需要用到自动识别身份证信息填充进去,所以看文档集成百度的卡证识别:

首先百度识别功能很强大:

小程序

有上述功能。

识别身份证需要识别的是身份证的图片,所以就要将图片拿到,在百度api的调用接口中,需要将图片转化成base64才可以用百度的api进行识别,上篇已经简述了图片转化为base64字符串了。下面主要说百度api的调用:

百度识别的url:https://aip.baidubce.com/rest/2.0/ocr/v1/idcard

若想识别还需要token和图片的参数:

1.图片的参数就是拿到的图片的base64的字符串,拼接的参数为:

params = "id_card_side=front&" + URLEncoder.encode("image", "UTF-8") + "="

                    + URLEncoder.encode(base64ImgStr, "UTF-8");

2.token的获取直接上代码:

// 获取token地址String authHost = "https://aip.baidubce.com/oauth/2.0/token?";String getAccessTokenUrl = authHost// 1. grant_type为固定参数+ "grant_type=client_credentials"// 2. 官网获取的 API Key+ "&client_id=" + ak// 3. 官网获取的 Secret Key+ "&client_secret=" + sk;try {URL realUrl = new URL(getAccessTokenUrl);// 打开和URL之间的连接HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();connection.setRequestMethod("GET");connection.connect();// 获取所有响应头字段Map<String, List<String>> map = connection.getHeaderFields();// 遍历所有的响应头字段for (String key : map.keySet()) {logger.info(key + "--->" + map.get(key));}// 定义 BufferedReader输入流来读取URL的响应BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));String result = "";String line;while ((line = in.readLine()) != null) {result += line;}/** * 返回结果示例 */logger.info("result:" + result);

根据你所拿到的参数去请求token,获取结果,这个token是会失效的,所以需要自己进行处理。

然后调用HttpUtil.post(idcardIdentificate, accessToken, params);

请求数据,其中idcardIdentificate是调用百度的api的url:https://aip.baidubce.com/rest/2.0/ocr/v1/idcard

其中所用的请求数据的工具类HttpUtil:

import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.Map;/** * http 工具类 */public class HttpUtil {    public static String post(String requestUrl, String accessToken, String params)            throws Exception {        String contentType = "application/x-www-form-urlencoded";        return HttpUtil.post(requestUrl, accessToken, contentType, params);    }    public static String post(String requestUrl, String accessToken, String contentType, String params)            throws Exception {        String encoding = "UTF-8";        if (requestUrl.contains("nlp")) {            encoding = "GBK";        }        return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);    }    public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)            throws Exception {        String url = requestUrl + "?access_token=" + accessToken;        return HttpUtil.postGeneralUrl(url, contentType, params, encoding);    }    public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)            throws Exception {        URL url = new URL(generalUrl);        // 打开和URL之间的连接        HttpURLConnection connection = (HttpURLConnection) url.openConnection();        connection.setRequestMethod("POST");        // 设置通用的请求属性        connection.setRequestProperty("Content-Type", contentType);        connection.setRequestProperty("Connection", "Keep-Alive");        connection.setUseCaches(false);        connection.setDoOutput(true);        connection.setDoInput(true);        // 得到请求的输出流对象        DataOutputStream out = new DataOutputStream(connection.getOutputStream());        out.write(params.getBytes(encoding));        out.flush();        out.close();        // 建立实际的连接        connection.connect();        // 获取所有响应头字段        Map<String, List<String>> headers = connection.getHeaderFields();        // 遍历所有的响应头字段        for (String key : headers.keySet()) {            System.err.println(key + "--->" + headers.get(key));        }        // 定义 BufferedReader输入流来读取URL的响应        BufferedReader in = null;        in = new BufferedReader(                new InputStreamReader(connection.getInputStream(), encoding));        String result = "";        String getLine;        while ((getLine = in.readLine()) != null) {            result += getLine;        }        in.close();        System.err.println("result:" + result);        return result;    }}

若有疑问咨询QQ:1147726728

小程序

TAG标签: 微信小程序实名认证查询

http://www.xiaochengxub.com/read-106580.html