15317bbafSopenharmony_ci# 25317bbafSopenharmony_ci# QR Code generator demo (Python) 35317bbafSopenharmony_ci# 45317bbafSopenharmony_ci# Run this command-line program with no arguments. The program computes a bunch of demonstration 55317bbafSopenharmony_ci# QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample. 65317bbafSopenharmony_ci# 75317bbafSopenharmony_ci# Copyright (c) Project Nayuki. (MIT License) 85317bbafSopenharmony_ci# https://www.nayuki.io/page/qr-code-generator-library 95317bbafSopenharmony_ci# 105317bbafSopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy of 115317bbafSopenharmony_ci# this software and associated documentation files (the "Software"), to deal in 125317bbafSopenharmony_ci# the Software without restriction, including without limitation the rights to 135317bbafSopenharmony_ci# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 145317bbafSopenharmony_ci# the Software, and to permit persons to whom the Software is furnished to do so, 155317bbafSopenharmony_ci# subject to the following conditions: 165317bbafSopenharmony_ci# - The above copyright notice and this permission notice shall be included in 175317bbafSopenharmony_ci# all copies or substantial portions of the Software. 185317bbafSopenharmony_ci# - The Software is provided "as is", without warranty of any kind, express or 195317bbafSopenharmony_ci# implied, including but not limited to the warranties of merchantability, 205317bbafSopenharmony_ci# fitness for a particular purpose and noninfringement. In no event shall the 215317bbafSopenharmony_ci# authors or copyright holders be liable for any claim, damages or other 225317bbafSopenharmony_ci# liability, whether in an action of contract, tort or otherwise, arising from, 235317bbafSopenharmony_ci# out of or in connection with the Software or the use or other dealings in the 245317bbafSopenharmony_ci# Software. 255317bbafSopenharmony_ci# 265317bbafSopenharmony_ci 275317bbafSopenharmony_cifrom typing import List 285317bbafSopenharmony_cifrom qrcodegen import QrCode, QrSegment 295317bbafSopenharmony_ci 305317bbafSopenharmony_ci 315317bbafSopenharmony_cidef main() -> None: 325317bbafSopenharmony_ci """The main application program.""" 335317bbafSopenharmony_ci do_basic_demo() 345317bbafSopenharmony_ci do_variety_demo() 355317bbafSopenharmony_ci do_segment_demo() 365317bbafSopenharmony_ci do_mask_demo() 375317bbafSopenharmony_ci 385317bbafSopenharmony_ci 395317bbafSopenharmony_ci 405317bbafSopenharmony_ci# ---- Demo suite ---- 415317bbafSopenharmony_ci 425317bbafSopenharmony_cidef do_basic_demo() -> None: 435317bbafSopenharmony_ci """Creates a single QR Code, then prints it to the console.""" 445317bbafSopenharmony_ci text = "Hello, world!" # User-supplied Unicode text 455317bbafSopenharmony_ci errcorlvl = QrCode.Ecc.LOW # Error correction level 465317bbafSopenharmony_ci 475317bbafSopenharmony_ci # Make and print the QR Code symbol 485317bbafSopenharmony_ci qr = QrCode.encode_text(text, errcorlvl) 495317bbafSopenharmony_ci print_qr(qr) 505317bbafSopenharmony_ci print(to_svg_str(qr, 4)) 515317bbafSopenharmony_ci 525317bbafSopenharmony_ci 535317bbafSopenharmony_cidef do_variety_demo() -> None: 545317bbafSopenharmony_ci """Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.""" 555317bbafSopenharmony_ci 565317bbafSopenharmony_ci # Numeric mode encoding (3.33 bits per digit) 575317bbafSopenharmony_ci qr = QrCode.encode_text("314159265358979323846264338327950288419716939937510", QrCode.Ecc.MEDIUM) 585317bbafSopenharmony_ci print_qr(qr) 595317bbafSopenharmony_ci 605317bbafSopenharmony_ci # Alphanumeric mode encoding (5.5 bits per character) 615317bbafSopenharmony_ci qr = QrCode.encode_text("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.HIGH) 625317bbafSopenharmony_ci print_qr(qr) 635317bbafSopenharmony_ci 645317bbafSopenharmony_ci # Unicode text as UTF-8 655317bbafSopenharmony_ci qr = QrCode.encode_text("\u3053\u3093\u306B\u3061\u0077\u0061\u3001\u4E16\u754C\uFF01\u0020\u03B1\u03B2\u03B3\u03B4", QrCode.Ecc.QUARTILE) 665317bbafSopenharmony_ci print_qr(qr) 675317bbafSopenharmony_ci 685317bbafSopenharmony_ci # Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland) 695317bbafSopenharmony_ci qr = QrCode.encode_text( 705317bbafSopenharmony_ci "Alice was beginning to get very tired of sitting by her sister on the bank, " 715317bbafSopenharmony_ci "and of having nothing to do: once or twice she had peeped into the book her sister was reading, " 725317bbafSopenharmony_ci "but it had no pictures or conversations in it, 'and what is the use of a book,' thought Alice " 735317bbafSopenharmony_ci "'without pictures or conversations?' So she was considering in her own mind (as well as she could, " 745317bbafSopenharmony_ci "for the hot day made her feel very sleepy and stupid), whether the pleasure of making a " 755317bbafSopenharmony_ci "daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly " 765317bbafSopenharmony_ci "a White Rabbit with pink eyes ran close by her.", QrCode.Ecc.HIGH) 775317bbafSopenharmony_ci print_qr(qr) 785317bbafSopenharmony_ci 795317bbafSopenharmony_ci 805317bbafSopenharmony_cidef do_segment_demo() -> None: 815317bbafSopenharmony_ci """Creates QR Codes with manually specified segments for better compactness.""" 825317bbafSopenharmony_ci 835317bbafSopenharmony_ci # Illustration "silver" 845317bbafSopenharmony_ci silver0 = "THE SQUARE ROOT OF 2 IS 1." 855317bbafSopenharmony_ci silver1 = "41421356237309504880168872420969807856967187537694807317667973799" 865317bbafSopenharmony_ci qr = QrCode.encode_text(silver0 + silver1, QrCode.Ecc.LOW) 875317bbafSopenharmony_ci print_qr(qr) 885317bbafSopenharmony_ci 895317bbafSopenharmony_ci segs = [ 905317bbafSopenharmony_ci QrSegment.make_alphanumeric(silver0), 915317bbafSopenharmony_ci QrSegment.make_numeric(silver1)] 925317bbafSopenharmony_ci qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) 935317bbafSopenharmony_ci print_qr(qr) 945317bbafSopenharmony_ci 955317bbafSopenharmony_ci # Illustration "golden" 965317bbafSopenharmony_ci golden0 = "Golden ratio \u03C6 = 1." 975317bbafSopenharmony_ci golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374" 985317bbafSopenharmony_ci golden2 = "......" 995317bbafSopenharmony_ci qr = QrCode.encode_text(golden0 + golden1 + golden2, QrCode.Ecc.LOW) 1005317bbafSopenharmony_ci print_qr(qr) 1015317bbafSopenharmony_ci 1025317bbafSopenharmony_ci segs = [ 1035317bbafSopenharmony_ci QrSegment.make_bytes(golden0.encode("UTF-8")), 1045317bbafSopenharmony_ci QrSegment.make_numeric(golden1), 1055317bbafSopenharmony_ci QrSegment.make_alphanumeric(golden2)] 1065317bbafSopenharmony_ci qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) 1075317bbafSopenharmony_ci print_qr(qr) 1085317bbafSopenharmony_ci 1095317bbafSopenharmony_ci # Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters 1105317bbafSopenharmony_ci madoka = "\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" 1115317bbafSopenharmony_ci qr = QrCode.encode_text(madoka, QrCode.Ecc.LOW) 1125317bbafSopenharmony_ci print_qr(qr) 1135317bbafSopenharmony_ci 1145317bbafSopenharmony_ci kanjicharbits = [ # Kanji mode encoding (13 bits per character) 1155317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1165317bbafSopenharmony_ci 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1175317bbafSopenharmony_ci 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1185317bbafSopenharmony_ci 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1195317bbafSopenharmony_ci 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1205317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1215317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1225317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1235317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1245317bbafSopenharmony_ci 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1255317bbafSopenharmony_ci 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1265317bbafSopenharmony_ci 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1275317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1285317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1295317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1305317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1315317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1325317bbafSopenharmony_ci 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1335317bbafSopenharmony_ci 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1345317bbafSopenharmony_ci 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1355317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1365317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1375317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1385317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1395317bbafSopenharmony_ci 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 1405317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1415317bbafSopenharmony_ci 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1425317bbafSopenharmony_ci 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1435317bbafSopenharmony_ci 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1445317bbafSopenharmony_ci ] 1455317bbafSopenharmony_ci segs = [QrSegment(QrSegment.Mode.KANJI, len(kanjicharbits) // 13, kanjicharbits)] 1465317bbafSopenharmony_ci qr = QrCode.encode_segments(segs, QrCode.Ecc.LOW) 1475317bbafSopenharmony_ci print_qr(qr) 1485317bbafSopenharmony_ci 1495317bbafSopenharmony_ci 1505317bbafSopenharmony_cidef do_mask_demo() -> None: 1515317bbafSopenharmony_ci """Creates QR Codes with the same size and contents but different mask patterns.""" 1525317bbafSopenharmony_ci 1535317bbafSopenharmony_ci # Project Nayuki URL 1545317bbafSopenharmony_ci segs = QrSegment.make_segments("https://www.nayuki.io/") 1555317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=-1)) # Automatic mask 1565317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.HIGH, mask=3)) # Force mask 3 1575317bbafSopenharmony_ci 1585317bbafSopenharmony_ci # Chinese text as UTF-8 1595317bbafSopenharmony_ci segs = QrSegment.make_segments( 1605317bbafSopenharmony_ci "\u7DAD\u57FA\u767E\u79D1\uFF08\u0057\u0069\u006B\u0069\u0070\u0065\u0064\u0069\u0061\uFF0C" 1615317bbafSopenharmony_ci "\u8046\u807D\u0069\u002F\u02CC\u0077\u026A\u006B\u1D7B\u02C8\u0070\u0069\u02D0\u0064\u0069" 1625317bbafSopenharmony_ci "\u002E\u0259\u002F\uFF09\u662F\u4E00\u500B\u81EA\u7531\u5167\u5BB9\u3001\u516C\u958B\u7DE8" 1635317bbafSopenharmony_ci "\u8F2F\u4E14\u591A\u8A9E\u8A00\u7684\u7DB2\u8DEF\u767E\u79D1\u5168\u66F8\u5354\u4F5C\u8A08" 1645317bbafSopenharmony_ci "\u756B") 1655317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=0)) # Force mask 0 1665317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=1)) # Force mask 1 1675317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=5)) # Force mask 5 1685317bbafSopenharmony_ci print_qr(QrCode.encode_segments(segs, QrCode.Ecc.MEDIUM, mask=7)) # Force mask 7 1695317bbafSopenharmony_ci 1705317bbafSopenharmony_ci 1715317bbafSopenharmony_ci 1725317bbafSopenharmony_ci# ---- Utilities ---- 1735317bbafSopenharmony_ci 1745317bbafSopenharmony_cidef to_svg_str(qr: QrCode, border: int) -> str: 1755317bbafSopenharmony_ci """Returns a string of SVG code for an image depicting the given QR Code, with the given number 1765317bbafSopenharmony_ci of border modules. The string always uses Unix newlines (\n), regardless of the platform.""" 1775317bbafSopenharmony_ci if border < 0: 1785317bbafSopenharmony_ci raise ValueError("Border must be non-negative") 1795317bbafSopenharmony_ci parts: List[str] = [] 1805317bbafSopenharmony_ci for y in range(qr.get_size()): 1815317bbafSopenharmony_ci for x in range(qr.get_size()): 1825317bbafSopenharmony_ci if qr.get_module(x, y): 1835317bbafSopenharmony_ci parts.append(f"M{x+border},{y+border}h1v1h-1z") 1845317bbafSopenharmony_ci return f"""<?xml version="1.0" encoding="UTF-8"?> 1855317bbafSopenharmony_ci<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 1865317bbafSopenharmony_ci<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 {qr.get_size()+border*2} {qr.get_size()+border*2}" stroke="none"> 1875317bbafSopenharmony_ci <rect width="100%" height="100%" fill="#FFFFFF"/> 1885317bbafSopenharmony_ci <path d="{" ".join(parts)}" fill="#000000"/> 1895317bbafSopenharmony_ci</svg> 1905317bbafSopenharmony_ci""" 1915317bbafSopenharmony_ci 1925317bbafSopenharmony_ci 1935317bbafSopenharmony_cidef print_qr(qrcode: QrCode) -> None: 1945317bbafSopenharmony_ci """Prints the given QrCode object to the console.""" 1955317bbafSopenharmony_ci border = 4 1965317bbafSopenharmony_ci for y in range(-border, qrcode.get_size() + border): 1975317bbafSopenharmony_ci for x in range(-border, qrcode.get_size() + border): 1985317bbafSopenharmony_ci print("\u2588 "[1 if qrcode.get_module(x,y) else 0] * 2, end="") 1995317bbafSopenharmony_ci print() 2005317bbafSopenharmony_ci print() 2015317bbafSopenharmony_ci 2025317bbafSopenharmony_ci 2035317bbafSopenharmony_ci# Run the main program 2045317bbafSopenharmony_ciif __name__ == "__main__": 2055317bbafSopenharmony_ci main() 206