From bb71ac6ffa76d1fcf0a577ac8a6517e20bf8d127 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 18 Jan 2023 16:00:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=20IBarduOcr=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Models/BaiduOcrResult.cs | 27 +++++++++++++++++++ .../Services/BaiduOcr.cs | 25 ++++++++++++++--- .../Services/IBaiduOcr.cs | 9 ++++++- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 src/Extensions/Components/BootstrapBlazor.BaiduOcr/Models/BaiduOcrResult.cs 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); } From 35c3774b077fddc764c2994313230d4c7e29f982 Mon Sep 17 00:00:00 2001 From: Argo-Asicotech Date: Wed, 18 Jan 2023 16:01:01 +0800 Subject: [PATCH 2/2] chore: bump version 7.0.1 --- .../BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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