Skip to content

Commit 293bbf2

Browse files
committed
!3771 feat(#I6AY0F): add baidu ocr component
* doc: 更新使用方法 * doc: 增加源码链接配置 * doc: 增加 IBarduOcr 菜单 * chore: 移除重复菜单 * doc: 增加增值税票据识别组件菜单 * Merge branch 'main' into feat/Baidu.Orc * doc: 更新示例 * refactor: 更改 SaveToFile 方法为异步方法 * refactor: SaveToFile 更改为扩展方法 * refactor: GetBytes 更改为扩展方法 * refactor: RequestBase64ImageFileAsync 更改为扩展方法 * doc: 增加格式化 * doc: 增加发票明细列表 * doc: 更新 Tips 文档 * doc: 不显示已上传列表 * chore: 更新项目依赖项 * doc: 格式化文档 * doc: 增加 BaiduOcr 示例 * chore: 增加 Logo * refactor: 更新 UploadFile * doc: 更新示例 * feat: 增加增值税识别方法 * feat: 增加增值税实体类 * doc: 增加反序列化示例 * doc: 增加 BaiduOrc 示例 * feat: 更新 IBaiduOrc 接口 * chore: 增加 BaiduOrc 识别服务 * feat: 增加百度增值税发票识别接口 * chore: 增加百度文字识别配置项 * doc: 增加注释 * Merge branch 'main' into feat/Baidu.Orc * chore: 增加 BaiduOcr 工程 * refactor: 精简代码消除警告信息 * feat: 增加文字识别服务
1 parent 35726b0 commit 293bbf2

26 files changed

Lines changed: 564 additions & 152 deletions

File tree

BootstrapBlazor.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "localization", "localizatio
140140
localization\zh-TW.json = localization\zh-TW.json
141141
EndProjectSection
142142
EndProject
143+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BootstrapBlazor.BaiduOcr", "src\Extensions\Components\BootstrapBlazor.BaiduOcr\BootstrapBlazor.BaiduOcr.csproj", "{F800DC0F-6829-4B64-B96B-B301A512AF2A}"
144+
EndProject
143145
Global
144146
GlobalSection(SolutionConfigurationPlatforms) = preSolution
145147
Debug|Any CPU = Debug|Any CPU
@@ -234,6 +236,10 @@ Global
234236
{7F7F3B85-7B15-485C-8DC2-BAFD8C086957}.Debug|Any CPU.Build.0 = Debug|Any CPU
235237
{7F7F3B85-7B15-485C-8DC2-BAFD8C086957}.Release|Any CPU.ActiveCfg = Release|Any CPU
236238
{7F7F3B85-7B15-485C-8DC2-BAFD8C086957}.Release|Any CPU.Build.0 = Release|Any CPU
239+
{F800DC0F-6829-4B64-B96B-B301A512AF2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
240+
{F800DC0F-6829-4B64-B96B-B301A512AF2A}.Debug|Any CPU.Build.0 = Debug|Any CPU
241+
{F800DC0F-6829-4B64-B96B-B301A512AF2A}.Release|Any CPU.ActiveCfg = Release|Any CPU
242+
{F800DC0F-6829-4B64-B96B-B301A512AF2A}.Release|Any CPU.Build.0 = Release|Any CPU
237243
EndGlobalSection
238244
GlobalSection(SolutionProperties) = preSolution
239245
HideSolutionNode = FALSE
@@ -271,6 +277,7 @@ Global
271277
{9552B649-17E2-4BCA-8774-664C83A960CB} = {7C1D79F1-87BC-42C1-BD5A-CDE4044AC1BD}
272278
{6312863E-771D-4EFE-9B9D-071A01222E7A} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
273279
{7F7F3B85-7B15-485C-8DC2-BAFD8C086957} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
280+
{F800DC0F-6829-4B64-B96B-B301A512AF2A} = {CD062AB6-244D-402A-8F33-C37DAC5856CC}
274281
EndGlobalSection
275282
GlobalSection(ExtensibilityGlobals) = postSolution
276283
SolutionGuid = {0DCB0756-34FA-4FD0-AE1D-D3F08B5B3A6B}

src/BootstrapBlazor.Server/Extensions/ServicesExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public static IServiceCollection AddBootstrapBlazorServices(this IServiceCollect
5555
// 设置地理位置定位器
5656
services.ConfigureIPLocatorOption(op => op.LocatorFactory = sp => new BaiDuIPLocator());
5757

58+
// 增加 Baidu ORC 服务
59+
services.AddBootstrapBlazorBaiduOcr();
60+
5861
// 增加多语言支持配置信息
5962
services.AddRequestLocalization<IOptionsMonitor<BootstrapBlazorOptions>>((localizerOption, blazorOption) =>
6063
{

src/BootstrapBlazor.Server/appsettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,10 @@
6363
"AppId": "<AppId>",
6464
"ApiKey": "<ApiKey>",
6565
"Secret": "<Secret>"
66+
},
67+
"BaiduOcrOption": {
68+
"AppId": "<AppId>",
69+
"ApiKey": "<ApiKey>",
70+
"Secret": "<Secret>"
6671
}
6772
}

src/BootstrapBlazor.Shared/BootstrapBlazor.Shared.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919

2020
<ItemGroup>
2121
<PackageReference Include="BootstrapBlazor.BaiduSpeech" Version="7.*" />
22+
<PackageReference Include="BootstrapBlazor.BaiduOcr" Version="7.*" />
2223
<PackageReference Include="BootstrapBlazor.BarCode" Version="7.*" />
2324
<PackageReference Include="BootstrapBlazor.Bluetooth" Version="7.*" />
2425
<PackageReference Include="BootstrapBlazor.Chart" Version="7.*" />
2526
<PackageReference Include="BootstrapBlazor.CherryMarkdown" Version="7.*" />
27+
<PackageReference Include="BootstrapBlazor.FileViewer" Version="7.*" />
2628
<PackageReference Include="BootstrapBlazor.FontAwesome" Version="7.*" />
2729
<PackageReference Include="BootstrapBlazor.Markdown" Version="7.*" />
2830
<PackageReference Include="BootstrapBlazor.OnScreenKeyboard" Version="7.*" />
@@ -32,7 +34,6 @@
3234
<PackageReference Include="BootstrapBlazor.TableExport" Version="7.*" />
3335
<PackageReference Include="BootstrapBlazor.Topology" Version="7.*" />
3436
<PackageReference Include="BootstrapBlazor.VideoPlayer" Version="7.*" />
35-
<PackageReference Include="BootstrapBlazor.FileViewer" Version="7.*" />
3637
</ItemGroup>
3738

3839
<ItemGroup>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
@inject IBaiduOcr OcrService
2+
3+
<ButtonUpload TValue="string" OnChange="@OnClickToUpload" BrowserButtonText="VATInvoice" ShowUploadFileList="false"></ButtonUpload>
4+
5+
@if (Invoice != null)
6+
{
7+
<div class="row form-inline g-3 mt-0">
8+
<div class="col-12 col-sm-6">
9+
<Display ShowLabel="true" DisplayText="发票代码" @bind-Value="Invoice.InvoiceCode"></Display>
10+
</div>
11+
<div class="col-12 col-sm-6">
12+
<Display ShowLabel="true" DisplayText="发票号码" Value="Invoice.InvoiceNum"></Display>
13+
</div>
14+
<div class="col-12 col-sm-6">
15+
<Display ShowLabel="true" DisplayText="发票类型" @bind-Value="Invoice.ServiceType"></Display>
16+
</div>
17+
<div class="col-12 col-sm-6">
18+
<Display ShowLabel="true" DisplayText="联次信息" Value="Invoice.SheetNum"></Display>
19+
</div>
20+
<div class="col-12 col-sm-6">
21+
<Display ShowLabel="true" DisplayText="发票种类" @bind-Value="Invoice.InvoiceType"></Display>
22+
</div>
23+
<div class="col-12 col-sm-6">
24+
<Display ShowLabel="true" DisplayText="发票名称" Value="Invoice.InvoiceTypeOrg"></Display>
25+
</div>
26+
<div class="col-12 col-sm-6">
27+
<Display ShowLabel="true" DisplayText="发票标志" @bind-Value="Invoice.InvoiceTag"></Display>
28+
</div>
29+
<div class="col-12 col-sm-6">
30+
<Display ShowLabel="true" DisplayText="开票日期" Value="Invoice.InvoiceDate"></Display>
31+
</div>
32+
<div class="col-12 col-sm-6">
33+
<Display ShowLabel="true" DisplayText="购方名称" @bind-Value="Invoice.PurchaserName"></Display>
34+
</div>
35+
<div class="col-12 col-sm-6">
36+
<Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.PurchaserRegisterNum"></Display>
37+
</div>
38+
<div class="col-12 col-sm-6">
39+
<Display ShowLabel="true" DisplayText="开户行及账号" @bind-Value="Invoice.PurchaserBank"></Display>
40+
</div>
41+
<div class="col-12 col-sm-6">
42+
<Display ShowLabel="true" DisplayText="购方地址" Value="Invoice.PurchaserAddress"></Display>
43+
</div>
44+
<div class="col-12 col-sm-6">
45+
<Display ShowLabel="true" DisplayText="销售方名称" @bind-Value="Invoice.SellerName"></Display>
46+
</div>
47+
<div class="col-12 col-sm-6">
48+
<Display ShowLabel="true" DisplayText="纳税人识别号" Value="Invoice.SellerRegisterNum"></Display>
49+
</div>
50+
<div class="col-12 col-sm-6">
51+
<Display ShowLabel="true" DisplayText="销售方及账号" @bind-Value="Invoice.SellerBank"></Display>
52+
</div>
53+
<div class="col-12 col-sm-6">
54+
<Display ShowLabel="true" DisplayText="销售方地址" Value="Invoice.SellerAddress"></Display>
55+
</div>
56+
<div class="col-12">
57+
<Display ShowLabel="true" DisplayText="备注" @bind-Value="Invoice.Remarks"></Display>
58+
</div>
59+
<div class="col-12 col-sm-6">
60+
<Display ShowLabel="true" DisplayText="价税合计" Value="Invoice.AmountInFiguers"></Display>
61+
</div>
62+
<div class="col-12 col-sm-6">
63+
<Display ShowLabel="true" DisplayText="合计金额" Value="Invoice.TotalAmount"></Display>
64+
</div>
65+
<div class="col-12 col-sm-6">
66+
<Display ShowLabel="true" DisplayText="合计税额" @bind-Value="Invoice.TotalTax"></Display>
67+
</div>
68+
<div class="col-12 col-sm-6">
69+
</div>
70+
@if (Invoice.CommodityName?.Any() ?? false)
71+
{
72+
<div class="col-12 col-sm-6">
73+
<Display ShowLabel="true" DisplayText="税率" Value="Invoice.CommodityName.First().CommodityName"></Display>
74+
</div>
75+
}
76+
@if (Invoice.CommodityTaxRate?.Any() ?? false)
77+
{
78+
<div class="col-12 col-sm-6">
79+
<Display ShowLabel="true" DisplayText="税率" Value="Invoice.CommodityTaxRate.First().CommodityTaxRate"></Display>
80+
</div>
81+
}
82+
</div>
83+
}
84+
85+
@code {
86+
private InvoiceEntity? Invoice { get; set; }
87+
88+
private async Task OnClickToUpload(UploadFile file)
89+
{
90+
if (file.File != null)
91+
{
92+
var payload = await file.GetBytesAsync(file.File.Size);
93+
if (payload?.Any() ?? false)
94+
{
95+
Invoice = await OcrService.CheckVatInvoiceAsync(payload);
96+
StateHasChanged();
97+
}
98+
}
99+
}
100+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
using System;
6+
7+
namespace BootstrapBlazor.Shared.Demos.BaiduOcr;
8+
9+
/// <summary>
10+
/// 增值税发票识别示例
11+
/// </summary>
12+
public partial class BaiduOcrNormal
13+
{
14+
}

src/BootstrapBlazor.Shared/Extensions/MenusLocalizerExtensions.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,6 @@ void AddData(DemoMenuItem item)
532532
Url = "qrcodes"
533533
},
534534
new()
535-
{
536-
Text = Localizer["Recognizer"],
537-
Url = "recognizers"
538-
},
539-
new()
540535
{
541536
Text = Localizer["Search"],
542537
Url = "searchs"
@@ -594,6 +589,12 @@ void AddData(DemoMenuItem item)
594589
Url = "transitions"
595590
},
596591
new()
592+
{
593+
IsNew = true,
594+
Text = Localizer["BaiduOcr"],
595+
Url = "ocr"
596+
},
597+
new()
597598
{
598599
IsNew = true,
599600
Text = Localizer["VideoPlayer"],

src/BootstrapBlazor.Shared/Locales/en.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,16 @@
559559
"NoRenderIntro": "By setting the <code>Editable=false</code> of the <code>IEditorItem</code> instance, the editable pop-up window cannot be edited.",
560560
"EditDialogOption": "EditDialogOption property"
561561
},
562+
"BootstrapBlazor.Shared.Samples.BaiduOcr": {
563+
"Title": "IBaiduOcr",
564+
"SubTitle": "Character recognition",
565+
"BasicUsageTitle": "VAT Invoice",
566+
"BasicUsageIntro": "Call Baidu Ocr interface for text recognition by uploading VAT invoice image",
567+
"BaiduOcrDesc": "本组件通过调用 Baidu AI 平台文字识别接口进行增值税发票文字识别。支持对增值税普票、专票、全电发票(新版全国统一电子发票,专票/普票)、卷票、区块链发票的所有字段进行结构化识别,包括发票基本信息、销售方及购买方信息、商品信息、价税信息等,其中五要素字段的识别准确率超过 99.9%; 同时,支持对增值税卷票的 21 个关键字段进行识别,包括发票类型、发票代码、发票号码、机打号码、机器编号、收款人、销售方名称、销售方纳税人识别号、开票日期、购买方名称、购买方纳税人识别号、项目、单价、数量、金额、税额、合计金额(小写)、合计金额(大写)、校验码、省、市,四要素字段的识别准确率可达95%。上传图片不能超过 4M",
568+
"BaiduOcrIntro": "Usage",
569+
"BaiduOcrStep1": "1. Get <code>IBaiduOcr</code> instance by inject service",
570+
"BaiduOcrStep2": "2. Call <code>IBaiduOcr</code> instance method"
571+
},
562572
"BootstrapBlazor.Shared.Demos.EditDialog.EditDialogNormal": {
563573
"LeftAlignedButton": "Edit popup (left-aligned)",
564574
"RightAlignedButton": "Edit popup (right aligned)"
@@ -4883,7 +4893,8 @@
48834893
"Bluetooth": "Bluetooth & Printer",
48844894
"PdfReader": "PDF Reader",
48854895
"VideoPlayer": "VideoPlayer",
4886-
"FileViewer": "FileViewer"
4896+
"FileViewer": "FileViewer",
4897+
"BaiduOcr": "IBaiduOcr "
48874898
},
48884899
"BootstrapBlazor.Shared.Samples.Table.TablesHeader": {
48894900
"H1": "Header grouping function",

src/BootstrapBlazor.Shared/Locales/zh.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,16 @@
560560
"NoRenderIntro": "通过设置 <code>IEditorItem</code> 实例的 <code>Editable=false</code>, 实现编辑弹窗不可编辑此属性",
561561
"EditDialogOption": "EditDialogOption 属性"
562562
},
563+
"BootstrapBlazor.Shared.Samples.BaiduOcr": {
564+
"Title": "IBaiduOcr 百度文字识别服务",
565+
"SubTitle": "文字识别服务",
566+
"BasicUsageTitle": "增值税发票文字识别",
567+
"BasicUsageIntro": "通过上传增值税发票图片调用百度 Ocr 接口进行文字识别",
568+
"BaiduOcrDesc": "本组件通过调用 Baidu AI 平台文字识别接口进行增值税发票文字识别。支持对增值税普票、专票、全电发票(新版全国统一电子发票,专票/普票)、卷票、区块链发票的所有字段进行结构化识别,包括发票基本信息、销售方及购买方信息、商品信息、价税信息等,其中五要素字段的识别准确率超过 99.9%; 同时,支持对增值税卷票的 21 个关键字段进行识别,包括发票类型、发票代码、发票号码、机打号码、机器编号、收款人、销售方名称、销售方纳税人识别号、开票日期、购买方名称、购买方纳税人识别号、项目、单价、数量、金额、税额、合计金额(小写)、合计金额(大写)、校验码、省、市,四要素字段的识别准确率可达95%。上传图片不能超过 4M",
569+
"BaiduOcrIntro": "使用方法",
570+
"BaiduOcrStep1": "1. 通过注入服务获得 <code>IBaiduOcr</code> 实例",
571+
"BaiduOcrStep2": "2. 调用服务相对应的识别方法即可"
572+
},
563573
"BootstrapBlazor.Shared.Demos.EditDialog.EditDialogNormal": {
564574
"LeftAlignedButton": "编辑弹窗(左对齐)",
565575
"RightAlignedButton": "编辑弹窗(右对齐)"
@@ -4897,7 +4907,8 @@
48974907
"Bluetooth": "蓝牙和打印 Bluetooth & Printer",
48984908
"PdfReader": "PDF阅读器 PDF Reader",
48994909
"VideoPlayer": "视频播放器 VideoPlayer",
4900-
"FileViewer": "文件预览器 FileViewer"
4910+
"FileViewer": "文件预览器 FileViewer",
4911+
"BaiduOcr": "文字识别服务 IBaiduOcr "
49014912
},
49024913
"BootstrapBlazor.Shared.Samples.Table.TablesHeader": {
49034914
"H1": "表头分组功能",
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@page "/ocr"
2+
@inject IStringLocalizer<BaiduOcr> Localizer
3+
4+
<h3>@Localizer["Title"]</h3>
5+
6+
<h4>@Localizer["SubTitle"]</h4>
7+
8+
<Tips class="mt-3">@Localizer["BaiduOcrDesc"]</Tips>
9+
10+
<PackageTips Name="BootstrapBlazor.BaiduOcr" />
11+
12+
<div class="mb-2"><b>@Localizer["BaiduOcrIntro"]</b></div>
13+
14+
<div>@((MarkupString)Localizer["BaiduOcrStep1"].Value)</div>
15+
<div>@((MarkupString)Localizer["BaiduOcrStep2"].Value)</div>
16+
17+
<DemoBlock Title="@Localizer["BasicUsageTitle"]" Introduction="@Localizer["BasicUsageIntro"]" Name="Normal" Demo="typeof(Demos.BaiduOcr.BaiduOcrNormal)" />

0 commit comments

Comments
 (0)