|
| 1 | +// |
| 2 | +// QR code generator library (.NET) |
| 3 | +// https://github.com/manuelbl/QrCodeGenerator |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Manuel Bleichenbacher |
| 6 | +// Licensed under MIT License |
| 7 | +// https://opensource.org/licenses/MIT |
| 8 | +// |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.ComponentModel; |
| 12 | +using System.Runtime.CompilerServices; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Windows.Media.Imaging; |
| 15 | + |
| 16 | +namespace Net.Codecrete.QrCodeGenerator.Analyzer; |
| 17 | + |
| 18 | +public enum HighlightKind { None, Blocks, HorizontalStreaks, VerticalStreaks } |
| 19 | + |
| 20 | +public sealed class MainWindowViewModel : INotifyPropertyChanged |
| 21 | +{ |
| 22 | + private readonly QrCodeBuilder.DebugInfo _debugInfo = new QrCodeBuilder.DebugInfo(); |
| 23 | + |
| 24 | + private string _text = "QR code text"; |
| 25 | + private int _borderWidth = 4; |
| 26 | + private QrCode.Ecc _errorCorrection = QrCode.Ecc.Medium; |
| 27 | + private int _dataMaskPattern = -1; |
| 28 | + private QrCodeBuilder.PenaltyInfo _penaltyDetails; |
| 29 | + private ImageSource? _qrCodeImage; |
| 30 | + private int _selectedDataMaskPattern = -1; |
| 31 | + private QrCode? _currentQrCode; |
| 32 | + private HighlightKind _highlight = HighlightKind.None; |
| 33 | + |
| 34 | + public MainWindowViewModel() |
| 35 | + { |
| 36 | + QrCodeBuilder.DebugAccess = _debugInfo; |
| 37 | + ErrorCorrectionLevels = |
| 38 | + [ |
| 39 | + new Tuple<string, QrCode.Ecc>("Low", QrCode.Ecc.Low), |
| 40 | + new Tuple<string, QrCode.Ecc>("Medium", QrCode.Ecc.Medium), |
| 41 | + new Tuple<string, QrCode.Ecc>("Quartile", QrCode.Ecc.Quartile), |
| 42 | + new Tuple<string, QrCode.Ecc>("High", QrCode.Ecc.High) |
| 43 | + ]; |
| 44 | + DataMasks = |
| 45 | + [ |
| 46 | + new Tuple<string, int>("Auto", -1), |
| 47 | + new Tuple<string, int>("Mask 0", 0), |
| 48 | + new Tuple<string, int>("Mask 1", 1), |
| 49 | + new Tuple<string, int>("Mask 2", 2), |
| 50 | + new Tuple<string, int>("Mask 3", 3), |
| 51 | + new Tuple<string, int>("Mask 4", 4), |
| 52 | + new Tuple<string, int>("Mask 5", 5), |
| 53 | + new Tuple<string, int>("Mask 6", 6), |
| 54 | + new Tuple<string, int>("Mask 7", 7) |
| 55 | + ]; |
| 56 | + UpdateQrCode(); |
| 57 | + } |
| 58 | + |
| 59 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 60 | + |
| 61 | + public string Text |
| 62 | + { |
| 63 | + get => _text; |
| 64 | + set |
| 65 | + { |
| 66 | + if (_text == value) return; |
| 67 | + _text = value; |
| 68 | + UpdateQrCode(); |
| 69 | + OnPropertyChanged(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public int BorderWidth |
| 74 | + { |
| 75 | + get => _borderWidth; |
| 76 | + set |
| 77 | + { |
| 78 | + if (_borderWidth == value) return; |
| 79 | + _borderWidth = value; |
| 80 | + UpdateQrCode(); |
| 81 | + OnPropertyChanged(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public Tuple<string, QrCode.Ecc>[] ErrorCorrectionLevels { get; } |
| 86 | + |
| 87 | + public Tuple<string, int>[] DataMasks { get; } |
| 88 | + |
| 89 | + public QrCode.Ecc ErrorCorrection |
| 90 | + { |
| 91 | + get => _errorCorrection; |
| 92 | + set |
| 93 | + { |
| 94 | + if (_errorCorrection == value) return; |
| 95 | + _errorCorrection = value; |
| 96 | + UpdateQrCode(); |
| 97 | + OnPropertyChanged(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + public int DataMaskPattern |
| 102 | + { |
| 103 | + get => _dataMaskPattern; |
| 104 | + set |
| 105 | + { |
| 106 | + if (_dataMaskPattern == value) return; |
| 107 | + _dataMaskPattern = Math.Clamp(value, -1, 7); |
| 108 | + UpdateQrCode(); |
| 109 | + OnPropertyChanged(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public int SelectedDataMaskPattern |
| 114 | + { |
| 115 | + get => _selectedDataMaskPattern; |
| 116 | + private set |
| 117 | + { |
| 118 | + if (_selectedDataMaskPattern == value) return; |
| 119 | + _selectedDataMaskPattern = value; |
| 120 | + OnPropertyChanged(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + public QrCodeBuilder.PenaltyInfo PenaltyDetails |
| 125 | + { |
| 126 | + get => _penaltyDetails; |
| 127 | + private set |
| 128 | + { |
| 129 | + _penaltyDetails = value; |
| 130 | + OnPropertyChanged(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public ImageSource? QrCodeImage |
| 135 | + { |
| 136 | + get => _qrCodeImage; |
| 137 | + private set |
| 138 | + { |
| 139 | + _qrCodeImage = value; |
| 140 | + OnPropertyChanged(); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public HighlightKind Highlight |
| 145 | + { |
| 146 | + get => _highlight; |
| 147 | + set |
| 148 | + { |
| 149 | + if (_highlight == value) return; |
| 150 | + _highlight = value; |
| 151 | + RenderQrCodeImage(); |
| 152 | + OnPropertyChanged(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public BitmapSource CreateClipboardBitmap() |
| 157 | + { |
| 158 | + var qrCode = QrCode.EncodeText(_text, _errorCorrection); |
| 159 | + return QrCodeDrawing.CreateBitmapImage(qrCode, 20, _borderWidth); |
| 160 | + } |
| 161 | + |
| 162 | + private void UpdateQrCode() |
| 163 | + { |
| 164 | + _debugInfo.ForcedDataMask = _dataMaskPattern; |
| 165 | + var qrCode = QrCode.EncodeText(_text, _errorCorrection); |
| 166 | + _currentQrCode = qrCode; |
| 167 | + SelectedDataMaskPattern = qrCode.Mask; |
| 168 | + PenaltyDetails = _debugInfo.Penalties[qrCode.Mask]; |
| 169 | + RenderQrCodeImage(); |
| 170 | + } |
| 171 | + |
| 172 | + private void RenderQrCodeImage() |
| 173 | + { |
| 174 | + if (_currentQrCode == null) return; |
| 175 | + var highlights = _highlight switch |
| 176 | + { |
| 177 | + HighlightKind.Blocks => Penalty.GetBlocks(_currentQrCode), |
| 178 | + HighlightKind.HorizontalStreaks => Penalty.GetHorizontalStreaks(_currentQrCode), |
| 179 | + HighlightKind.VerticalStreaks => Penalty.GetVerticalStreaks(_currentQrCode), |
| 180 | + _ => null |
| 181 | + }; |
| 182 | + QrCodeImage = QrCodeDrawing.CreateDrawing(_currentQrCode, 192, _borderWidth, highlights); |
| 183 | + } |
| 184 | + |
| 185 | + private void OnPropertyChanged([CallerMemberName] string? propertyName = null) |
| 186 | + { |
| 187 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 188 | + } |
| 189 | +} |
0 commit comments