注册 登录

火车票识别支持对大陆火车票的车票号、始发站、目的站、车次、日期、票价、席别、姓名进行结构化识别

火车票识别
所属分类: OCR识别 上线时间: 2019-03-01
使用人数: 165 版      权: 官方自营
  • 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/train
返回格式: JSON
请求方式: GET
请求示例: https://ai.data5u.com/ocr/train?imageUrl=http://i03.c.aliimg.com/img/ibank/2014/058/950/1155059850_1698491453.jpg&by=1&appsec=a&appid=b
备注信息: 165
 请求参数说明
名称 必填 默认值 说明
  appsec YES APPSEC,购买后获取
  appid YES APPID,购买后获取
  imageFile YES 上传文件到服务器,并进行识别,by的值为2
  imageUrl YES 要识别的图片URL地址,by的值为1
  by NO 1 识别方式,1:通过URL地址识别;2:通过图片文件识别,文件大小不超过4M
 返回参数释义
名称 含义
  ticket_num 车票号
  start_station 始发站
  destination_station 到达站
  train_num 车次号
  ticket_price 车票金额
  seat_category 席别
  name 乘客姓名
  date 出发日期
 JSON返回示例
{
    "code": "0",
    "message": "操作成功",
    "timeMs": 12849,
    "logId": "aa03a534-5833-41a5-b119-88c494452431",
    "imageFile": "b1e94e69-df96-492f-809d-8e2978810794",
    "result": {
        "date": "郴州西",
        "seat_category": "Guang Zhounan",
        "ticket_num": "郴州西售",
        "start_station": "2014年01月04日",
        "train_num": "09:31开",
        "ticket_price": "Chenzhou",
        "destination_station": "03车10D号",
        "name": ""
    }
}
 参考代码示例
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/train";
		String appSec = "请改为你的APPSEC";
		String appId = "请改为你的APPID";
		String imageUrl = "http://i03.c.aliimg.com/img/ibank/2014/058/950/1155059850_1698491453.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("ticket_num"));
				System.out.println("始发站:" + resultObject.getString("start_station"));
				System.out.println("到达站:" + resultObject.getString("destination_station"));
				System.out.println("车次号:" + resultObject.getString("train_num"));
				System.out.println("车票金额:" + resultObject.getString("ticket_price"));
				System.out.println("席别:" + resultObject.getString("seat_category"));
				System.out.println("乘客姓名:" + resultObject.getString("name"));
				System.out.println("出发日期:" + resultObject.getString("date"));
			}
           
        } 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