public void InvalidAlphanumeric() { Assert.False(QrSegment.IsAlphanumeric(",")); Assert.False(QrSegment.IsAlphanumeric("^")); Assert.False(QrSegment.IsAlphanumeric("(")); Assert.False(QrSegment.IsAlphanumeric("a")); }
public static void GenerateQrCode(string text, string file) { var qrSegment = QrSegment.MakeBytes(Encoding.UTF8.GetBytes(text)); var qrCode = QrCode.EncodeSegments(new List <QrSegment>() { qrSegment }, QrCode.Ecc.Low, 10, 40, 0, false); var qrCodePng = new QrCodePng(qrCode, 5, 5); File.WriteAllBytes(file, qrCodePng.GetBytes()); }
void AlphanumericEncoding() { QrSegment segment = QrSegment.MakeAlphanumeric(TextAlphanumeric); Assert.Equal(segment.EncodingMode, QrSegment.Mode.Alphanumeric); Assert.Equal(TextAlphanumeric.Length, segment.NumChars); BitArray data = segment.GetData(); Assert.Equal(BitLengthAlphanumeric, data.Length); Assert.Equal(BitsAlphanumeric, BitArrayToByteArray(data)); }
// Creates QR Codes with manually specified segments for better compactness. private static void DoSegmentDemo() { QrCode qr; List <QrSegment> segs; // Illustration "silver" var silver0 = "THE SQUARE ROOT OF 2 IS 1."; var silver1 = "41421356237309504880168872420969807856967187537694807317667973799"; qr = QrCode.EncodeText(silver0 + silver1, Ecc.Low); WritePng(qr.ToImage(10, 3), "sqrt2-monolithic-QR.png"); segs = new List <QrSegment>() { QrSegment.MakeAlphanumeric(silver0), QrSegment.MakeNumeric(silver1) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(10, 3), "sqrt2-segmented-QR.png"); // Illustration "golden" var golden0 = "Golden ratio φ = 1."; var golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"; var golden2 = "......"; qr = QrCode.EncodeText(golden0 + golden1 + golden2, Ecc.Low); WritePng(qr.ToImage(8, 5), "phi-monolithic-QR.png"); segs = new List <QrSegment>() { QrSegment.MakeBytes(Encoding.UTF8.GetBytes(golden0)), QrSegment.MakeNumeric(golden1), QrSegment.MakeAlphanumeric(golden2) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(8, 5), "phi-segmented-QR.png"); // Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters var madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?"; qr = QrCode.EncodeText(madoka, Ecc.Low); WritePng(qr.ToImage(9, 4), "madoka-utf8-QR.png"); segs = new List <QrSegment>() { QrSegmentAdvanced.MakeKanji(madoka) }; qr = QrCode.EncodeSegments(segs, Ecc.Low); WritePng(qr.ToImage(9, 4), "madoka-kanji-QR.png"); }
void Utf8Encoding() { List <QrSegment> segments = QrSegment.MakeSegments(TextUtf8); Assert.Single(segments); QrSegment segment = segments[0]; Assert.Equal(segment.EncodingMode, QrSegment.Mode.Byte); Assert.Equal(Encoding.UTF8.GetBytes(TextUtf8).Length, segment.NumChars); BitArray data = segment.GetData(); Assert.Equal(BitLengthUtf8, data.Length); Assert.Equal(BitsUtf8, BitArrayToByteArray(data)); }
void AutoAlphanumericEncoding() { List <QrSegment> segments = QrSegment.MakeSegments(TextAlphanumeric); Assert.Single(segments); QrSegment segment = segments[0]; Assert.Equal(segment.EncodingMode, QrSegment.Mode.Alphanumeric); Assert.Equal(TextAlphanumeric.Length, segment.NumChars); BitArray data = segment.GetData(); Assert.Equal(BitLengthAlphanumeric, data.Length); Assert.Equal(BitsAlphanumeric, BitArrayToByteArray(data)); }
// 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); SaveAsPng(qr, "project-nayuki-automask-QR.png", 8, 6); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.High, QrCode.MinVersion, QrCode.MaxVersion, 3); // 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); // Force mask 0 SaveAsPng(qr, "unicode-mask0-QR.png", 10, 3); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 1); // Force mask 1 SaveAsPng(qr, "unicode-mask1-QR.png", 10, 3); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 5); // Force mask 5 SaveAsPng(qr, "unicode-mask5-QR.png", 10, 3); qr = QrCode.EncodeSegments(segs, QrCode.Ecc.Medium, QrCode.MinVersion, QrCode.MaxVersion, 7); // Force mask 7 SaveAsPng(qr, "unicode-mask7-QR.png", 10, 3); }
// Creates QR Codes with the same size and contents but different mask patterns. private static void DoMaskDemo() { QrCode qr; List <QrSegment> segs; // Project Nayuki URL segs = QrSegment.MakeSegments("https://www.nayuki.io/"); qr = QrCode.EncodeSegments(segs, Ecc.High, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true); // Automatic mask WritePng(qr.ToImage(8, 6), "project-nayuki-automask-QR.png"); qr = QrCode.EncodeSegments(segs, Ecc.High, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true); // Force mask 3 WritePng(qr.ToImage(8, 6), "project-nayuki-mask3-QR.png"); // Chinese text as UTF-8 segs = QrSegment.MakeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"); qr = QrCode.EncodeSegments(segs, Ecc.Medium, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 0, true); // Force mask 0 WritePng(qr.ToImage(10, 3), "unicode-mask0-QR.png"); qr = QrCode.EncodeSegments(segs, Ecc.Medium, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 1, true); // Force mask 1 WritePng(qr.ToImage(10, 3), "unicode-mask1-QR.png"); qr = QrCode.EncodeSegments(segs, Ecc.Medium, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 5, true); // Force mask 5 WritePng(qr.ToImage(10, 3), "unicode-mask5-QR.png"); qr = QrCode.EncodeSegments(segs, Ecc.Medium, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 7, true); // Force mask 7 WritePng(qr.ToImage(10, 3), "unicode-mask7-QR.png"); }
public void EmptyIsAlphanumeric() { Assert.True(QrSegment.IsAlphanumeric("")); }
public void ValidAlphanumeric() { Assert.True(QrSegment.IsAlphanumeric("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./")); }
public void WhitespaceIsNotNumeric() { Assert.False(QrSegment.IsNumeric("123\n345")); }
public void TextIsNotNumeric() { Assert.False(QrSegment.IsNumeric("123a")); }
public void IsNumeric() { Assert.True(QrSegment.IsNumeric("1234")); }
void RejectNonAlphanumeric() { Assert.Throws <ArgumentOutOfRangeException>(() => QrSegment.MakeAlphanumeric("abc,def")); }
void RejectNonNumeric() { Assert.Throws <ArgumentOutOfRangeException>(() => QrSegment.MakeNumeric("abc")); }
void EmptyTest() { List <QrSegment> segments = QrSegment.MakeSegments(""); Assert.Empty(segments); }