|
| 1 | +/* |
| 2 | + * QR code generator library (.NET) |
| 3 | + * |
| 4 | + * Copyright (c) Manuel Bleichenbacher (MIT License) |
| 5 | + * https://github.com/manuelbl/QrCodeGenerator |
| 6 | + */ |
| 7 | + |
| 8 | +using System.IO; |
| 9 | +using System.Text; |
| 10 | + |
| 11 | +namespace Net.Codecrete.QrCodeGenerator.Demo |
| 12 | +{ |
| 13 | + internal class Program |
| 14 | + { |
| 15 | + // The main application program. |
| 16 | + internal static void Main() |
| 17 | + { |
| 18 | + // Enable support for special encodings like Shift-JIS |
| 19 | + Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
| 20 | + |
| 21 | + BasicQrCode(); |
| 22 | + AlphanumericText(); |
| 23 | + LongerText(); |
| 24 | + Emojis(); |
| 25 | + EmojisWithoutEci(); |
| 26 | + BinaryData(); |
| 27 | + GraphicsFormats(); |
| 28 | + } |
| 29 | + |
| 30 | + // Creates a single QR code, then writes it to an SVG file. |
| 31 | + private static void BasicQrCode() |
| 32 | + { |
| 33 | + const string text = "Hello, world!"; // Payload text |
| 34 | + var errCorLvl = QrCode.Ecc.Low; // Minimal error correction level |
| 35 | + |
| 36 | + var qrCode = QrCode.EncodeText(text, errCorLvl); // Create the QR code symbol |
| 37 | + SaveAsSvg(qrCode, "hello-world-qr.svg"); // Save as SVG |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + // Creates QR code with digits and alphanumeric characters only. |
| 42 | + private static void AlphanumericText() |
| 43 | + { |
| 44 | + // For digits, a more compact representation will automatically be chosen (3.33 bits per digit) |
| 45 | + var qrCode = QrCode.EncodeText("27182818284590452353602874713526624977572470936999595749669676277240766", |
| 46 | + QrCode.Ecc.Medium); |
| 47 | + SaveAsSvg(qrCode, "digits-qr.svg"); |
| 48 | + |
| 49 | + // For an alphanumeric subset of characters (not including lower-case letters), |
| 50 | + // a more compact representation will be automatically chosen (5.5 bits per character) |
| 51 | + qrCode = QrCode.EncodeText("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG", QrCode.Ecc.High); |
| 52 | + SaveAsSvg(qrCode, "alphanumeric-qr.svg"); |
| 53 | + } |
| 54 | + |
| 55 | + private static void LongerText() |
| 56 | + { |
| 57 | + // Moderately large QR code using longer text |
| 58 | + var qrCode = QrCode.EncodeText( |
| 59 | + "I was a Flower of the mountain yes when I put the rose in my hair like the Andalusian girls used " + |
| 60 | + "or shall I wear a red yes and how he kissed me under the Moorish wall and I thought well as well " + |
| 61 | + "him as another and then I asked him with my eyes to ask again yes and then he asked me would I " + |
| 62 | + "yes to say yes my mountain flower and first I put my arms around him yes and drew him down to me " + |
| 63 | + "so he could feel my breasts all perfume yes and his heart was going like mad and yes I said yes " + |
| 64 | + "I will Yes.", QrCode.Ecc.High); |
| 65 | + SaveAsSvg(qrCode, "joyce-qr.svg"); |
| 66 | + } |
| 67 | + |
| 68 | + private static void Emojis() |
| 69 | + { |
| 70 | + // The full Unicode character set is supported. |
| 71 | + // By default, the library uses UTF-8 encoding and indicates this with an ECI designator. |
| 72 | + var qrCode = QrCode.EncodeText("🎲 😇 🤒 🏌 ⏭ 🚍", QrCode.Ecc.Quartile); |
| 73 | + SaveAsSvg(qrCode, "emojis-qr.svg"); |
| 74 | + } |
| 75 | + |
| 76 | + private static void EmojisWithoutEci() |
| 77 | + { |
| 78 | + // Suppress the ECI designator. |
| 79 | + // Most QR code readers will correctly guess the encoding. |
| 80 | + // Some readers always ignore the ECI designator. |
| 81 | + var qrCode = QrCode.EncodeTextAdvanced("🎲 😇 🤒 🏌 ⏭ 🚍", QrCode.Ecc.Quartile, |
| 82 | + encoding: Encoding.UTF8, eci: ECI.None); |
| 83 | + SaveAsSvg(qrCode, "emojis-no-eci-qr.svg"); |
| 84 | + } |
| 85 | + |
| 86 | + private static void BinaryData() |
| 87 | + { |
| 88 | + // Encode binary data. An ECI designator will be added to indicate it. |
| 89 | + // Exchanging binary data with QR codes usually works in closed systems only. |
| 90 | + byte[] data = { |
| 91 | + 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, |
| 92 | + 0x01, 0x00, 0x80, 0x01, 0x00, 0xff, 0xff, 0xff, |
| 93 | + 0x00, 0x00, 0x00, 0x21, 0xf9, 0x04, 0x01, 0x0a, |
| 94 | + 0x00, 0x01, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00, |
| 95 | + 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4c, |
| 96 | + 0x01, 0x00, 0x3b |
| 97 | + }; |
| 98 | + var qr = QrCode.EncodeBinary(data, QrCode.Ecc.Medium); |
| 99 | + SaveAsSvg(qr, "binary.svg"); |
| 100 | + } |
| 101 | + |
| 102 | + private static void GraphicsFormats() |
| 103 | + { |
| 104 | + // Save the QR code in various graphics formats, directly supported by the library. |
| 105 | + // See the demo applications for further graphics format and displaying options. |
| 106 | + var qrCode = QrCode.EncodeText( |
| 107 | + "Ineluctable modality of the visible: at least that if no more, thought through my eyes. Signatures " + |
| 108 | + "of all things I am here to read, seapawn and searack, the nearing tide, that rusty boot. Snotgreen, " + |
| 109 | + "bluesilver, rust: colored signs. Limits of the diaphane.", QrCode.Ecc.Medium); |
| 110 | + |
| 111 | + File.WriteAllBytes("qr-code.png", qrCode.ToPngBitmap(border: 4)); |
| 112 | + |
| 113 | + File.WriteAllBytes("qr-code.bmp", qrCode.ToBmpBitmap(border: 4)); |
| 114 | + } |
| 115 | + |
| 116 | + private static void SaveAsSvg(QrCode qrCode, string filname) |
| 117 | + { |
| 118 | + string svg = qrCode.ToSvgString(4); // Convert to SVG XML code |
| 119 | + File.WriteAllText(filname, svg, Encoding.UTF8); // Write image to file |
| 120 | + } |
| 121 | + |
| 122 | + } |
| 123 | +} |
0 commit comments