一、腾讯票据单据识别 Invoice OCR服务介绍
二、开发完整流程
2.1 开通文字识别服务
2.2 创建开发者密钥
2.3 创建项目编写代码集成
using Newtonsoft.Json;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
namespace TrainTicketRecognitionDemo
{
/// <summary>
/// 火车票查询工具类
/// </summary>
public class TrainTicketRecognitionUtils
{
public static TrainTicketRecognitionModel GetTicketStr(string imageUrl)
{
// 注意密钥妥善保存,避免泄露,可以放入配置文件或者数据库中
Credential cred = new Credential
{
SecretId = "个人SecretId",
SecretKey = "个人SecretKey"
};
// 实例化一个client选项,可选的,没有特殊需求可以跳过
ClientProfile clientProfile = new ClientProfile();
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("ocr.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
// 实例化要请求产品的client对象,clientProfile是可选的
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
TrainTicketOCRRequest req = new TrainTicketOCRRequest();
req.ImageUrl = imageUrl;
// 返回的resp是一个TrainTicketOCRResponse的实例,与请求对象对应
TrainTicketOCRResponse resp = client.TrainTicketOCRSync(req);
TrainTicketRecognitionModel trainTicketRecognitionModel = JsonConvert.DeserializeObject<TrainTicketRecognitionModel>(AbstractModel.ToJsonString(resp));
return trainTicketRecognitionModel;
}
}
}
public class TrainTicketRecognitionModel
{
/// <summary>
/// 编号
/// </summary>
public string TicketNum { get; set; }
/// <summary>
/// 出发站
/// </summary>
public string StartStation { get; set; }
/// <summary>
/// 到达站
/// </summary>
public string DestinationStation { get; set; }
/// <summary>
/// 出发时间
/// </summary>
public string Date { get; set; }
/// <summary>
/// 车次
/// </summary>
public string TrainNum { get; set; }
/// <summary>
/// 座位号
/// </summary>
public string Seat { get; set; }
/// <summary>
/// 乘车人姓名
/// </summary>
public string Name { get; set; }
/// <summary>
/// 票价
/// </summary>
public string Price { get; set; }
/// <summary>
/// 席别
/// </summary>
public string SeatCategory { get; set; }
/// <summary>
/// 身份证号
/// </summary>
public string ID { get; set; }
/// <summary>
/// 发票消费类型
/// </summary>
public string InvoiceType { get; set; }
/// <summary>
/// 序列号
/// </summary>
public string SerialNumber { get; set; }
/// <summary>
/// 加收票价
/// </summary>
public string AdditionalCost { get; set; }
/// <summary>
/// 手续费
/// </summary>
public string HandlingFee { get; set; }
/// <summary>
/// 大写金额(票面有大写金额该字段才有值
/// </summary>
public string LegalAmount { get; set; }
/// <summary>
/// 售票站
/// </summary>
public string TicketStation { get; set; }
/// <summary>
/// 原票价(一般有手续费的才有原始票价字段)
/// </summary>
public string OriginalPrice { get; set; }
/// <summary>
/// 发票类型:火车票、火车票补票、火车票退票凭证
/// </summary>
public string InvoiceStyle { get; set; }
/// <summary>
/// 收据号码
/// </summary>
public string ReceiptNumber { get; set; }
/// <summary>
/// 仅供报销使用:1为是,0为否
/// </summary>
public string IsReceipt { get; set; }
/// <summary>
/// 唯一请求 ID,由服务端生成
/// </summary>
public string RequestId { get; set; }
}
/// <summary>
/// 查询代码
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
string url = txtImageUrl.Text;
if (string.IsNullOrWhiteSpace(url))
{
MessageBox.Show("请输入查询车票图片的URL");
}
else
{
TrainTicketRecognitionModel model = TrainTicketRecognitionUtils.GetTicketStr(url);
txtStartStation.Text = model.StartStation;
txtDestinationStation.Text = model.DestinationStation;
txtDate.Text = model.Date;
txtTrainNum.Text= model.TrainNum;
txtPrice.Text = model.Price;
txtSeat.Text = model.Seat;
txtSeatCategory.Text = model.SeatCategory;
txtName.Text = model.Name;
}
}
三、总结