Skip to content

Commit ab46d91

Browse files
authored
feat: IBarduOcr support get json raw string (#326)
* feat: 更新 IBarduOcr 接口 * chore: bump version 7.0.1
1 parent 957c94b commit ab46d91

4 files changed

Lines changed: 58 additions & 5 deletions

File tree

src/Extensions/Components/BootstrapBlazor.BaiduOcr/BootstrapBlazor.BaiduOcr.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>7.0.0</Version>
4+
<Version>7.0.1</Version>
55
</PropertyGroup>
66

77
<PropertyGroup>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// 百度文字识别返回类
9+
/// </summary>
10+
public class BaiduOcrResult<TEntity>
11+
{
12+
/// <summary>
13+
/// 获得/设置 错误码
14+
/// </summary>
15+
public int ErrorCode { get; set; }
16+
17+
/// <summary>
18+
/// 获得/设置 错误描述信息
19+
/// </summary>
20+
/// <remarks>https://ai.baidu.com/ai-doc/OCR/dk3h7y5vr</remarks>
21+
public string? ErrorMessage { get; set; }
22+
23+
/// <summary>
24+
/// 获得/设置 返回实例
25+
/// </summary>
26+
public TEntity? Entity { get; set; }
27+
}

src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/BaiduOcr.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,30 @@ public void Scan(byte[] image)
3232
/// <summary>
3333
/// 识别增值税发票方法
3434
/// </summary>
35-
public Task<InvoiceEntity> CheckVatInvoiceAsync(byte[] image) => Task.Run(() =>
35+
public Task<string> CheckVatInvoiceJsonAsync(byte[] image) => Task.Run(() =>
3636
{
3737
var client = new Ocr(Options.CurrentValue.ApiKey, Options.CurrentValue.Secret);
3838
var resp = client.VatInvoice(image);
39-
var ret = resp.GetValue("words_result").ToObject(typeof(InvoiceEntity)) as InvoiceEntity;
40-
return ret ?? new InvoiceEntity() { CommodityName = new(), CommodityTaxRate = new() };
39+
return resp.ToString();
40+
});
41+
42+
/// <summary>
43+
/// 识别增值税发票方法
44+
/// </summary>
45+
public Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image) => Task.Run(() =>
46+
{
47+
var ret = new BaiduOcrResult<InvoiceEntity>();
48+
var client = new Ocr(Options.CurrentValue.ApiKey, Options.CurrentValue.Secret);
49+
var resp = client.VatInvoice(image);
50+
if (resp.TryGetValue("words_result", out var value))
51+
{
52+
ret.Entity = value.ToObject(typeof(InvoiceEntity)) as InvoiceEntity;
53+
}
54+
else
55+
{
56+
ret.ErrorCode = resp.Value<int>("error_code");
57+
ret.ErrorMessage = resp.Value<string>("error_msg");
58+
}
59+
return ret;
4160
});
4261
}

src/Extensions/Components/BootstrapBlazor.BaiduOcr/Services/IBaiduOcr.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,12 @@ public interface IBaiduOcr
1313
/// 增值税发票识别
1414
/// </summary>
1515
/// <param name="image"></param>
16-
Task<InvoiceEntity> CheckVatInvoiceAsync(byte[] image);
16+
Task<BaiduOcrResult<InvoiceEntity>> CheckVatInvoiceAsync(byte[] image);
17+
18+
/// <summary>
19+
/// 增值税发票识别
20+
/// </summary>
21+
/// <param name="image"></param>
22+
/// <returns></returns>
23+
Task<string> CheckVatInvoiceJsonAsync(byte[] image);
1724
}

0 commit comments

Comments
 (0)