diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj index 9c3d1847bd2..4b5a9993149 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj +++ b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj @@ -1,7 +1,7 @@ - 7.0.0 + 7.0.1 diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Models/BaiduOcrResult.cs b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Models/BaiduOcrResult.cs new file mode 100644 index 00000000000..00d77c82a60 --- /dev/null +++ b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Models/BaiduOcrResult.cs @@ -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; + +/// +/// 百度文字识别返回类 +/// +public class BaiduOcrResult +{ + /// + /// 获得/设置 错误码 + /// + public int ErrorCode { get; set; } + + /// + /// 获得/设置 错误描述信息 + /// + /// https://ai.baidu.com/ai-doc/OCR/dk3h7y5vr + public string? ErrorMessage { get; set; } + + /// + /// 获得/设置 返回实例 + /// + public TEntity? Entity { get; set; } +} diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs index 3f1e270f97d..090ac3d3dc5 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs @@ -32,11 +32,30 @@ public void Scan(byte[] image) /// /// 识别增值税发票方法 /// - public Task CheckVatInvoiceAsync(byte[] image) => Task.Run(() => + public Task 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(); + }); + + /// + /// 识别增值税发票方法 + /// + public Task> CheckVatInvoiceAsync(byte[] image) => Task.Run(() => + { + var ret = new BaiduOcrResult(); + 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("error_code"); + ret.ErrorMessage = resp.Value("error_msg"); + } + return ret; }); } diff --git a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/IBaiduOcr.cs b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/IBaiduOcr.cs index c79def60e69..ce560367752 100644 --- a/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/IBaiduOcr.cs +++ b/src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/IBaiduOcr.cs @@ -13,5 +13,12 @@ public interface IBaiduOcr /// 增值税发票识别 /// /// - Task CheckVatInvoiceAsync(byte[] image); + Task> CheckVatInvoiceAsync(byte[] image); + + /// + /// 增值税发票识别 + /// + /// + /// + Task CheckVatInvoiceJsonAsync(byte[] image); }