|
| 1 | +using System; |
| 2 | +using SixLabors.ImageSharp; |
| 3 | +using SixLabors.ImageSharp.PixelFormats; |
| 4 | +using SixLabors.ImageSharp.Processing; |
| 5 | +using static QRCoder.QRCodeGenerator; |
| 6 | + |
| 7 | +namespace QRCoder.ImageSharp |
| 8 | +{ |
| 9 | + public class QRCode : AbstractQRCode, IDisposable |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Constructor without params to be used in COM Objects connections |
| 13 | + /// </summary> |
| 14 | + public QRCode() { } |
| 15 | + |
| 16 | + public QRCode(QRCodeData data) |
| 17 | + : base(data) { } |
| 18 | + |
| 19 | + public Image GetGraphic(int pixelsPerModule) |
| 20 | + { |
| 21 | + return GetGraphic(pixelsPerModule, Color.Black, Color.White, true); |
| 22 | + } |
| 23 | + |
| 24 | + public Image GetGraphic(int pixelsPerModule, string darkColorHtmlHex, string lightColorHtmlHex, bool drawQuietZones = true) |
| 25 | + { |
| 26 | + return GetGraphic(pixelsPerModule, Color.Parse(darkColorHtmlHex), Color.Parse(lightColorHtmlHex), drawQuietZones); |
| 27 | + } |
| 28 | + |
| 29 | + public Image GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, bool drawQuietZones = true) |
| 30 | + { |
| 31 | + var moduleOffset = drawQuietZones ? 0 : 4; |
| 32 | + var size = (QrCodeData.ModuleMatrix.Count - (moduleOffset * 2)) * pixelsPerModule; |
| 33 | + |
| 34 | + var image = new Image<Rgba32>(size, size); |
| 35 | + DrawQRCode(image, pixelsPerModule, moduleOffset, darkColor, lightColor); |
| 36 | + |
| 37 | + return image; |
| 38 | + } |
| 39 | + |
| 40 | + public Image GetGraphic(int pixelsPerModule, Color darkColor, Color lightColor, Image icon = null, int iconSizePercent = 15, int iconBorderWidth = 0, bool drawQuietZones = true, Color? iconBackgroundColor = null) |
| 41 | + { |
| 42 | + var img = GetGraphic(pixelsPerModule, darkColor, lightColor, drawQuietZones) as Image<Rgba32>; |
| 43 | + if (icon != null && iconSizePercent > 0 && iconSizePercent <= 100) |
| 44 | + { |
| 45 | + var iconDestWidth = iconSizePercent * img.Width / 100f; |
| 46 | + var iconDestHeight = iconDestWidth * icon.Height / icon.Width; |
| 47 | + var iconX = (img.Width - iconDestWidth) / 2; |
| 48 | + var iconY = (img.Height - iconDestHeight) / 2; |
| 49 | + var centerDest = new RectangleF(iconX - iconBorderWidth, iconY - iconBorderWidth, iconDestWidth + (iconBorderWidth * 2), iconDestHeight + (iconBorderWidth * 2)); |
| 50 | + var iconDestRect = new RectangleF(iconX, iconY, iconDestWidth, iconDestHeight); |
| 51 | + |
| 52 | + if (iconBorderWidth > 0) |
| 53 | + { |
| 54 | + if (!iconBackgroundColor.HasValue) |
| 55 | + { |
| 56 | + iconBackgroundColor = lightColor; |
| 57 | + } |
| 58 | + |
| 59 | + if (iconBackgroundColor != Color.Transparent) |
| 60 | + { |
| 61 | + img.ProcessPixelRows(accessor => |
| 62 | + { |
| 63 | + for (var y = (int)centerDest.Top; y <= (int)centerDest.Bottom; y++) |
| 64 | + { |
| 65 | + var pixelRow = accessor.GetRowSpan(y); |
| 66 | + |
| 67 | + for (var x = (int)centerDest.Left; x <= (int)centerDest.Right; x++) |
| 68 | + { |
| 69 | + pixelRow[x] = iconBackgroundColor ?? lightColor; |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + var sizedIcon = icon.Clone(x => x.Resize((int)iconDestWidth, (int)iconDestHeight)); |
| 77 | + img.Mutate(x => x.DrawImage(sizedIcon, new Point((int)iconDestRect.X, (int)iconDestRect.Y), 1)); |
| 78 | + } |
| 79 | + |
| 80 | + return img; |
| 81 | + } |
| 82 | + |
| 83 | + private void DrawQRCode(Image<Rgba32> image, int pixelsPerModule, int moduleOffset, Color darkColor, Color lightColor) |
| 84 | + { |
| 85 | + var row = new Rgba32[image.Width]; |
| 86 | + |
| 87 | + image.ProcessPixelRows(accessor => |
| 88 | + { |
| 89 | + for (var modY = moduleOffset; modY < QrCodeData.ModuleMatrix.Count - moduleOffset; modY++) |
| 90 | + { |
| 91 | + // Generate row for this y-Module |
| 92 | + for (var modX = moduleOffset; modX < QrCodeData.ModuleMatrix.Count - moduleOffset; modX++) |
| 93 | + { |
| 94 | + for (var idx = 0; idx < pixelsPerModule; idx++) |
| 95 | + { |
| 96 | + row[((modX - moduleOffset) * pixelsPerModule) + idx] = this.QrCodeData.ModuleMatrix[modY][modX] ? darkColor : lightColor; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Copy the prepared row to the image |
| 101 | + for (var idx = 0; idx < pixelsPerModule; idx++) |
| 102 | + { |
| 103 | + var pixelRow = accessor.GetRowSpan(((modY - moduleOffset) * pixelsPerModule) + idx); |
| 104 | + row.CopyTo(pixelRow); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public static class ImageSharpQRCodeHelper |
| 112 | + { |
| 113 | + public static Image GetQRCode(string plainText, int pixelsPerModule, Color darkColor, Color lightColor, ECCLevel eccLevel, bool forceUtf8 = false, bool utf8BOM = false, EciMode eciMode = EciMode.Default, int requestedVersion = -1, Image icon = null, int iconSizePercent = 15, int iconBorderWidth = 0, bool drawQuietZones = true) |
| 114 | + { |
| 115 | + using (var qrGenerator = new QRCodeGenerator()) |
| 116 | + using (var qrCodeData = qrGenerator.CreateQrCode(plainText, eccLevel, forceUtf8, utf8BOM, eciMode, requestedVersion)) |
| 117 | + using (var qrCode = new QRCode(qrCodeData)) |
| 118 | + { |
| 119 | + return qrCode.GetGraphic(pixelsPerModule, |
| 120 | + darkColor, |
| 121 | + lightColor, |
| 122 | + icon, |
| 123 | + iconSizePercent, |
| 124 | + iconBorderWidth, |
| 125 | + drawQuietZones); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments