|
| 1 | +using Microsoft.AspNetCore.Components; |
| 2 | +using MudBlazor; |
| 3 | +using SkiaSharp; |
| 4 | +using ZXing; |
| 5 | +using ZXing.QrCode; |
| 6 | +using ZXing.SkiaSharp; |
| 7 | + |
| 8 | +namespace MudExtensions |
| 9 | +{ |
| 10 | + public partial class MudQrCode : MudComponentBase |
| 11 | + { |
| 12 | + [Parameter] |
| 13 | + public string Value { get; set; } |
| 14 | + |
| 15 | + [Parameter] |
| 16 | + public EventCallback<string> ValueChanged { get; set; } |
| 17 | + |
| 18 | + [Parameter] |
| 19 | + public BarcodeFormat BarcodeFormat { get; set; } = BarcodeFormat.QR_CODE; |
| 20 | + |
| 21 | + [Parameter] |
| 22 | + public int Width { get; set; } = 200; |
| 23 | + |
| 24 | + [Parameter] |
| 25 | + public int Height { get; set; } = 200; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// If true, it goes to specified href when click. |
| 29 | + /// </summary> |
| 30 | + [Parameter] |
| 31 | + public bool Clickable { get; set; } |
| 32 | + |
| 33 | + [Parameter] |
| 34 | + public string Target { get; set; } = "_blank"; |
| 35 | + |
| 36 | + [Parameter] |
| 37 | + public string ErrorText { get; set; } |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// If true, no text show on barcode format. |
| 41 | + /// </summary> |
| 42 | + [Parameter] |
| 43 | + public bool PureBarcode { get; set; } |
| 44 | + |
| 45 | + protected byte[] GetQrCode() |
| 46 | + { |
| 47 | + if (string.IsNullOrEmpty(Value)) |
| 48 | + { |
| 49 | + return null; |
| 50 | + } |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + BarcodeWriter writer = new BarcodeWriter |
| 55 | + { |
| 56 | + Format = BarcodeFormat, |
| 57 | + Options = new QrCodeEncodingOptions |
| 58 | + { |
| 59 | + Width = Width, |
| 60 | + Height = Height, |
| 61 | + PureBarcode = PureBarcode, |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + var qrCodeImage = writer.Write(Value); |
| 66 | + |
| 67 | + using (var stream = new MemoryStream()) |
| 68 | + { |
| 69 | + qrCodeImage.Encode(stream, SKEncodedImageFormat.Png, 100); |
| 70 | + ErrorText = null; |
| 71 | + return stream.ToArray(); |
| 72 | + } |
| 73 | + } |
| 74 | + catch (Exception ex) |
| 75 | + { |
| 76 | + ErrorText = ex.Message; |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | +} |
0 commit comments