11using Microsoft . AspNetCore . Components ;
22using MudBlazor ;
3- using SkiaSharp ;
43using ZXing ;
5- using ZXing . QrCode ;
6- using ZXing . SkiaSharp ;
4+ using ZXing . Common ;
75
86namespace MudExtensions
97{
108 public partial class MudQrCode : MudComponentBase
119 {
10+ private static readonly Writer Encoder = new MultiFormatWriter ( ) ;
11+
1212 [ Parameter ]
13- public string Value { get ; set ; }
13+ public BarcodeFormat BarcodeFormat { get ; set ; } = BarcodeFormat . QR_CODE ;
1414
15+ /// <summary>
16+ /// If true, it goes to specified href when click.
17+ /// </summary>
1518 [ Parameter ]
16- public EventCallback < string > ValueChanged { get ; set ; }
19+ public bool Clickable { get ; set ; }
1720
1821 [ Parameter ]
19- public BarcodeFormat BarcodeFormat { get ; set ; } = BarcodeFormat . QR_CODE ;
22+ public string ErrorText { get ; set ; }
2023
2124 [ Parameter ]
2225 public int Width { get ; set ; } = 200 ;
2326
2427 [ Parameter ]
2528 public int Height { get ; set ; } = 200 ;
2629
27- /// <summary>
28- /// If true, it goes to specified href when click.
29- /// </summary>
30- [ Parameter ]
31- public bool Clickable { get ; set ; }
32-
3330 [ Parameter ]
3431 public string Target { get ; set ; } = "_blank" ;
3532
3633 [ Parameter ]
37- public string ErrorText { get ; set ; }
34+ public string Value { get ; set ; }
3835
39- /// <summary>
40- /// If true, no text show on barcode format.
41- /// </summary>
4236 [ Parameter ]
43- public bool PureBarcode { get ; set ; }
37+ public EventCallback < string > ValueChanged { get ; set ; }
4438
45- protected byte [ ] GetQrCode ( )
39+ protected QRCodeResult GetCode ( )
4640 {
4741 if ( string . IsNullOrEmpty ( Value ) )
4842 {
@@ -51,25 +45,29 @@ protected byte[] GetQrCode()
5145
5246 try
5347 {
54- BarcodeWriter writer = new BarcodeWriter
48+ var width = Width ;
49+ var height = Height ;
50+
51+ if ( BarcodeFormat == BarcodeFormat . All_1D )
5552 {
56- Format = BarcodeFormat ,
57- Options = new QrCodeEncodingOptions
53+ }
54+ else
55+ {
56+ if ( width > height )
57+ {
58+ width = height ;
59+ }
60+ else
5861 {
59- Width = Width ,
60- Height = Height ,
61- PureBarcode = PureBarcode ,
62+ height = width ;
6263 }
63- } ;
64+ }
6465
65- var qrCodeImage = writer . Write ( Value ) ;
66+ var matrix = Encoder . encode ( Value , BarcodeFormat , 0 , 0 ) ;
6667
67- using ( var stream = new MemoryStream ( ) )
68- {
69- qrCodeImage . Encode ( stream , SKEncodedImageFormat . Png , 100 ) ;
70- ErrorText = null ;
71- return stream . ToArray ( ) ;
72- }
68+ var moduleSizeX = width / matrix . Width ;
69+ var moduleSizeY = height / matrix . Height ;
70+ return new QRCodeResult ( matrix , moduleSizeX , moduleSizeY ) ;
7371 }
7472 catch ( Exception ex )
7573 {
@@ -79,4 +77,4 @@ protected byte[] GetQrCode()
7977 }
8078
8179 }
82- }
80+ }
0 commit comments