实现增值税发票识别 c# demo
时间: 2023-08-29 17:11:09 浏览: 213
很高兴能为您解答问题。根据您的需求,我为您提供了以下基于 C# 的增值税发票识别 Demo:
```csharp
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace InvoiceRecognitionDemo
{
class Program
{
static async Task Main(string[] args)
{
// 设置请求的 API 地址和 API Key
string url = "https://api-cn.document-ai.baidubce.com/v1/ocr/vat_invoice";
string apiKey = "Your API Key";
// 读取图片文件
byte[] imageBytes = File.ReadAllBytes("Your Image Path");
// 构造 HTTP 请求
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("APPCODE", apiKey);
using (var content = new MultipartFormDataContent())
{
using (var imageContent = new ByteArrayContent(imageBytes))
{
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(imageContent, "image");
// 发送 HTTP 请求
var response = await httpClient.PostAsync(url, content);
// 解析返回的 JSON 数据
var responseContent = await response.Content.ReadAsStringAsync();
var result = JObject.Parse(responseContent);
// 输出识别结果
Console.WriteLine("发票代码:" + result["code"].ToString());
Console.WriteLine("发票号码:" + result["no"].ToString());
Console.WriteLine("开票日期:" + result["date"].ToString());
Console.WriteLine("购方名称:" + result["buyer"]["name"].ToString());
Console.WriteLine("购方纳税人识别号:" + result["buyer"]["taxpayerId"].ToString());
Console.WriteLine("购方地址电话:" + result["buyer"]["address"].ToString() + " " + result["buyer"]["phone"].ToString());
Console.WriteLine("购方开户行及账号:" + result["buyer"]["bank"].ToString() + " " + result["buyer"]["account"].ToString());
Console.WriteLine("货物或应税劳务名称:" + result["items"][0]["name"].ToString());
Console.WriteLine("规格型号:" + result["items"][0]["spec"].ToString());
Console.WriteLine("单位:" + result["items"][0]["unit"].ToString());
Console.WriteLine("数量:" + result["items"][0]["quantity"].ToString());
Console.WriteLine("单价:" + result["items"][0]["price"].ToString());
Console.WriteLine("金额:" + result["items"][0]["amount"].ToString());
Console.WriteLine("税率:" + result["items"][0]["taxRate"].ToString());
Console.WriteLine("税额:" + result["items"][0]["taxAmount"].ToString());
Console.WriteLine("价税合计:" + result["totalAmount"].ToString());
}
}
}
Console.ReadLine();
}
}
}
```
请替换代码中的 Your API Key 和 Your Image Path 为您自己的信息,然后运行程序即可进行增值税发票识别。希望能够对您有所帮助!
阅读全文