注册 登录

身份证识别识别姓名、性别、民族、出生日期、住址、 身份证号、签发机关、有效期限

身份证识别
所属分类: OCR识别 上线时间: 2019-03-01
使用人数: 342 版      权: 官方自营
  • 0.1元5次
  • 12.0元100次
  • 90.0元1000次
  • 800.0元10000次
   
  • 功能介绍
  • 接口文档
  • 错误代码
说明:
支持对二代居民身份证正反面的关键字段识别,包括姓名、性别、民族、出生日期、住址、身份证号、签发机关、有效期限,识别准确率可达98%以上。 

参数:
请求参数by=1的时候,意思是下载网络图片并识别,需要制定imageUrl参数,忽略imageFile参数; 请求参数by=2的时候,意思是上传本地图片并识别,需要制定imageFile参数,忽略imageUrl参数。 

注意:
可支持PNG、JPG、JPEG、BMP,图片大小不超过4M,长边不大于4096像素,请保证需要识别的部分为图片主体部分。
 接口基本信息
接口地址: https://ai.data5u.com/ocr/idcard
返回格式: JSON
请求方式: GET
请求示例: https://ai.data5u.com/ocr/idcard?imageUrl=http://i3.qhmsg.com/t0148d78bd495777810.jpg&by=1&side=front&appsec=your-appsec&appid=your-appid
备注信息: 342
 请求参数说明
名称 必填 默认值 说明
  appsec YES APPSEC,购买后获取
  appid YES APPID,购买后获取
  imageFile YES 上传文件到服务器,并进行识别,by的值为2
  imageUrl YES 要识别的图片URL地址,by的值为1
  side NO front 身份证正反面,正面front,反面back
  by NO 1 识别方式,1:通过URL地址识别;2:通过图片文件识别,文件大小不超过4M
 返回参数释义
名称 含义
  address 住址
  nation 民族
  sex 性别
  idcard 身份证号
  name 姓名
  birth 出生日期
  image_status 图片状态:
      normal-识别正常;
      reversed_side-身份证正反面颠倒;
      not_idcard-上传的图片中不包含身份证;
      blur-身份证模糊;
      other_type_card-其他类型证照;
      exposure-身份证关键字段反光或过曝;
      dark--身份证欠曝(亮度过低);
      unknown-未知状态
 JSON返回示例
{
    "code": "0",
    "message": "操作成功",
    "timeMs": 991,
    "logId": "d3d147fe-a95c-49fa-8ddf-2d0bcf5dfc26",
    "imageFile": "1a7ef045-485a-449a-9db1-1fd238ccf1e5",
    "result": {
        "address": "江苏省昆山市玉山镇汛塘商苑10幢2209室",
        "nation": "汉",
        "sex": "男",
        "idcard": "320523197309040008",
        "name": "张三四",
        "birth": "19730904",
        "image_status": "normal"
    }
}
 参考代码示例
package com.data5u.api.ocr.test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.json.JSONObject;

/**
 * 代码中用到了fastjson,请引入jar包或者相关maven依赖,地址:https://mvnrepository.com/artifact/com.alibaba/fastjson/1.2.56
 * @author data5u.com
 *
 */
public class ApiTest {

	public static void main(String[] args) {
		
		String host = "https://ai.data5u.com/ocr/idcard";
		String appSec = "请改为你的APPSEC";
		String appId = "请改为你的APPID";
		String imageUrl = "http://i3.qhmsg.com/t0148d78bd495777810.jpg"; // 网络上随意找的图片
		String side = "front"; // 身份证正面,如果是反面取值为back
		Integer by = 1; // 通过提供图片URL地址进行识别,如果本地上传取值为2
		String imageFilePath = "/tmp/23423.png"; // 本地文件路径
		
        try {
        	String result = "";

        	if ( by == 1 ) { // 通过网络图片识别
        		String api = host + "?imageUrl=" + imageUrl + "&by=" + by + "&side=" + side + "&appsec=" + appSec + "&appid=" + appId; 
        		URL realUrl = new URL(api);
        		
                HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
              
                // 定义 BufferedReader输入流来读取URL的响应
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
    		} else if ( by == 2 ) { // 通过本地图片识别
        		String api = host + "?by=" + by + "&side=" + side + "&appsec=" + appSec + "&appid=" + appId; 
        		result = uploadFile(api, imageFilePath);
    		}
    		
            // 输出返回结果
            System.out.println("API返回数据为:" + result);
            JSONObject jsonObject = new JSONObject(result);
            
            // 判断状态码是否为0,0说明请求成功,其他值失败
            String code = jsonObject.getString("code");
            String msg = jsonObject.getString("message");
            
            if ( !"0".equals(code) ) {
				throw new Exception(msg);
			} else {
				JSONObject resultObject = jsonObject.getJSONObject("result");
				System.out.println("姓名:" + resultObject.getString("name"));
				System.out.println("性别:" + resultObject.getString("sex"));
				System.out.println("身份证:" + resultObject.getString("idcard"));
				System.out.println("民族:" + resultObject.getString("nation"));
				System.out.println("住址:" + resultObject.getString("address"));
				System.out.println("生日:" + resultObject.getString("birth"));
			}
           
        } catch (Exception e) {
            System.err.printf("调用API失败:" + e.getMessage());
        }
		
	}
	

    /**
     * 文件上传的方法
     * 
     * @param actionUrl:上传的路径
     * @param uploadFilePath:需要上传的文件路径
     * @return
     */
    public static String uploadFile(String actionUrl, String uploadFilePath) {
        String end = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";

        DataOutputStream ds = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        StringBuffer resultBuffer = new StringBuffer();
        String tempLine = null;

        try {
            // 统一资源
            URL url = new URL(actionUrl);
            // 连接类的父类,抽象类
            URLConnection urlConnection = url.openConnection();
            // http的连接类
            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;

            // 设置是否从httpUrlConnection读入,默认情况下是true;
            httpURLConnection.setDoInput(true);
            // 设置是否向httpUrlConnection输出
            httpURLConnection.setDoOutput(true);
            // Post 请求不能使用缓存
            httpURLConnection.setUseCaches(false);
            // 设定请求的方法,默认是GET
            httpURLConnection.setRequestMethod("POST");
            // 设置字符编码连接参数
            httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
            // 设置字符编码
            httpURLConnection.setRequestProperty("Charset", "UTF-8");
            // 设置请求内容类型
            httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

            // 设置DataOutputStream
            ds = new DataOutputStream(httpURLConnection.getOutputStream());

            String uploadFile = uploadFilePath;
            String filename = uploadFile.substring(uploadFile.lastIndexOf("//") + 1);
            ds.writeBytes(twoHyphens + boundary + end);
            ds.writeBytes("Content-Disposition: form-data; " + "name=\"imageFile\";filename=\"" + filename + "\"" + end);
            ds.writeBytes(end);
            FileInputStream fStream = new FileInputStream(uploadFile);
            int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int length = -1;
            while ((length = fStream.read(buffer)) != -1) {
                ds.write(buffer, 0, length);
            }
            ds.writeBytes(end);
            fStream.close();
        
            ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
            ds.flush();
            if (httpURLConnection.getResponseCode() >= 300) {
                throw new Exception("HTTP Request is not success, Response code is " + httpURLConnection.getResponseCode());
            }

            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                reader = new BufferedReader(inputStreamReader);
                tempLine = null;
                resultBuffer = new StringBuffer();
                while ((tempLine = reader.readLine()) != null) {
                    resultBuffer.append(tempLine);
                    resultBuffer.append("\n");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ds != null) {
                try {
                    ds.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

         }

        return resultBuffer.toString();
    }
}
错误代码 详细释义 其他说明
  1005 请求参数不正确
  1003 服务器内部错误
  1006 OCR识别失败
  1004 文件下载失败
  0 请求成功
  1007 鉴权失败 一般APPSEC或APPID错误会出现鉴权失败
  1008 剩余次数为0 请求次数已用光,请重新购买

购买流程

  • 注册成为平台用户
  • 购买需要API接口
  • 根据文档说明本地调用
  • 获取数据
<
咨询售后


·

官方客服QQ
在线客服 1598729786
咨询热线TEL
4007-745-096官方电话 4007-745-096