Skip to content

Commit dae9514

Browse files
authored
MudBarcode (#183)
1 parent 7d64e85 commit dae9514

File tree

13 files changed

+155
-56
lines changed

13 files changed

+155
-56
lines changed

CodeBeam.MudBlazor.Extensions.MudQrCode/Components/QrCode/MudQrCode.razor.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,6 @@ protected QRCodeResult GetCode()
4848
var width = Width;
4949
var height = Height;
5050

51-
if (BarcodeFormat == BarcodeFormat.All_1D)
52-
{
53-
}
54-
else
55-
{
56-
if (width > height)
57-
{
58-
width = height;
59-
}
60-
else
61-
{
62-
height = width;
63-
}
64-
}
65-
6651
var matrix = Encoder.encode(Value, BarcodeFormat, 0, 0);
6752

6853
var moduleSizeX = width / matrix.Width;

CodeBeam.MudBlazor.Extensions/CodeBeam.MudBlazor.Extensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<PackageReference Include="BuildBundlerMinifier" Version="3.2.449" />
2626
<PackageReference Include="CsvHelper" Version="30.0.1" />
2727
<PackageReference Include="MudBlazor" Version="6.2.1" />
28+
<PackageReference Include="ZXing.Net" Version="0.16.9" />
2829
</ItemGroup>
2930

3031
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using ZXing.Common;
7+
8+
namespace MudExtensions
9+
{
10+
public class BarcodeResult
11+
{
12+
private readonly BitMatrix bitMatrix;
13+
14+
public BarcodeResult(BitMatrix bitMatrix, int moduleSizeX, int moduleSizeY)
15+
{
16+
this.bitMatrix = bitMatrix ?? throw new ArgumentNullException(nameof(bitMatrix));
17+
ModuleSizeX = moduleSizeX;
18+
ModuleSizeY = moduleSizeY;
19+
}
20+
21+
public int Columns => bitMatrix.Width;
22+
public int ModuleSizeX { get; }
23+
public int ModuleSizeY { get; }
24+
public int Rows => bitMatrix.Height;
25+
26+
public bool this[int x, int y] => bitMatrix[x, y];
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@namespace MudExtensions
2+
@inherits MudComponentBase
3+
4+
<a href="@(Clickable ? Value : null)" target="@Target">
5+
@{
6+
var content = GetCode();
7+
}
8+
@if (content != null)
9+
{
10+
<svg width="@Width" height="@Height">
11+
@for (int y = 0; y < content.Rows; y++)
12+
{
13+
@for(int x = 0; x < content.Columns; x++)
14+
{
15+
@if(content[x,y])
16+
{
17+
<rect width="@content.ModuleSizeX" height="@content.ModuleSizeY" style="fill:black; stroke-width:1px; stroke:black"
18+
x="@(x * content.ModuleSizeX)" y="@(y * content.ModuleSizeY)" />
19+
}
20+
}
21+
}
22+
</svg>
23+
}
24+
else
25+
{
26+
<MudImage Height="Height" Width="Width" Class="@Class" Style="@Style" />
27+
}
28+
</a>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.AspNetCore.Components;
2+
using MudBlazor;
3+
using ZXing;
4+
using ZXing.Common;
5+
6+
namespace MudExtensions
7+
{
8+
public partial class MudBarcode : MudComponentBase
9+
{
10+
private static readonly Writer Encoder = new MultiFormatWriter();
11+
12+
[Parameter]
13+
public BarcodeFormat BarcodeFormat { get; set; } = BarcodeFormat.QR_CODE;
14+
15+
/// <summary>
16+
/// If true, it goes to specified href when click.
17+
/// </summary>
18+
[Parameter]
19+
public bool Clickable { get; set; }
20+
21+
[Parameter]
22+
public string ErrorText { get; set; }
23+
24+
[Parameter]
25+
public int Width { get; set; } = 200;
26+
27+
[Parameter]
28+
public int Height { get; set; } = 200;
29+
30+
[Parameter]
31+
public string Target { get; set; } = "_blank";
32+
33+
[Parameter]
34+
public string Value { get; set; }
35+
36+
[Parameter]
37+
public EventCallback<string> ValueChanged { get; set; }
38+
39+
protected BarcodeResult GetCode()
40+
{
41+
if (string.IsNullOrEmpty(Value))
42+
{
43+
return null;
44+
}
45+
46+
try
47+
{
48+
var width = Width;
49+
var height = Height;
50+
51+
var matrix = Encoder.encode(Value, BarcodeFormat, 0, 0);
52+
53+
var moduleSizeX = width / matrix.Width;
54+
var moduleSizeY = height / matrix.Height;
55+
var result = new BarcodeResult(matrix, moduleSizeX, moduleSizeY);
56+
ErrorText = null;
57+
return result;
58+
}
59+
catch (Exception ex)
60+
{
61+
ErrorText = ex.Message;
62+
return null;
63+
}
64+
}
65+
66+
}
67+
}

CodeBeamMudExtensions.sln

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeBeam.MudBlazor.Extensio
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CodeBeam.MudBlazor.Extensions.UnitTests.Viewer", "CodeBeam.MudBlazor.Extensions.UnitTests.Viewer\CodeBeam.MudBlazor.Extensions.UnitTests.Viewer.csproj", "{590CCBEC-3D84-42C8-A925-C0CD2135268C}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeBeam.MudBlazor.Extensions.MudQrCode", "CodeBeam.MudBlazor.Extensions.MudQrCode\CodeBeam.MudBlazor.Extensions.MudQrCode.csproj", "{02810B56-8DDC-41A2-B97D-59768B396B67}"
19-
EndProject
2018
Global
2119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2220
Debug|Any CPU = Debug|Any CPU
@@ -47,10 +45,6 @@ Global
4745
{590CCBEC-3D84-42C8-A925-C0CD2135268C}.Debug|Any CPU.Build.0 = Debug|Any CPU
4846
{590CCBEC-3D84-42C8-A925-C0CD2135268C}.Release|Any CPU.ActiveCfg = Release|Any CPU
4947
{590CCBEC-3D84-42C8-A925-C0CD2135268C}.Release|Any CPU.Build.0 = Release|Any CPU
50-
{02810B56-8DDC-41A2-B97D-59768B396B67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51-
{02810B56-8DDC-41A2-B97D-59768B396B67}.Debug|Any CPU.Build.0 = Debug|Any CPU
52-
{02810B56-8DDC-41A2-B97D-59768B396B67}.Release|Any CPU.ActiveCfg = Release|Any CPU
53-
{02810B56-8DDC-41A2-B97D-59768B396B67}.Release|Any CPU.Build.0 = Release|Any CPU
5448
EndGlobalSection
5549
GlobalSection(SolutionProperties) = preSolution
5650
HideSolutionNode = FALSE

ComponentViewer.Docs/ComponentViewer.Docs.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
</ItemGroup>
1818

1919
<ItemGroup>
20-
<ProjectReference Include="..\CodeBeam.MudBlazor.Extensions.MudQrCode\CodeBeam.MudBlazor.Extensions.MudQrCode.csproj" />
2120
<ProjectReference Include="..\CodeBeam.MudBlazor.Extensions\CodeBeam.MudBlazor.Extensions.csproj" />
2221
</ItemGroup>
2322

ComponentViewer.Docs/Pages/Components/ApiPage.razor

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,27 @@
2525
</MudTable>
2626
</ExampleCard>
2727

28+
<ExampleCard Title="Api - MudBarcode" HasExpansionPanel="true">
29+
<MudTable Items="@(typeof(MudBarcode).GetProperties().Where(x => x.Name != "FieldId" && x.Name != "UserAttributes").OrderBy(x => x.Name).ToList())">
30+
<HeaderContent>
31+
<MudTh>Name</MudTh>
32+
<MudTh>Type</MudTh>
33+
<MudTh>Default</MudTh>
34+
</HeaderContent>
35+
<RowTemplate>
36+
<MudTd DataLabel="Name">@context.Name</MudTd>
37+
<MudTd DataLabel="Type">@context.PropertyType.Name</MudTd>
38+
<MudTd DataLabel="Default">
39+
@if (true)
40+
{
41+
MudBarcode instance = new();
42+
<MudText Typo="Typo.body2">@(context.GetValue(instance)?.ToString() ?? "null")</MudText>
43+
}
44+
</MudTd>
45+
</RowTemplate>
46+
</MudTable>
47+
</ExampleCard>
48+
2849
<ExampleCard Title="Api - MudChipField" HasExpansionPanel="true">
2950
<MudTable Items="@(typeof(MudChipField<string>).GetProperties().Where(x => x.Name != "FieldId" && x.Name != "UserAttributes").OrderBy(x => x.Name).ToList())">
3051
<HeaderContent>
@@ -351,27 +372,6 @@
351372
</MudTable>
352373
</ExampleCard>
353374

354-
<ExampleCard Title="Api - MudQrCode" HasExpansionPanel="true">
355-
<MudTable Items="@(typeof(MudQrCode).GetProperties().Where(x => x.Name != "FieldId" && x.Name != "UserAttributes").OrderBy(x => x.Name).ToList())">
356-
<HeaderContent>
357-
<MudTh>Name</MudTh>
358-
<MudTh>Type</MudTh>
359-
<MudTh>Default</MudTh>
360-
</HeaderContent>
361-
<RowTemplate>
362-
<MudTd DataLabel="Name">@context.Name</MudTd>
363-
<MudTd DataLabel="Type">@context.PropertyType.Name</MudTd>
364-
<MudTd DataLabel="Default">
365-
@if (true)
366-
{
367-
MudQrCode instance = new();
368-
<MudText Typo="Typo.body2">@(context.GetValue(instance)?.ToString() ?? "null")</MudText>
369-
}
370-
</MudTd>
371-
</RowTemplate>
372-
</MudTable>
373-
</ExampleCard>
374-
375375
<ExampleCard Title="Api - MudScrollbar" HasExpansionPanel="true">
376376
<MudTable Items="@(typeof(MudScrollbar).GetProperties().Where(x => x.Name != "FieldId" && x.Name != "UserAttributes").OrderBy(x => x.Name).ToList())">
377377
<HeaderContent>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
@page "/mudqrcode"
1+
@page "/mudbarcode"
22
@using ComponentViewer.Docs.Pages.Examples
33

4-
<ExamplePage Title="MudQrCode">
5-
<ExampleCard ExampleName="QrCodeExample1" Title="Usage" Description="QrCode shows the QR with defined value.">
6-
<QrCodeExample1 />
4+
<ExamplePage Title="MudBarcode">
5+
<ExampleCard ExampleName="BarcodeExample1" Title="Usage" Description="Barcode shows different bar codes like QR code with defined value.">
6+
<BarcodeExample1 />
77
</ExampleCard>
88
</ExamplePage>

ComponentViewer.Docs/Pages/Examples/QrCodeExample1.razor renamed to ComponentViewer.Docs/Pages/Examples/BarcodeExample1.razor

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<MudGrid>
44
<MudItem xs="12" sm="8" Class="d-flex justify-center">
5-
<MudQrCode @ref="_qrCode" @bind-Value="_qrValue" Width="_width" Height="_height" BarcodeFormat="_barcodeFormat" Clickable="_clickable" />
5+
<MudBarcode @ref="_barcode" @bind-Value="_qrValue" Width="_width" Height="_height" BarcodeFormat="_barcodeFormat" Clickable="_clickable" />
66
</MudItem>
77

88
<MudItem xs="12" sm="4">
@@ -11,14 +11,14 @@
1111
<MudNumericField @bind-Value="_width" Label="Width" Variant="Variant.Outlined" Margin="Margin.Dense" />
1212
<MudNumericField @bind-Value="_height" Label="Height" Variant="Variant.Outlined" Margin="Margin.Dense" />
1313
<MudSelectExtended ItemCollection="@(Enum.GetValues<BarcodeFormat>())" @bind-Value="_barcodeFormat" Label="Barcode Format" Variant="Variant.Outlined" Margin="Margin.Dense" Dense="true" />
14-
<MudText Color="Color.Error">@(_qrCode.ErrorText == null ? null : "Error: " + _qrCode.ErrorText)</MudText>
14+
<MudText Color="Color.Error">@(_barcode.ErrorText == null ? null : "Error: " + _barcode.ErrorText)</MudText>
1515
<MudSwitchM3 @bind-Checked="_clickable" Color="Color.Secondary" Label="Clickable" />
1616
</MudStack>
1717
</MudItem>
1818
</MudGrid>
1919

2020
@code {
21-
MudQrCode _qrCode;
21+
MudBarcode _barcode;
2222
string _qrValue;
2323
int _width = 200;
2424
int _height = 200;

0 commit comments

Comments
 (0)