注册 登录

驾驶证识别 对机动车驾驶证所有关键字段进行识别

驾驶证识别
所属分类: OCR识别 上线时间: 2019-03-01
使用人数: 18 版      权: 官方自营
  • 0.1元5次
  • 12.0元100次
  • 90.0元1000次
  • 800.0元10000次
   
  • 功能介绍
  • 接口文档
  • 错误代码
说明:
支持对机动车驾驶证所有关键字段进行识别 

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

注意:
可支持PNG、JPG、JPEG、BMP,图片大小不超过4M,长边不大于4096像素,请保证需要识别的部分为图片主体部分。
 接口基本信息
接口地址: https://ai.data5u.com/ocr/driving-book
返回格式: JSON
请求方式: GET
请求示例: https://ai.data5u.com/ocr/driving-book?imageUrl=http://meizhou.gdrc.com/uimg/1/8/1289821089871.jpg&by=1&appsec=your-appsec&appid=your-appid
备注信息: 18
 请求参数说明
名称 必填 默认值 说明
  appsec YES APPSEC,购买后获取
  appid YES APPID,购买后获取
  imageFile YES 上传文件到服务器,并进行识别,by的值为2
  imageUrl YES 要识别的图片URL地址,by的值为1
  by NO 1 识别方式,1:通过URL地址识别;2:通过图片文件识别,文件大小不超过4M
 返回参数释义
名称 含义
  book_no 证号
  start_time 有效期限
  car_type 准驾车型
  address 住址
  end_time 有效期
  name 姓名
  nation 国籍
  birth 出生日期
  sex 性别
  fetch_time 初次领证日期
 JSON返回示例
{
    "code": "0",
    "message": "操作成功",
    "timeMs": 3599,
    "logId": "8b7ce6b6-55f1-4416-9cbd-b2258200a3d2",
    "imageFile": "7e0d7795-ba04-4b91-9707-eb4ba9728574",
    "result": {
        "start_time": "6年",
        "address": "广西柳州市城中区东环大道268号集体户",
        "nation": "中国",
        "sex": "男",
        "end_time": null,
        "name": "零文超",
        "birth": "19870203",
        "book_no": "450621198702030410",
        "car_type": "C1",
        "fetch_time": "20100114"
    }
}
 参考代码示例
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/driving-book";
		String appSec = "请改为你的APPSEC";
		String appId = "请改为你的APPID";
		String imageUrl = "http://meizhou.gdrc.com/uimg/1/8/1289821089871.jpg"; // 网络上随意找的图片
		Integer by = 1; // 通过提供图片URL地址进行识别,如果本地上传取值为2
		String imageFilePath = "/tmp/23423.png"; // 本地文件路径,如果选择本地上传识别模式,需要此项
		
        try {
        	String result = "";

        	if ( by == 1 ) { // 通过网络图片识别
        		String api = host + "?imageUrl=" + imageUrl + "&by=" + by + "&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 + "&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("book_no"));
				System.out.println("有效期限:" + resultObject.getString("start_time"));
				System.out.println("准驾车型:" + resultObject.getString("car_type"));
				System.out.println("住址:" + resultObject.getString("address"));
				System.out.println("有效期:" + resultObject.getString("end_time"));
				System.out.println("姓名:" + resultObject.getString("name"));
				System.out.println("国籍:" + resultObject.getString("nation"));
				System.out.println("出生日期:" + resultObject.getString("birth"));
				System.out.println("性别:" + resultObject.getString("sex"));
				System.out.println("初次领证日期:" + resultObject.getString("fetch_time"));
			}
           
        } 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