OCR java百度云身份证识别

因项目需要,需要对身份证进行拍照或者扫描,获取到所有相关信息,现就对百度云的OCR技术做个小demo,复制完代码直接可以运行,简单方便,也可以根据自己项目需求做更改!

第一步还是导入相关依赖:

    <!--百度文字识别接口-->
    <dependency>
           <groupId>com.baidu.aip</groupId>
           <artifactId>java-sdk</artifactId>
           <version>4.3.2</version>
    </dependency>

/**
 * 百度文字识别demo
 * 1.获取access_token
 */
public class BaiDuOcr {
    /**
     * 获取权限token

     * @return 返回示例:
     * {
     * "access_token": "24.c9303e47f0729c40f2bc2be6f8f3d589.2592000.1530936208.282335-1234567",
     * "expires_in":2592000
     * }
     */

    public static String getAuth() {
        // 官网获取的 API Key
        String clientId = "API Key";
        // 官网获取的 Secret Key
        String clientSecret = "Secret Key";
        return getAuth(clientId, clientSecret);
    }

    /**
     * 
     * 获取API访问token
     * 该token有一定的有效期,需要自行管理,当失效时需重新获取.
     * @param ak - 百度云的 API Key
     * @param sk - 百度云的 Securet Key
     * @return assess_token 示例:
     * "24.c9303e47f0729c40f2bc2be6f8f3d589.2592000.1530936208.282335-1234567"
     * 
     */

    @SuppressWarnings("unused")
    public static String getAuth(String ak, String sk) {
        // 获取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("POST");//百度推荐使用POST请求
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String result = "";
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.err.println("result:" + result);
            JSONObject jsonObject = new JSONObject(result);
            String access_token = jsonObject.getString("access_token");
            return access_token;
        } catch (Exception e) {
            System.err.printf("获取token失败!");
            e.printStackTrace(System.err);
        }
        return null;
    }
    
    /*public static void main(String[] args) {
        System.out.println(getAuth());
    }*/
}

/**
     * 2.将本地图片进行Base64位编码
     *
     * @param imgUrl
     *            图片的url路径,如e:\\123.png
     * @return
     */

public class BASE64 {
   
    public static String encodeImgageToBase64(File imageFile) {
        // 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
        // 其进行Base64编码处理
        byte[] data = null;
        // 读取图片字节数组
        try {
            FileInputStream in = new FileInputStream(imageFile);
            int count=0;
            while(count==0){
                count=in.available();//InputStream类的available()方法:读取数据
            }
            data = new byte[count];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);// 返回Base64编码过的字节数组字符串
    }
}

/**
 * 3.文字识别
 * 身份证识别
 * @author zl
 * @date 2018/08/30
 *
 */

public class RequestIdentityCard {
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        try {
            //用java JDK自带的URL去请求
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            //设置该请求的消息头
            //设置HTTP方法:POST
            connection.setRequestMethod("POST");
            //设置其Header的Content-Type参数为application/x-www-form-urlencoded
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            // 填入apikey到HTTP header
            connection.setRequestProperty("apikey",API Key);
            //将第二步获取到的token填入到HTTP header
            connection.setRequestProperty("access_token", BaiDuOcr.getAuth());
            connection.setDoOutput(true);
            connection.getOutputStream().write(httpArg.getBytes("UTF-8"));
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 4.获取到的结果是json格式,于是我把他转换成了HashMap
     * @param jsonResult
     * @return
     */

    @SuppressWarnings("unchecked")
    public static HashMap<String,String> getHashMapByJson(String jsonResult){
          @SuppressWarnings("rawtypes")
        HashMap map = new HashMap<String,String>();
          try {
              JSONObject jsonObject = new JSONObject(jsonResult);
              JSONObject words_result= jsonObject.getJSONObject("words_result");
              Iterator<String> it = words_result.keys();              
              while (it.hasNext()){
                  String key = it.next();
                 // System.out.println("1111"+key);
                  JSONObject result = words_result.getJSONObject(key);
                  String value=result.getString("words");
                  switch (key){
                      case "姓名":
                          map.put("name",value);
                          break;
                      case "民族":
                          map.put("nation",value);
                          break;
                      case "住址":
                          map.put("address",value);
                          break;
                      case "公民身份号码":
                          map.put("IDCard",value);
                          break;
                      case "出生":
                          map.put("Birth",value);
                          break;
                      case "性别":
                          map.put("sex",value);
                          break;
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return map;
      }
    
    public static void main(String[] args) {
        //获取本地的绝对路径图片
      File file = new File("d:/pohoto/123.jpg");
      //进行BASE64位编码
      String imageBase = BASE64.encodeImgageToBase64(file);
      imageBase = imageBase.replaceAll("\r\n","");
      imageBase = imageBase.replaceAll("\\+","%2B");
      //百度云的文字识别接口,后面参数为获取到的token
      String httpUrl="https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token="+BaiDuOcr.getAuth();
      String httpArg = "detect_direction=true&id_card_side=front&image="+imageBase;
      String jsonResult = request(httpUrl, httpArg);
      System.out.println("返回的结果--------->"+jsonResult);
      HashMap<String, String> map = getHashMapByJson(jsonResult);
      Collection<String> values=map.values();
      Iterator<String> iterator2=values.iterator();
      while (iterator2.hasNext()){
          System.out.print(iterator2.next()+", ");
      }
  }

}
 

猜你喜欢

转载自blog.csdn.net/zhangleiyes123/article/details/82259413