|
| 1 | +using System; |
| 2 | +using static QRCoder.QRCodeGenerator; |
| 3 | + |
| 4 | +namespace QRCoder; |
| 5 | + |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Represents a QR code generator that outputs QR codes as bitmap byte arrays. |
| 9 | +/// </summary> |
| 10 | +// ReSharper disable once InconsistentNaming |
| 11 | +public class BitmapByteQRCode : AbstractQRCode, IDisposable |
| 12 | +{ |
| 13 | + private static readonly byte[] _bitmapHeaderPart1 = new byte[] { 0x42, 0x4D }; |
| 14 | + private static readonly byte[] _bitmapHeaderPart2 = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00 }; |
| 15 | + private static readonly byte[] _bitmapHeaderPartEnd = new byte[] { 0x01, 0x00, 0x18, 0x00 }; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="BitmapByteQRCode"/> class. |
| 19 | + /// Constructor without parameters to be used in COM objects connections. |
| 20 | + /// </summary> |
| 21 | + public BitmapByteQRCode() { } |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// Initializes a new instance of the <see cref="BitmapByteQRCode"/> class with the specified <see cref="QRCodeData"/>. |
| 25 | + /// </summary> |
| 26 | + /// <param name="data"><see cref="QRCodeData"/> generated by the QRCodeGenerator.</param> |
| 27 | + public BitmapByteQRCode(QRCodeData data) : base(data) { } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Returns the QR code graphic as a bitmap byte array. |
| 31 | + /// </summary> |
| 32 | + /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param> |
| 33 | + /// <returns>Returns the QR code graphic as a bitmap byte array.</returns> |
| 34 | + public byte[] GetGraphic(int pixelsPerModule) |
| 35 | + => GetGraphic(pixelsPerModule, new byte[] { 0x00, 0x00, 0x00 }, new byte[] { 0xFF, 0xFF, 0xFF }); |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Returns the QR code graphic as a bitmap byte array. |
| 39 | + /// </summary> |
| 40 | + /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param> |
| 41 | + /// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param> |
| 42 | + /// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param> |
| 43 | + /// <returns>Returns the QR code graphic as a bitmap byte array.</returns> |
| 44 | + public byte[] GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex) |
| 45 | + => GetGraphic(pixelsPerModule, HexColorToByteArray(darkColorHtmlHex), HexColorToByteArray(lightColorHtmlHex)); |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Returns the QR code graphic as a bitmap byte array. |
| 49 | + /// </summary> |
| 50 | + /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param> |
| 51 | + /// <param name="darkColorRgb">The color of the dark modules as an RGB byte array.</param> |
| 52 | + /// <param name="lightColorRgb">The color of the light modules as an RGB byte array.</param> |
| 53 | + /// <returns>Returns the QR code graphic as a bitmap byte array.</returns> |
| 54 | + public byte[] GetGraphic(int pixelsPerModule, byte[] darkColorRgb, byte[] lightColorRgb) |
| 55 | + { |
| 56 | + var sideLength = QrCodeData.ModuleMatrix.Count * pixelsPerModule; |
| 57 | + |
| 58 | + // Pre-calculate color/module bytes |
| 59 | + byte[] moduleDark = new byte[pixelsPerModule * 3]; |
| 60 | + byte[] moduleLight = new byte[pixelsPerModule * 3]; |
| 61 | + for (int i = 0; i < pixelsPerModule * 3; i += 3) |
| 62 | + { |
| 63 | + moduleDark[i] = darkColorRgb[2]; |
| 64 | + moduleDark[i + 1] = darkColorRgb[1]; |
| 65 | + moduleDark[i + 2] = darkColorRgb[0]; |
| 66 | + moduleLight[i] = lightColorRgb[2]; |
| 67 | + moduleLight[i + 1] = lightColorRgb[1]; |
| 68 | + moduleLight[i + 2] = lightColorRgb[0]; |
| 69 | + } |
| 70 | + |
| 71 | + // Pre-calculate padding bytes |
| 72 | + var paddingLen = sideLength % 4; |
| 73 | + |
| 74 | + // Calculate filesize (header + pixel data + padding) |
| 75 | + var fileSize = 54 + (3 * (sideLength * sideLength)) + (sideLength * paddingLen); |
| 76 | + |
| 77 | + // Bitmap container |
| 78 | + byte[] bmp = new byte[fileSize]; |
| 79 | + int ix = 0; |
| 80 | + |
| 81 | + // Header part 1 |
| 82 | + Array.Copy(_bitmapHeaderPart1, 0, bmp, ix, _bitmapHeaderPart1.Length); |
| 83 | + ix += _bitmapHeaderPart1.Length; |
| 84 | + |
| 85 | + // Filesize |
| 86 | + CopyIntAs4ByteToArray(fileSize, ix, bmp); |
| 87 | + ix += 4; |
| 88 | + |
| 89 | + // Header part 2 |
| 90 | + Array.Copy(_bitmapHeaderPart2, 0, bmp, ix, _bitmapHeaderPart2.Length); |
| 91 | + ix += _bitmapHeaderPart2.Length; |
| 92 | + |
| 93 | + // Width |
| 94 | + CopyIntAs4ByteToArray(sideLength, ix, bmp); |
| 95 | + ix += 4; |
| 96 | + // Height |
| 97 | + CopyIntAs4ByteToArray(sideLength, ix, bmp); |
| 98 | + ix += 4; |
| 99 | + |
| 100 | + // Header end |
| 101 | + Array.Copy(_bitmapHeaderPartEnd, 0, bmp, ix, _bitmapHeaderPartEnd.Length); |
| 102 | + ix += _bitmapHeaderPartEnd.Length; |
| 103 | + |
| 104 | + // Add header null-bytes |
| 105 | + ix += 24; |
| 106 | + |
| 107 | + |
| 108 | + // Draw qr code |
| 109 | + for (var x = sideLength - 1; x >= 0; x -= pixelsPerModule) |
| 110 | + { |
| 111 | + var modMatrixX = (x + pixelsPerModule) / pixelsPerModule - 1; |
| 112 | + |
| 113 | + // Write data for first pixel of pixelsPerModule |
| 114 | + int posStartFirstPx = ix; |
| 115 | + for (var y = 0; y < sideLength; y += pixelsPerModule) |
| 116 | + { |
| 117 | + var module = QrCodeData.ModuleMatrix[modMatrixX][(y + pixelsPerModule) / pixelsPerModule - 1]; |
| 118 | + Array.Copy(module ? moduleDark : moduleLight, 0, bmp, ix, moduleDark.Length); |
| 119 | + ix += moduleDark.Length; |
| 120 | + } |
| 121 | + // Add padding (to make line length a multiple of 4) |
| 122 | + ix += paddingLen; |
| 123 | + int lenFirstPx = ix - posStartFirstPx; |
| 124 | + |
| 125 | + // Re-write (copy) first pixel (pixelsPerModule - 1) times |
| 126 | + for (int pm = 0; pm < (pixelsPerModule - 1); pm++) |
| 127 | + { |
| 128 | + // Draw pixels |
| 129 | + Array.Copy(bmp, posStartFirstPx, bmp, ix, lenFirstPx); |
| 130 | + ix += lenFirstPx; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return bmp; |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + /// <summary> |
| 139 | + /// Converts a hex color string to a byte array. |
| 140 | + /// </summary> |
| 141 | + /// <param name="colorString">The hex color string to convert.</param> |
| 142 | + /// <returns>Returns the color as a byte array.</returns> |
| 143 | + private byte[] HexColorToByteArray(string colorString) |
| 144 | + { |
| 145 | + if (colorString.StartsWith("#")) |
| 146 | + colorString = colorString.Substring(1); |
| 147 | + byte[] byteColor = new byte[colorString.Length / 2]; |
| 148 | + for (int i = 0; i < byteColor.Length; i++) |
| 149 | + byteColor[i] = byte.Parse(colorString.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture); |
| 150 | + return byteColor; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + /// <summary> |
| 155 | + /// Converts an integer to a 4 bytes and writes them to a byte array at given position |
| 156 | + /// </summary> |
| 157 | + /// <param name="inp">The integer to convert.</param> |
| 158 | + /// <param name="destinationIndex">Index of destinationArray where the converted bytes are written to</param> |
| 159 | + /// <param name="destinationArray">Destination byte array that receives the bytes</param> |
| 160 | + private void CopyIntAs4ByteToArray(int inp, int destinationIndex, byte[] destinationArray) |
| 161 | + { |
| 162 | + unchecked |
| 163 | + { |
| 164 | + destinationArray[destinationIndex + 3] = (byte)(inp >> 24); |
| 165 | + destinationArray[destinationIndex + 2] = (byte)(inp >> 16); |
| 166 | + destinationArray[destinationIndex + 1] = (byte)(inp >> 8); |
| 167 | + destinationArray[destinationIndex + 0] = (byte)(inp); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | +/// <summary> |
| 174 | +/// Provides helper functions for creating bitmap byte array QR codes. |
| 175 | +/// </summary> |
| 176 | +public static class BitmapByteQRCodeHelper |
| 177 | +{ |
| 178 | + /// <summary> |
| 179 | + /// Creates a bitmap byte array QR code with a single function call. |
| 180 | + /// </summary> |
| 181 | + /// <param name="plainText">The text or payload to be encoded inside the QR code.</param> |
| 182 | + /// <param name="pixelsPerModule">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param> |
| 183 | + /// <param name="darkColorHtmlHex">The color of the dark modules in HTML hex format.</param> |
| 184 | + /// <param name="lightColorHtmlHex">The color of the light modules in HTML hex format.</param> |
| 185 | + /// <param name="eccLevel">The level of error correction data.</param> |
| 186 | + /// <param name="forceUtf8">Specifies whether the generator should be forced to work in UTF-8 mode.</param> |
| 187 | + /// <param name="utf8BOM">Specifies whether the byte-order-mark should be used.</param> |
| 188 | + /// <param name="eciMode">Specifies which ECI mode should be used.</param> |
| 189 | + /// <param name="requestedVersion">Sets the fixed QR code target version.</param> |
| 190 | + /// <returns>Returns the QR code graphic as a bitmap byte array.</returns> |
| 191 | + public static byte[] GetQRCode(string plainText, int pixelsPerModule, string darkColorHtmlHex, |
| 192 | + string lightColorHtmlHex, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, |
| 193 | + EciMode eciMode = EciMode.Default, int requestedVersion = -1) |
| 194 | + { |
| 195 | + using var qrGenerator = new QRCodeGenerator(); |
| 196 | + using var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, |
| 197 | + requestedVersion); |
| 198 | + using var qrCode = new BitmapByteQRCode(qrCodeData); |
| 199 | + return qrCode.GetGraphic(pixelsPerModule, darkColorHtmlHex, lightColorHtmlHex); |
| 200 | + } |
| 201 | + |
| 202 | + /// <summary> |
| 203 | + /// Creates a bitmap byte array QR code with a single function call. |
| 204 | + /// </summary> |
| 205 | + /// <param name="txt">The text or payload to be encoded inside the QR code.</param> |
| 206 | + /// <param name="eccLevel">The level of error correction data.</param> |
| 207 | + /// <param name="size">The number of pixels each dark/light module of the QR code will occupy in the final QR code image.</param> |
| 208 | + /// <returns>Returns the QR code graphic as a bitmap byte array.</returns> |
| 209 | + public static byte[] GetQRCode(string txt, QRCodeGenerator.ECCLevel eccLevel, int size) |
| 210 | + { |
| 211 | + using var qrGen = new QRCodeGenerator(); |
| 212 | + using var qrCode = qrGen.CreateQrCode(txt, eccLevel); |
| 213 | + using var qrBmp = new BitmapByteQRCode(qrCode); |
| 214 | + return qrBmp.GetGraphic(size); |
| 215 | + |
| 216 | + } |
| 217 | +} |
0 commit comments