From a0e70ee56f374720bfffd17ba5eb9ffdae385ef4 Mon Sep 17 00:00:00 2001 From: manuelbl Date: Sun, 13 Jan 2019 23:20:24 +0100 Subject: [PATCH] Optimal segments --- dotnet/QrCodeGenerator/QrCode.cs | 2 +- dotnet/QrCodeGenerator/QrSegment.cs | 2 +- dotnet/QrCodeGenerator/QrSegmentAdvanced.cs | 281 +++++++++++++++++- dotnet/QrCodeGeneratorDemo/Program.cs | 244 ++++++++------- .../QrCodeGeneratorTest/OptimalSegmentTest.cs | 77 +++++ 5 files changed, 476 insertions(+), 130 deletions(-) create mode 100644 dotnet/QrCodeGeneratorTest/OptimalSegmentTest.cs diff --git a/dotnet/QrCodeGenerator/QrCode.cs b/dotnet/QrCodeGenerator/QrCode.cs index dd1282b..a42dce3 100644 --- a/dotnet/QrCodeGenerator/QrCode.cs +++ b/dotnet/QrCodeGenerator/QrCode.cs @@ -966,7 +966,7 @@ namespace Io.Nayuki.QrCodeGen // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any // QR Code of the given version number and error correction level, with remainder bits discarded. // This stateless pure function could be implemented as a (40*4)-cell lookup table. - private static int GetNumDataCodewords(int ver, Ecc ecl) + internal static int GetNumDataCodewords(int ver, Ecc ecl) { return GetNumRawDataModules(ver) / 8 - EccCodewordsPerBlock[ecl.Ordinal, ver] diff --git a/dotnet/QrCodeGenerator/QrSegment.cs b/dotnet/QrCodeGenerator/QrSegment.cs index 046aa13..108a136 100644 --- a/dotnet/QrCodeGenerator/QrSegment.cs +++ b/dotnet/QrCodeGenerator/QrSegment.cs @@ -329,7 +329,7 @@ namespace Io.Nayuki.QrCodeGen // The set of all legal characters in alphanumeric mode, where // each character value maps to the index in the string. - static readonly string AlphanumericCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; + internal static readonly string AlphanumericCharset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; #endregion diff --git a/dotnet/QrCodeGenerator/QrSegmentAdvanced.cs b/dotnet/QrCodeGenerator/QrSegmentAdvanced.cs index f53e2a7..4d6cf28 100644 --- a/dotnet/QrCodeGenerator/QrSegmentAdvanced.cs +++ b/dotnet/QrCodeGenerator/QrSegmentAdvanced.cs @@ -24,7 +24,9 @@ using System; using System.Collections; +using System.Collections.Generic; using System.Diagnostics; +using System.Text; using static Io.Nayuki.QrCodeGen.QrSegment; namespace Io.Nayuki.QrCodeGen @@ -39,6 +41,282 @@ namespace Io.Nayuki.QrCodeGen /// public static class QrSegmentAdvanced { + #region Optimal list of segments encoder + + /// + /// Returns a list of zero or more segments to represent the specified Unicode text string. + /// The resulting list optimally minimizes the total encoded bit length, subjected to the constraints + /// in the specified {error correction level, minimum version number, maximum version number}. + /// + /// + /// This function can utilize all four text encoding modes: numeric, alphanumeric, byte (UTF-8), + /// and kanji. This can be considered as a sophisticated but slower replacement for + /// . This requires more input parameters because it searches a + /// range of versions, like . + /// + /// the text to be encoded (not null), which can be any Unicode string + /// the error correction level to use (not null) + /// the minimum allowed version of the QR Code (at least 1) + /// the maximum allowed version of the QR Code (at most 40) + /// a new mutable list (not null) of segments (not null) + /// containing the text, minimizing the bit length with respect to the constraints + /// Thrown if the text or error correction level is null + /// Thrown if 1 ≤ minVersion ≤ maxVersion ≤ 40 is violated + /// Thrown if the text fails to fit in the maxVersion QR Code at the ECL + public static List MakeSegmentsOptimally(string text, QrCode.Ecc ecl, int minVersion, int maxVersion) + { + // Check arguments + Objects.RequireNonNull(text); + Objects.RequireNonNull(ecl); + if (minVersion < QrCode.MinVersion || minVersion > maxVersion) + { + throw new ArgumentOutOfRangeException(nameof(minVersion), "Invalid value"); + } + + if (maxVersion > QrCode.MaxVersion) + { + throw new ArgumentOutOfRangeException(nameof(maxVersion), "Invalid value"); + } + + // Iterate through version numbers, and make tentative segments + List segs = null; + var codePoints = ToCodePoints(text); + for (int version = minVersion;; version++) + { + if (version == minVersion || version == 10 || version == 27) + segs = MakeSegmentsOptimally(codePoints, version); + Debug.Assert(segs != null); + + // Check if the segments fit + int dataCapacityBits = QrCode.GetNumDataCodewords(version, ecl) * 8; + int dataUsedBits = GetTotalBits(segs, version); + if (dataUsedBits != -1 && dataUsedBits <= dataCapacityBits) + return segs; // This version number is found to be suitable + + if (version < maxVersion) continue; + + // All versions in the range could not fit the given text + var msg = "Segment too long"; + if (dataUsedBits != -1) + msg = $"Data length = {dataUsedBits} bits, Max capacity = {dataCapacityBits} bits"; + throw new DataTooLongException(msg); + } + } + + + // Returns a new list of segments that is optimal for the given text at the given version number. + private static List MakeSegmentsOptimally(int[] codePoints, int version) + { + if (codePoints.Length == 0) + return new List(); + var charModes = ComputeCharacterModes(codePoints, version); + return SplitIntoSegments(codePoints, charModes); + } + + + // Returns a new array representing the optimal mode per code point based on the given text and version. + private static Mode[] ComputeCharacterModes(int[] codePoints, int version) + { + if (codePoints.Length == 0) + { + throw new ArgumentOutOfRangeException(nameof(codePoints)); + } + + Mode[] modeTypes = {Mode.Byte, Mode.Alphanumeric, Mode.Numeric, Mode.Kanji}; // Do not modify + int numModes = modeTypes.Length; + + // Segment header sizes, measured in 1/6 bits + var headCosts = new int[numModes]; + for (var i = 0; i < numModes; i++) + { + headCosts[i] = (4 + modeTypes[i].NumCharCountBits(version)) * 6; + } + + // charModes[i][j] represents the mode to encode the code point at + // index i such that the final segment ends in modeTypes[j] and the + // total number of bits is minimized over all possible choices + var charModes = new Mode[codePoints.Length, numModes]; + + // At the beginning of each iteration of the loop below, + // prevCosts[j] is the exact minimum number of 1/6 bits needed to + // encode the entire string prefix of length i, and end in modeTypes[j] + var prevCosts = (int[]) headCosts.Clone(); + + // Calculate costs using dynamic programming + for (var i = 0; i < codePoints.Length; i++) + { + int c = codePoints[i]; + var curCosts = new int[numModes]; + { + // Always extend a byte mode segment + curCosts[0] = prevCosts[0] + CountUtf8Bytes(c) * 8 * 6; + charModes[i, 0] = modeTypes[0]; + } + // Extend a segment if possible + if (AlphanumericCharset.IndexOf((char) c) != -1) + { + // Is alphanumeric + curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char + charModes[i, 1] = modeTypes[1]; + } + + if ('0' <= c && c <= '9') + { + // Is numeric + curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit + charModes[i, 2] = modeTypes[2]; + } + + if (IsKanji(c)) + { + curCosts[3] = prevCosts[3] + 78; // 13 bits per Shift JIS char + charModes[i, 3] = modeTypes[3]; + } + + // Start new segment at the end to switch modes + for (var j = 0; j < numModes; j++) + { + // To mode + for (var k = 0; k < numModes; k++) + { + // From mode + int newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j]; + if (charModes[i, k] == null || (charModes[i, j] != null && newCost >= curCosts[j])) + continue; + curCosts[j] = newCost; + charModes[i, j] = modeTypes[k]; + } + } + + prevCosts = curCosts; + } + + // Find optimal ending mode + Mode curMode = null; + for (int i = 0, minCost = 0; i < numModes; i++) + { + if (curMode != null && prevCosts[i] >= minCost) continue; + minCost = prevCosts[i]; + curMode = modeTypes[i]; + } + + // Get optimal mode for each code point by tracing backwards + var result = new Mode[codePoints.Length]; + for (int i = result.Length - 1; i >= 0; i--) + { + for (var j = 0; j < numModes; j++) + { + if (modeTypes[j] != curMode) continue; + curMode = charModes[i, j]; + result[i] = curMode; + break; + } + } + + return result; + } + + + // Returns a new list of segments based on the given text and modes, such that + // consecutive code points in the same mode are put into the same segment. + private static List SplitIntoSegments(int[] codePoints, Mode[] charModes) + { + if (codePoints.Length == 0) + throw new ArgumentOutOfRangeException(nameof(codePoints)); + var result = new List(); + + // Accumulate run of modes + var curMode = charModes[0]; + var start = 0; + for (var i = 1;; i++) + { + if (i < codePoints.Length && charModes[i] == curMode) + continue; + + string s = FromCodePoints(codePoints, start, i - start); + if (curMode == Mode.Byte) + { + result.Add(MakeBytes(Encoding.UTF8.GetBytes(s))); + } + else if (curMode == Mode.Numeric) + { + result.Add(MakeNumeric(s)); + } + else if (curMode == Mode.Alphanumeric) + { + result.Add(MakeAlphanumeric(s)); + } + else if (curMode == Mode.Kanji) + { + result.Add(MakeKanji(s)); + } + else + { + Debug.Assert(false); + } + + if (i >= codePoints.Length) + { + return result; + } + + curMode = charModes[i]; + start = i; + } + } + + + public static string FromCodePoints(int[] codepoints, int startIndex, int count) + { + bool useBigEndian = !BitConverter.IsLittleEndian; + Encoding utf32 = new UTF32Encoding(useBigEndian, false , true); + + var octets = new byte[count * 4]; + for (int i = startIndex, j = 0; i < startIndex + count; i++, j += 4) + { + var bytes = BitConverter.GetBytes(codepoints[i]); + octets[j] = bytes[0]; + octets[j + 1] = bytes[1]; + octets[j + 2] = bytes[2]; + octets[j + 3] = bytes[3]; + } + + return utf32.GetString(octets); + } + + + // Returns a new array of Unicode code points (effectively + // UTF-32 / UCS-4) representing the given UTF-16 string. + private static int[] ToCodePoints(string s) + { + bool useBigEndian = !BitConverter.IsLittleEndian; + Encoding utf32 = new UTF32Encoding(useBigEndian, false , true); + var octets = utf32.GetBytes(s) ; + + var result = new int[octets.Length / 4]; + for (int i = 0, j = 0; i < octets.Length; i += 4, j++) + { + result[j] = BitConverter.ToInt32(octets, i); + } + + return result; + } + + + // Returns the number of UTF-8 bytes needed to encode the given Unicode code point. + private static int CountUtf8Bytes(int cp) + { + if (cp < 0) throw new ArgumentOutOfRangeException(nameof(cp), "Invalid code point"); + if (cp < 0x80) return 1; + if (cp < 0x800) return 2; + if (cp < 0x10000) return 3; + if (cp < 0x110000) return 4; + throw new ArgumentOutOfRangeException(nameof(cp), "Invalid code point"); + } + + + #endregion + #region Kanji mode segment encoder @@ -85,7 +363,7 @@ namespace Io.Nayuki.QrCodeGen /// Examples of non-encodable characters include {ordinary ASCII, half-width katakana, /// more extensive Chinese hanzi}. /// - /// the string to test for encodability (not {@code null}) + /// the string to test for encodability (not null) /// true iff each character is in the kanji mode character set /// Thrown if the string is null public static bool IsEncodableAsKanji(string text) { @@ -236,7 +514,6 @@ namespace Io.Nayuki.QrCodeGen } #endregion - } } diff --git a/dotnet/QrCodeGeneratorDemo/Program.cs b/dotnet/QrCodeGeneratorDemo/Program.cs index 56083a8..d26ef88 100644 --- a/dotnet/QrCodeGeneratorDemo/Program.cs +++ b/dotnet/QrCodeGeneratorDemo/Program.cs @@ -38,144 +38,136 @@ namespace Io.Nayuki.QrCodeGen.Demo DoSegmentDemo(); DoMaskDemo(); } - - - /*---- Demo suite ----*/ - - // Creates a single QR Code, then writes it to a PNG file and an SVG file. - private static void DoBasicDemo() - { - const string text = "Hello, world!"; // User-supplied Unicode text - QrCode.Ecc errCorLvl = QrCode.Ecc.Low; // Error correction level - var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR Code symbol - using (var img = qr.ToBitmap(10, 4)) // Convert to bitmap image + #region Demo suite + + // Creates a single QR Code, then writes it to a PNG file and an SVG file. + private static void DoBasicDemo() { - img.Save("hello-world-QR.png", ImageFormat.Png); // Write image to file - } - - string svg = qr.ToSvgString(4); // Convert to SVG XML code - File.WriteAllText("hello-world-QR.svg", svg, Encoding.UTF8); // Write image to file - } - + const string text = "Hello, world!"; // User-supplied Unicode text + var errCorLvl = QrCode.Ecc.Low; // Error correction level - // Creates a variety of QR Codes that exercise different features of the library, and writes each one to file. - private static void DoVarietyDemo() { - QrCode qr; - - // Numeric mode encoding (3.33 bits per digit) - qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.Medium); - SaveAsPng(qr, "pi-digits-QR.png", 13, 1); - - // Alphanumeric mode encoding (5.5 bits per character) - qr = QrCode.EncodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.High); - SaveAsPng(qr, "alphanumeric-QR.png", 10, 2); - - // Unicode text as UTF-8 - qr = QrCode.EncodeText("こんにちwa、世界! αβγδ", QrCode.Ecc.Quartile); - SaveAsPng(qr, "unicode-QR.png", 10, 3); - - // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland) - qr = QrCode.EncodeText( - "Alice was beginning to get very tired of sitting by her sister on the bank, " - + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " - + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " - + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " - + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " - + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " - + "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.High); - SaveAsPng(qr, "alice-wonderland-QR.png", 6, 10); - } - - - // Creates QR Codes with manually specified segments for better compactness. - private static void DoSegmentDemo() - { - QrCode qr; - List segs; + var qr = QrCode.EncodeText(text, errCorLvl); // Make the QR Code symbol - // Illustration "silver" - var silver0 = "THE SQUARE ROOT OF 2 IS 1."; - var silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; - qr = QrCode.EncodeText(silver0 + silver1, QrCode.Ecc.Low); - SaveAsPng(qr, "sqrt2-monolithic-QR.png", 10, 3); + using (var img = qr.ToBitmap(10, 4)) // Convert to bitmap image + { + img.Save("hello-world-QR.png", ImageFormat.Png); // Write image to file + } - segs = new List - { - QrSegment.MakeAlphanumeric(silver0), - QrSegment.MakeNumeric(silver1) - }; - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); - SaveAsPng(qr, "sqrt2-segmented-QR.png", 10, 3); - - // Illustration "golden" - string golden0 = "Golden ratio φ = 1."; - string golden1 = - "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; - string golden2 = "......"; - qr = QrCode.EncodeText(golden0 + golden1 + golden2, QrCode.Ecc.Low); - SaveAsPng(qr, "phi-monolithic-QR.png", 8, 5); - - segs = new List - { - QrSegment.MakeBytes(Encoding.UTF8.GetBytes(golden0)), - QrSegment.MakeNumeric(golden1), - QrSegment.MakeAlphanumeric(golden2) - }; + string svg = qr.ToSvgString(4); // Convert to SVG XML code + File.WriteAllText("hello-world-QR.svg", svg, Encoding.UTF8); // Write image to file + } - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); - SaveAsPng(qr, "phi-segmented-QR.png", 8, 5); - - // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters - string madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; - qr = QrCode.EncodeText(madoka, QrCode.Ecc.Low); - SaveAsPng(qr, "madoka-utf8-QR.png", 9, 4); - segs = new List + // Creates a variety of QR Codes that exercise different features of the library, and writes each one to file. + private static void DoVarietyDemo() { + // Numeric mode encoding (3.33 bits per digit) + var qr = QrCode.EncodeText("314159265358979323846264338327950288419716939937510", QrCode.Ecc.Medium); + SaveAsPng(qr, "pi-digits-QR.png", 13, 1); + + // Alphanumeric mode encoding (5.5 bits per character) + qr = QrCode.EncodeText("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.High); + SaveAsPng(qr, "alphanumeric-QR.png", 10, 2); + + // Unicode text as UTF-8 + qr = QrCode.EncodeText("こんにちwa、世界! αβγδ", QrCode.Ecc.Quartile); + SaveAsPng(qr, "unicode-QR.png", 10, 3); + + // Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland) + qr = QrCode.EncodeText( + "Alice was beginning to get very tired of sitting by her sister on the bank, " + + "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " + + "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " + + "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " + + "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " + + "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " + + "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.High); + SaveAsPng(qr, "alice-wonderland-QR.png", 6, 10); + } + + + // Creates QR Codes with manually specified segments for better compactness. + private static void DoSegmentDemo() { - // QrSegmentAdvanced.MakeKanji(madoka) - }; - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); - SaveAsPng(qr, "madoka-kanji-QR.png", 9, 4); - } - - - // Creates QR Codes with the same size and contents but different mask patterns. - private static void DoMaskDemo() { - QrCode qr; - List segs; - - // Project Nayuki URL - segs = QrSegment.MakeSegments("https://www.nayuki.io/"); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, -1, true); // Automatic mask - SaveAsPng(qr, "project-nayuki-automask-QR.png", 8, 6); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3, true); // Force mask 3 - SaveAsPng(qr, "project-nayuki-mask3-QR.png", 8, 6); - - // Chinese text as UTF-8 - segs = QrSegment.MakeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 0, true); // Force mask 0 - SaveAsPng(qr, "unicode-mask0-QR.png", 10, 3); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 1, true); // Force mask 1 - SaveAsPng(qr, "unicode-mask1-QR.png", 10, 3); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 5, true); // Force mask 5 - SaveAsPng(qr, "unicode-mask5-QR.png", 10, 3); - qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 7, true); // Force mask 7 - SaveAsPng(qr, "unicode-mask7-QR.png", 10, 3); - } - - #region Utilities - - private static void SaveAsPng(QrCode qrCode, string filename, int scale, int border) - { - using (var bitmap = qrCode.ToBitmap(scale, border)) + // Illustration "silver" + const string silver0 = "THE SQUARE ROOT OF 2 IS 1."; + const string silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; + var qr = QrCode.EncodeText(silver0 + silver1, QrCode.Ecc.Low); + SaveAsPng(qr, "sqrt2-monolithic-QR.png", 10, 3); + + var segs = new List + { + QrSegment.MakeAlphanumeric(silver0), + QrSegment.MakeNumeric(silver1) + }; + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); + SaveAsPng(qr, "sqrt2-segmented-QR.png", 10, 3); + + // Illustration "golden" + const string golden0 = "Golden ratio φ = 1."; + const string golden1 = + "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; + const string golden2 = "......"; + qr = QrCode.EncodeText(golden0 + golden1 + golden2, QrCode.Ecc.Low); + SaveAsPng(qr, "phi-monolithic-QR.png", 8, 5); + + segs = new List + { + QrSegment.MakeBytes(Encoding.UTF8.GetBytes(golden0)), + QrSegment.MakeNumeric(golden1), + QrSegment.MakeAlphanumeric(golden2) + }; + + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); + SaveAsPng(qr, "phi-segmented-QR.png", 8, 5); + + // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters + const string madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; + qr = QrCode.EncodeText(madoka, QrCode.Ecc.Low); + SaveAsPng(qr, "madoka-utf8-QR.png", 9, 4); + + segs = new List { QrSegmentAdvanced.MakeKanji(madoka) }; + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Low); + SaveAsPng(qr, "madoka-kanji-QR.png", 9, 4); + } + + + // Creates QR Codes with the same size and contents but different mask patterns. + private static void DoMaskDemo() { + // Project Nayuki URL + var segs = QrSegment.MakeSegments("https://www.nayuki.io/"); + var qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, -1, true); + SaveAsPng(qr, "project-nayuki-automask-QR.png", 8, 6); + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3, true); // Force mask 3 + SaveAsPng(qr, "project-nayuki-mask3-QR.png", 8, 6); + + // Chinese text as UTF-8 + segs = QrSegment.MakeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"); + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 0, true); // Force mask 0 + SaveAsPng(qr, "unicode-mask0-QR.png", 10, 3); + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 1, true); // Force mask 1 + SaveAsPng(qr, "unicode-mask1-QR.png", 10, 3); + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 5, true); // Force mask 5 + SaveAsPng(qr, "unicode-mask5-QR.png", 10, 3); + qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 7, true); // Force mask 7 + SaveAsPng(qr, "unicode-mask7-QR.png", 10, 3); + } + + #endregion + + + #region Utilities + + private static void SaveAsPng(QrCode qrCode, string filename, int scale, int border) { - bitmap.Save(filename, ImageFormat.Png); + using (var bitmap = qrCode.ToBitmap(scale, border)) + { + bitmap.Save(filename, ImageFormat.Png); + } } - } - #endregion + #endregion } } diff --git a/dotnet/QrCodeGeneratorTest/OptimalSegmentTest.cs b/dotnet/QrCodeGeneratorTest/OptimalSegmentTest.cs new file mode 100644 index 0000000..0f4ae49 --- /dev/null +++ b/dotnet/QrCodeGeneratorTest/OptimalSegmentTest.cs @@ -0,0 +1,77 @@ +/* + * QR Code generator library (.NET) + * + * Copyright (c) Project Nayuki. (MIT License) + * https://www.nayuki.io/page/qr-code-generator-library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + * the Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * - The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * - The Software is provided "as is", without warranty of any kind, express or + * implied, including but not limited to the warranties of merchantability, + * fitness for a particular purpose and noninfringement. In no event shall the + * authors or copyright holders be liable for any claim, damages or other + * liability, whether in an action of contract, tort or otherwise, arising from, + * out of or in connection with the Software or the use or other dealings in the + * Software. + */ + +using System.Collections.Generic; +using Xunit; + +namespace Io.Nayuki.QrCodeGen.Test +{ + public class OptimalSegmentTest + { + private const string Text1 = "2342342340ABC234234jkl~~"; + + private static readonly string[] Modules1 = { + "XXXXXXX XXXX XXXX X XXXXXXX", + "X X X XX X X", + "X XXX X X XXX XXX X X XXX X", + "X XXX X XXXXX X XX X XXX X", + "X XXX X X XX XX X XXX X", + "X X X X X X X", + "XXXXXXX X X X X X X X XXXXXXX", + " X XX XXX ", + " X XXX XX XXX X XX X X", + " X XX X XX X XXXXX", + " XXX X XXXX X ", + " X X X X XX X XXX", + "XX X XXXXX XXXXXXXXX XXXX ", + " X X X XX X X X X ", + " XXXXX XXX XXX XX X X", + "XXXXX XX XX X XXX X XXX ", + "XXX XXX XXX X XX ", + " X X XX X X X X ", + "X X XXXX XXXX X X X X X ", + " X X X XX X X XXX X XX XXX", + "X X XX X XXX XX XXXXXXX X", + " X X X X XXXX ", + "XXXXXXX X XX X XXX X X X ", + "X X X X XX X XX X XX ", + "X XXX X XXXX XX X X XXXXX ", + "X XXX X X X X XX XX X ", + "X XXX X X X XXXXXX X X", + "X X XXX XX X X XXX X ", + "XXXXXXX XXXX X XX XX X" + }; + + [Fact] + private void OptimalSegmentCode() + { + var segments = QrSegmentAdvanced.MakeSegmentsOptimally(Text1, QrCode.Ecc.High, 1, 40); + var qrCode = QrCode.EncodeSegments(segments, QrCode.Ecc.High); + + Assert.Same(QrCode.Ecc.High, qrCode.ErrorCorrectionLevel); + Assert.Equal(29, qrCode.Size); + Assert.Equal(0, qrCode.Mask); + Assert.Equal(Modules1, TestHelper.ToStringArray(qrCode)); + } + } +}