|
| 1 | +// |
| 2 | +// QR code generator library (.NET) |
| 3 | +// https://github.com/manuelbl/QrCodeGenerator |
| 4 | +// |
| 5 | +// Copyright (c) 2022 suxiaobu9, Manuel Bleichenbacher |
| 6 | +// Licensed under MIT License |
| 7 | +// https://opensource.org/licenses/MIT |
| 8 | +// |
| 9 | + |
| 10 | +using ImageMagick; |
| 11 | + |
| 12 | +namespace Net.Codecrete.QrCodeGenerator; |
| 13 | + |
| 14 | +public static class QrCodeImageExtensions |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Creates a image of this QR code. |
| 18 | + /// <para> |
| 19 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 20 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 21 | + /// of modules to add as a border to all four sides can be specified. |
| 22 | + /// </para> |
| 23 | + /// <para> |
| 24 | + /// For example, <c>ToBitmap(scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 25 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 26 | + /// </para> |
| 27 | + /// </summary> |
| 28 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 29 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 30 | + /// <param name="background">The background color.</param> |
| 31 | + /// <param name="foreground">The foreground color.</param> |
| 32 | + /// <returns></returns> |
| 33 | + /// <exception cref="ArgumentOutOfRangeException"></exception> |
| 34 | + public static MagickImage ToImage(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background) |
| 35 | + { |
| 36 | + if (scale <= 0) |
| 37 | + { |
| 38 | + throw new ArgumentOutOfRangeException(nameof(scale), " Value out of range"); |
| 39 | + } |
| 40 | + |
| 41 | + if (border < 0) |
| 42 | + { |
| 43 | + throw new ArgumentOutOfRangeException(nameof(border), " Value out of range"); |
| 44 | + } |
| 45 | + |
| 46 | + var size = qrCode.Size; |
| 47 | + var dim = (size + border * 2) * scale; |
| 48 | + |
| 49 | + if (dim > short.MaxValue) |
| 50 | + { |
| 51 | + throw new ArgumentOutOfRangeException(nameof(scale), " Scale or border too large"); |
| 52 | + } |
| 53 | + |
| 54 | + var image = new MagickImage(background, dim, dim) |
| 55 | + { |
| 56 | + Format = MagickFormat.Png, |
| 57 | + }; |
| 58 | + |
| 59 | + var drawables = new Drawables(); |
| 60 | + drawables.FillColor(foreground); |
| 61 | + |
| 62 | + for (var x = 0; x < size; x++) |
| 63 | + { |
| 64 | + var pointerX = (x + border) * scale; |
| 65 | + |
| 66 | + for (var y = 0; y < size; y++) |
| 67 | + { |
| 68 | + if (qrCode.GetModule(x, y)) |
| 69 | + { |
| 70 | + var pointerY = (y + border) * scale; |
| 71 | + drawables.Rectangle(pointerX, pointerY, pointerX + scale - 1, pointerY + scale - 1); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + drawables.Draw(image); |
| 76 | + return image; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Creates a PNG image of this QR code and returns it as a byte array. |
| 81 | + /// <para> |
| 82 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 83 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 84 | + /// of modules to add as a border to all four sides can be specified. |
| 85 | + /// </para> |
| 86 | + /// <para> |
| 87 | + /// For example, <c>ToPng(scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 88 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 89 | + /// </para> |
| 90 | + /// </summary> |
| 91 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 92 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 93 | + /// <param name="foreground">The foreground color.</param> |
| 94 | + /// <param name="background">The background color.</param> |
| 95 | + /// <returns></returns> |
| 96 | + public static byte[] ToPng(this QrCode qrCode, int scale, int border, MagickColor foreground, MagickColor background) |
| 97 | + { |
| 98 | + using var image = qrCode.ToImage(scale, border, foreground, background); |
| 99 | + return image.ToByteArray(); |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Saves this QR code as a PNG file. |
| 104 | + /// <para> |
| 105 | + /// The <paramref name="scale"/> parameter specifies the scale of the image, which is |
| 106 | + /// equivalent to the width and height of each QR code module. Additionally, the number |
| 107 | + /// of modules to add as a border to all four sides can be specified. |
| 108 | + /// </para> |
| 109 | + /// <para> |
| 110 | + /// For example, <c>SaveAsPng("qrcode.png", scale: 10, border: 4)</c> means to pad the QR code with 4 white |
| 111 | + /// border modules on all four sides, and use 10×10 pixels to represent each module. |
| 112 | + /// </para> |
| 113 | + /// </summary> |
| 114 | + /// <param name="scale">The width and height, in pixels, of each module.</param> |
| 115 | + /// <param name="border">The number of border modules to add to each of the four sides.</param> |
| 116 | + /// <param name="foreground">The foreground color.</param> |
| 117 | + /// <param name="background">The background color.</param> |
| 118 | + public static void SaveAsPng(this QrCode qrCode, string fileName, int scale, int border, MagickColor foreground, MagickColor background) |
| 119 | + { |
| 120 | + using var image = qrCode.ToImage(scale, border, foreground, background); |
| 121 | + image.Write(fileName); |
| 122 | + } |
| 123 | +} |
0 commit comments