Get text information from ID card picture

      Recently, the information company has a project that needs to collect the identity information of merchants. At that time, it was considered that the functions should be implemented in WeChat instead of the app. In addition to developing two platforms, the app needed to consider various mobile phone versions, which was very cumbersome. However, since the ID card reader purchased has only Android and IOS versions of SDK, there is no SDK that can support WeChat, so the development can only be outsourced to the APP.

     In the past two days, I saw an Avatar data API interface, which is quite powerful and fully capable of directly taking photos of the ID card to obtain the information in the ID card, and there is also an interface for ID card verification processing. The only regret is that the interface is basically charged, if it is not commercial, the cost is too expensive.

     The implementation is relatively simple, first convert the image into a base64 encoded string.

   

/**
	 * Convert image to BASE64 encrypted string.
	 *
	 * @param imagePath
	 * Image path.
	 * @param format
	 * Image Format.
	 * @return
	 */
	public static String convertImageToByte(String imagePath, String format) {
		BASE64Encoder encoder = new BASE64Encoder();
		File file = new File(imagePath);
		BufferedImage bi = null;
		ByteArrayOutputStream baos = null;
		String result = null;
		try {
			bi = ImageIO.read(file);
			baos = new ByteArrayOutputStream();
			ImageIO.write(bi, format == null ? "jpg" : format, baos);
			byte[] bytes = baos.toByteArray();
			result = encoder.encode(bytes).trim();
			System.out.println("Convert the picture to BASE64 encrypted string successfully!"+result);
		} catch (IOException e) {
			System.out.println("Failed to convert image to BASE64 encrypted string: " + e);
		} finally {
			try {
				if (baos != null) {
					baos.close();
					baos = null;
				}
			} catch (Exception e) {
				System.out.println("An exception occurred when closing the file stream: " + e);
			}
		}
		return result;
	}

 

          Pass in the corresponding URL and encoding, and the JSON string corresponding to the ID card can be returned.

  

private static void httpUrlConnection(String pathUrl, String base64Str) {
		try {
			    String param= "key=6766ee9f3dfd4297868bcf024ea68c49&pic_ext=jpg&bas64String="+ base64Str ; //establish connection
			    URL url=new URL(pathUrl);
			    HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
			    //Setting parameters
			    httpConn.setDoOutput(true); //Requires output
			    httpConn.setDoInput(true); //Requires input
			    httpConn.setUseCaches(false); //Do not allow caching
			    httpConn.setRequestMethod("POST"); //Set the POST connection
			    // set request properties
			    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			    httpConn.setRequestProperty("Connection", "Keep-Alive");// maintain a long connection
			    httpConn.setRequestProperty("Charset", "UTF-8");
			    //Connection, you can also connect without clear text, using the following httpConn.getOutputStream() will automatically connect
			    httpConn.connect();
			    //Create an input stream and pass parameters to the pointed URL
			    DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream());
			    dos.writeBytes(param);
			    dos.flush();
			    dos.close();
			    // get response status
			    int resultCode=httpConn.getResponseCode();
			    if(HttpURLConnection.HTTP_OK==resultCode){
			      StringBuffer sb=new StringBuffer();
			      String readLine=new String();
			      BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
			      while((readLine=responseReader.readLine())!=null){
			        sb.append(readLine).append("\n");
			      }
			      responseReader.close();
			      System.out.println(sb.toString());
			    }

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

        The corresponding JSON format:

    

{"result":{"nation":"汉","number":"330782198410185315","name":"xx阳","address":"浙江省XXXXXXX溪村4组","sex":"男"},"error_code":0,"reason":"Succes"}

 

   

   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326528571&siteId=291194637