Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>7.0.0</Version>
<Version>7.0.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/

namespace BootstrapBlazor.Components;

/// <summary>
/// 百度文字识别返回类
/// </summary>
public class BaiduOcrResult<TEntity>
{
/// <summary>
/// 获得/设置 错误码
/// </summary>
public int ErrorCode { get; set; }

/// <summary>
/// 获得/设置 错误描述信息
/// </summary>
/// <remarks>https://ai.baidu.com/ai-doc/OCR/dk3h7y5vr</remarks>
public string? ErrorMessage { get; set; }

/// <summary>
/// 获得/设置 返回实例
/// </summary>
public TEntity? Entity { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,30 @@ public void Scan(byte[] image)
/// <summary>
/// 识别增值税发票方法
/// </summary>
public Task<InvoiceEntity> CheckVatInvoiceAsync(byte[] image) => Task.Run(() =>
public Task<string> CheckVatInvoiceJsonAsync(byte[] image) => Task.Run(() =>
{
var client = new Ocr(Options.CurrentValue.ApiKey, Options.CurrentValue.Secret);
var resp = client.VatInvoice(image);
var ret = resp.GetValue("words_result").ToObject(typeof(InvoiceEntity)) as InvoiceEntity;
return ret ?? new InvoiceEntity() { CommodityName = new(), CommodityTaxRate = new() };
return resp.ToString();
});

/// <summary>
/// 识别增值税发票方法
/// </summary>
public Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image) => Task.Run(() =>
{
var ret = new BaiduOcrResult<InvoiceEntity>();
var client = new Ocr(Options.CurrentValue.ApiKey, Options.CurrentValue.Secret);
var resp = client.VatInvoice(image);
if (resp.TryGetValue("words_result", out var value))
{
ret.Entity = value.ToObject(typeof(InvoiceEntity)) as InvoiceEntity;
}
else
{
ret.ErrorCode = resp.Value<int>("error_code");
ret.ErrorMessage = resp.Value<string>("error_msg");
}
return ret;
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ public interface IBaiduOcr
/// 增值税发票识别
/// </summary>
/// <param name="image"></param>
Task<InvoiceEntity> CheckVatInvoiceAsync(byte[] image);
Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image);

/// <summary>
/// 增值税发票识别
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
Task<string> CheckVatInvoiceJsonAsync(byte[] image);
}