def do_mask_demo(): """Creates QR Codes with the same size and contents but different mask patterns.""" # Project Nayuki URL segs = QrSegment.make_segments("https://www.nayuki.io/") print_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=-1)) # Automatic mask print_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=3)) # Force mask 3 # Chinese text as UTF-8 segs = QrSegment.make_segments( u"\u7DAD\u57FA\u767E\u79D1\uFF08\u0057\u0069\u006B\u0069\u0070\u0065\u0064\u0069\u0061\uFF0C" "\u8046\u807D\u0069\u002F\u02CC\u0077\u026A\u006B\u1D7B\u02C8\u0070\u0069\u02D0\u0064\u0069" "\u002E\u0259\u002F\uFF09\u662F\u4E00\u500B\u81EA\u7531\u5167\u5BB9\u3001\u516C\u958B\u7DE8" "\u8F2F\u4E14\u591A\u8A9E\u8A00\u7684\u7DB2\u8DEF\u767E\u79D1\u5168\u66F8\u5354\u4F5C\u8A08" "\u756B") print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=0)) # Force mask 0 print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=1)) # Force mask 1 print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=5)) # Force mask 5 print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=7)) # Force mask 7
def do_segment_demo(): """Creates QR Codes with manually specified segments for better compactness.""" # Illustration "silver" silver0 = "THE SQUARE ROOT OF 2 IS 1." silver1 = "41421356237309504880168872420969807856967187537694807317667973799" qr = QrCode.encode_text(silver0 + silver1, QrCode.Ecc.LOW) print_qr(qr) segs = [ QrSegment.make_alphanumeric(silver0), QrSegment.make_numeric(silver1) ] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr) # Illustration "golden" golden0 = u"Golden ratio \u03C6 = 1." golden1 = u"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" golden2 = u"......" qr = QrCode.encode_text(golden0 + golden1 + golden2, QrCode.Ecc.LOW) print_qr(qr) segs = [ QrSegment.make_bytes(golden0.encode("UTF-8")), QrSegment.make_numeric(golden1), QrSegment.make_alphanumeric(golden2) ] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr) # Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters madoka = u"\u300C\u9B54\u6CD5\u5C11\u5973\u307E\u3069\u304B\u2606\u30DE\u30AE\u30AB\u300D\u3063\u3066\u3001\u3000\u0418\u0410\u0418\u3000\uFF44\uFF45\uFF53\uFF55\u3000\u03BA\u03B1\uFF1F" qr = QrCode.encode_text(madoka, QrCode.Ecc.LOW) print_qr(qr) kanjiCharBits = [ # Kanji mode encoding (13 bits per character) 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, ] segs = [ QrSegment(QrSegment.Mode.KANJI, len(kanjiCharBits) // 13, kanjiCharBits) ] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr)
def do_segment_demo(): """Creates QR Codes with manually specified segments for better compactness.""" # Illustration "silver" silver0 = "THE SQUARE ROOT OF 2 IS 1." silver1 = "41421356237309504880168872420969807856967187537694807317667973799" qr = QrCode.encode_text(silver0 + silver1, QrCode.Ecc.LOW) print_qr(qr) segs = [ QrSegment.make_alphanumeric(silver0), QrSegment.make_numeric(silver1)] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr) # Illustration "golden" golden0 = u"Golden ratio \u03C6 = 1." golden1 = u"6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" golden2 = u"......" qr = QrCode.encode_text(golden0 + golden1 + golden2, QrCode.Ecc.LOW) print_qr(qr) segs = [ QrSegment.make_bytes(golden0.encode("UTF-8")), QrSegment.make_numeric(golden1), QrSegment.make_alphanumeric(golden2)] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr) # Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters madoka = u"\u300C\u9B54\u6CD5\u5C11\u5973\u307E\u3069\u304B\u2606\u30DE\u30AE\u30AB\u300D\u3063\u3066\u3001\u3000\u0418\u0410\u0418\u3000\uFF44\uFF45\uFF53\uFF55\u3000\u03BA\u03B1\uFF1F" qr = QrCode.encode_text(madoka, QrCode.Ecc.LOW) print_qr(qr) kanjiCharBits = [ # Kanji mode encoding (13 bits per character) 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, ] segs = [QrSegment(QrSegment.Mode.KANJI, len(kanjiCharBits) // 13, kanjiCharBits)] qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) print_qr(qr)