parent
22fac31bdf
commit
93237ecbd6
@ -0,0 +1,201 @@
|
||||
#
|
||||
# QR Code generator demo (Python)
|
||||
#
|
||||
# Run this command-line program with no arguments. The program computes a bunch of demonstration
|
||||
# QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
from typing import List
|
||||
from qrcodegen import QrCode, QrSegment
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""The main application program."""
|
||||
do_basic_demo()
|
||||
|
||||
|
||||
|
||||
# ---- Demo suite ----
|
||||
|
||||
def do_basic_demo() -> None:
|
||||
"""Creates a single QR Code, then prints it to the console."""
|
||||
text = "Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!Hello, world!" # User-supplied Unicode text
|
||||
errcorlvl = QrCode.Ecc.LOW # Error correction level
|
||||
|
||||
# Make and print the QR Code symbol
|
||||
qr = QrCode.encode_text(text, errcorlvl)
|
||||
print_qr(qr)
|
||||
|
||||
|
||||
def do_variety_demo() -> None:
|
||||
"""Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console."""
|
||||
|
||||
# Numeric mode encoding (3.33 bits per digit)
|
||||
qr = QrCode.encode_text("314159265358979323846264338327950288419716939937510", QrCode.Ecc.MEDIUM)
|
||||
print_qr(qr)
|
||||
|
||||
# Alphanumeric mode encoding (5.5 bits per character)
|
||||
qr = QrCode.encode_text("DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/", QrCode.Ecc.HIGH)
|
||||
print_qr(qr)
|
||||
|
||||
# Unicode text as UTF-8
|
||||
qr = QrCode.encode_text("\u3053\u3093\u306B\u3061\u0077\u0061\u3001\u4E16\u754C\uFF01\u0020\u03B1\u03B2\u03B3\u03B4", QrCode.Ecc.QUARTILE)
|
||||
print_qr(qr)
|
||||
|
||||
# Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
|
||||
qr = QrCode.encode_text(
|
||||
"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)
|
||||
print_qr(qr)
|
||||
|
||||
|
||||
def do_segment_demo() -> None:
|
||||
"""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 = "Golden ratio \u03C6 = 1."
|
||||
golden1 = "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374"
|
||||
golden2 = "......"
|
||||
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 = "\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_mask_demo() -> None:
|
||||
"""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(
|
||||
"\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
|
||||
|
||||
|
||||
|
||||
# ---- Utilities ----
|
||||
|
||||
def to_svg_str(qr: QrCode, border: int) -> str:
|
||||
"""Returns a string of SVG code for an image depicting the given QR Code, with the given number
|
||||
of border modules. The string always uses Unix newlines (\n), regardless of the platform."""
|
||||
if border < 0:
|
||||
raise ValueError("Border must be non-negative")
|
||||
parts: List[str] = []
|
||||
for y in range(qr.get_size()):
|
||||
for x in range(qr.get_size()):
|
||||
if qr.get_module(x, y):
|
||||
parts.append(f"M{x+border},{y+border}h1v1h-1z")
|
||||
return f"""<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
|
||||
<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">
|
||||
<rect width="100%" height="100%" fill="#FFFFFF"/>
|
||||
<path d="{" ".join(parts)}" fill="#000000"/>
|
||||
</svg>
|
||||
"""
|
||||
|
||||
|
||||
def print_qr(qrcode: QrCode) -> None:
|
||||
"""Prints the given QrCode object to the console."""
|
||||
border = 4
|
||||
for y in range(-border, qrcode.get_size() + border):
|
||||
for x in range(-border, qrcode.get_size() + border):
|
||||
print("\u2588 "[1 if qrcode.get_module(x,y) else 0] * 2, end="")
|
||||
print()
|
||||
print()
|
||||
|
||||
|
||||
# Run the main program
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -0,0 +1,13 @@
|
||||
QR Code generator library - SCHEME R7RS-small
|
||||
=============================================
|
||||
|
||||
|
||||
Introduction
|
||||
------------
|
||||
|
||||
This project aims to be a simple R7RS-small implementation of QR code generator.
|
||||
|
||||
It is tested using the interpreter TR7 (https://gitlab.com/jobol/tr7)
|
||||
|
||||
|
||||
Improvements for other implementations are very welcome
|
@ -0,0 +1,68 @@
|
||||
;
|
||||
; QR Code generator library (Scheme R7RS)
|
||||
;
|
||||
; Copyright (c) José Bollo. (MIT License)
|
||||
; 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.
|
||||
;
|
||||
|
||||
(cond-expand
|
||||
((library (srfi 151))
|
||||
(import (only (srfi 151) bit-set? bit-xor bit-or)))
|
||||
(else
|
||||
; ad-hoc implementation without multiply or divide
|
||||
|
||||
(define (bit-set? idx n)
|
||||
(define (cuth mask a)
|
||||
(if (> mask a)
|
||||
a
|
||||
(let ((na (cuth (+ mask mask) a)))
|
||||
(if (> mask na) na (- na mask)))))
|
||||
(define (bset idx mask)
|
||||
(if (positive? idx)
|
||||
(bset (- idx 1) (+ mask mask))
|
||||
(>= (cuth (+ mask mask) n) mask)))
|
||||
(bset idx 1))
|
||||
|
||||
(define (bit-xor a b)
|
||||
(define (bxor mask a b)
|
||||
(if (or (and (< a mask) (< b mask)) (zero? mask))
|
||||
(values 0 a b)
|
||||
(let-values (((r na nb) (bxor (+ mask mask) a b)))
|
||||
(if (< na mask)
|
||||
(if (< nb mask)
|
||||
(values r na nb)
|
||||
(values (+ r mask) na (- nb mask)))
|
||||
(if (< nb mask)
|
||||
(values (+ r mask) (- na mask) nb)
|
||||
(values r (- na mask) (- nb mask)))))))
|
||||
(let-values (((r x y) (bxor 1 a b))) r))
|
||||
|
||||
(define (bit-or a b)
|
||||
(define (bor mask a b)
|
||||
(if (or (and (< a mask) (< b mask)) (zero? mask))
|
||||
(values 0 a b)
|
||||
(let-values (((r na nb) (bor (+ mask mask) a b)))
|
||||
(if (< na mask)
|
||||
(if (< nb mask)
|
||||
(values r na nb)
|
||||
(values (+ r mask) na (- nb mask)))
|
||||
(values (+ r mask) (- na mask) (if (< nb mask) nb (- nb mask)))))))
|
||||
(let-values (((r x y) (bor 1 a b))) r))))
|
||||
|
@ -0,0 +1,197 @@
|
||||
;
|
||||
; QR Code generator demo (Python)
|
||||
;
|
||||
; Run this command-line program with no arguments. The program computes a bunch of demonstration
|
||||
; QR Codes and prints them to the console. Also, the SVG code for one QR Code is printed as a sample.
|
||||
;
|
||||
; Copyright (c) José Bollo. (MIT License)
|
||||
; 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.
|
||||
;
|
||||
|
||||
;(import (prefix (QR-code encode) qr-))
|
||||
(import (prefix (qrcodegen) qr-))
|
||||
|
||||
; ---- Utilities ----
|
||||
|
||||
(define (to-svg-str qr border)
|
||||
;;; Returns a string of SVG code for an image depicting the given QR Code, with the given number
|
||||
;;; of border modules. The string always uses Unix newlines (\n), regardless of the platform.
|
||||
(if (negative? border)
|
||||
(error "Border must be non-negative"))
|
||||
|
||||
(let* ((size (qr-get-size qr))
|
||||
(parts (let loop ((r '())(y (- size 1))(x (- size 1)))
|
||||
(if (negative? y)
|
||||
r
|
||||
(if (negative? x)
|
||||
(loop r (- y 1) (- size 1))
|
||||
(let ((n (if (qr-get-module qr x y)
|
||||
`(" " "M" ,(number->string (+ x border)) "," ,(number->string (+ y border)) "h1v1h-1z" . ,r)
|
||||
r)))
|
||||
(loop n y (- x 1))))))))
|
||||
(apply string-append `(
|
||||
"<?xml version='1.0' encoding='UTF-8'?>
|
||||
<!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
|
||||
<svg xmlns='http://www.w3.org/2000/svg' version='1.1' viewBox='0 0 " ,(number->string (+ size border border)) " " ,(number->string (+ size border border)) "' stroke='none'>
|
||||
<rect width='100%' height='100%' fill='#FFFFFF'/>
|
||||
<path d='" ,@parts "' fill='#000000'/>
|
||||
</svg>"))))
|
||||
|
||||
|
||||
(define (print-qr qr)
|
||||
;;; Prints the given QrCode object to the console.
|
||||
(let* ((border 4)
|
||||
(low (- border))
|
||||
(high (+ border (qr-get-size qr))))
|
||||
(do ((y low (+ y 1)))
|
||||
((>= y high))
|
||||
(do ((x low (+ x 1)))
|
||||
((>= x high))
|
||||
(let ((c (if (qr-get-module qr x y) #\space #\x2588)))
|
||||
(write-char c)
|
||||
(write-char c)))
|
||||
(newline))
|
||||
(newline)))
|
||||
|
||||
; ---- Demos ----
|
||||
|
||||
(define (basic-demo)
|
||||
;;; Creates a single QR Code, then prints it to the console.
|
||||
(define text "Hello, world!") ; User-supplied Unicode text
|
||||
(define errcorlvl qr-ecc-LOW) ; Error correction level
|
||||
|
||||
; Make and print the QR Code symbol
|
||||
(define qr (qr-encode-text text errcorlvl))
|
||||
(print-qr qr)
|
||||
(display (to-svg-str qr 4))
|
||||
(newline))
|
||||
|
||||
|
||||
(define (variety-demo)
|
||||
;;; Creates a variety of QR Codes that exercise different features of the library, and prints each one to the console.
|
||||
|
||||
; Numeric mode encoding (3.33 bits per digit)
|
||||
(print-qr (qr-encode-text "314159265358979323846264338327950288419716939937510" qr-ecc-MEDIUM))
|
||||
|
||||
; Alphanumeric mode encoding (5.5 bits per character)
|
||||
(print-qr (qr-encode-text "DOLLAR-AMOUNT:$39.87 PERCENTAGE:100.00% OPERATIONS:+-*/" qr-ecc-HIGH))
|
||||
|
||||
; Unicode text as UTF-8
|
||||
(print-qr (qr-encode-text "\x3053;\x3093;\x306B;\x3061;\x77;\x61;\x3001;\x4E16;\x754C;\xFF01;\x20;\x3B1;\x3B2;\x3B3;\x3B4;" qr-ecc-QUARTILE))
|
||||
|
||||
; Moderately large QR Code using longer text (from Lewis Carroll's Alice in Wonderland)
|
||||
(print-qr (qr-encode-text
|
||||
"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." qr-ecc-HIGH)))
|
||||
|
||||
|
||||
(define (segment-demo)
|
||||
;;; Creates QR Codes with manually specified segments for better compactness.
|
||||
|
||||
; Illustration "silver"
|
||||
(let ((silver0 "THE SQUARE ROOT OF 2 IS 1.")
|
||||
(silver1 "41421356237309504880168872420969807856967187537694807317667973799"))
|
||||
(print-qr (qr-encode-text (string-append silver0 silver1) qr-ecc-LOW))
|
||||
(let ((seg-silver0 (qr-make-segment-alpha-numeric silver0))
|
||||
(seg-silver1 (qr-make-segment-numeric silver1)))
|
||||
(print-qr (qr-encode-segments (list seg-silver0 seg-silver1) qr-ecc-LOW))))
|
||||
|
||||
; Illustration "golden"
|
||||
(let ((golden0 "Golden ratio \x3C6; = 1.")
|
||||
(golden1 "6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374")
|
||||
(golden2 "......"))
|
||||
(print-qr (qr-encode-text (string-append golden0 golden1 golden2) qr-ecc-LOW))
|
||||
(let ((seg-golden0 (qr-make-segment-bytes golden0))
|
||||
(seg-golden1 (qr-make-segment-numeric golden1))
|
||||
(seg-golden2 (qr-make-segment-alpha-numeric golden2)))
|
||||
(print-qr (qr-encode-segments (list seg-golden0 seg-golden1 seg-golden2) qr-ecc-LOW))))
|
||||
|
||||
; Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
|
||||
(let ((madoka "\x300C;\x9B54;\x6CD5;\x5C11;\x5973;\x307E;\x3069;\x304B;\x2606;\x30DE;\x30AE;\x30AB;\x300D;\x3063;\x3066;\x3001;\x3000;\x418;\x410;\x418;\x3000;\xFF44;\xFF45;\xFF53;\xFF55;\x3000;\x3BA;\x3B1;\xFF1F;"))
|
||||
(print-qr (qr-encode-text madoka qr-ecc-LOW)))
|
||||
|
||||
; Kanji mode encoding (13 bits per character)
|
||||
(let* ((kanjicharbits #(
|
||||
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))
|
||||
(kanjiseg (qr-make-segment qr-mode-KANJI (quotient (vector-length kanjicharbits) 13) kanjicharbits)))
|
||||
(print-qr (qr-encode-segments (list kanjiseg) qr-ecc-LOW))))
|
||||
|
||||
(define (mask-demo)
|
||||
;;; Creates QR Codes with the same size and contents but different mask patterns.
|
||||
|
||||
; Project Nayuki URL
|
||||
(let ((segs (list (qr-make-segment-text "https://www.nayuki.io/"))))
|
||||
(print-qr (qr-encode-segments segs qr-ecc-HIGH 'mask -1)) ; Automatic mask
|
||||
(print-qr (qr-encode-segments segs qr-ecc-HIGH 'mask 3))) ; Force mask 3
|
||||
|
||||
; Chinese text as UTF-8
|
||||
(let ((segs (list (qr-make-segment-text "\
|
||||
\x7DAD;\x57FA;\x767E;\x79D1;\xFF08;\x0057;\x0069;\x006B;\x0069;\x0070;\x0065;\x0064;\x0069;\x0061;\xFF0C;\
|
||||
\x8046;\x807D;\x0069;\x002F;\x02CC;\x0077;\x026A;\x006B;\x1D7B;\x02C8;\x0070;\x0069;\x02D0;\x0064;\x0069;\
|
||||
\x002E;\x0259;\x002F;\xFF09;\x662F;\x4E00;\x500B;\x81EA;\x7531;\x5167;\x5BB9;\x3001;\x516C;\x958B;\x7DE8;\
|
||||
\x8F2F;\x4E14;\x591A;\x8A9E;\x8A00;\x7684;\x7DB2;\x8DEF;\x767E;\x79D1;\x5168;\x66F8;\x5354;\x4F5C;\x8A08;\
|
||||
\x756B;"))))
|
||||
(print-qr (qr-encode-segments segs qr-ecc-MEDIUM 'mask 0)) ; Force mask 0
|
||||
(print-qr (qr-encode-segments segs qr-ecc-MEDIUM 'mask 1)) ; Force mask 1
|
||||
(print-qr (qr-encode-segments segs qr-ecc-MEDIUM 'mask 5)) ; Force mask 5
|
||||
(print-qr (qr-encode-segments segs qr-ecc-MEDIUM 'mask 7)))) ; Force mask 7
|
||||
|
||||
|
||||
;;; The main application program.
|
||||
|
||||
(basic-demo)
|
||||
(variety-demo)
|
||||
(segment-demo)
|
||||
(mask-demo)
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,58 @@
|
||||
;
|
||||
; QR Code generator library (Scheme R7RS)
|
||||
;
|
||||
; Copyright (c) José Bollo. (MIT License)
|
||||
; 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.
|
||||
;
|
||||
|
||||
;# ---- QR Code library ----
|
||||
|
||||
(define-library (qrcodegen)
|
||||
|
||||
(export
|
||||
ecc?
|
||||
ecc-ordinal
|
||||
ecc-LOW
|
||||
ecc-MEDIUM
|
||||
ecc-QUARTILE
|
||||
ecc-HIGH
|
||||
encode-text
|
||||
encode-binary
|
||||
encode-segments
|
||||
(rename QR-code-size get-size)
|
||||
(rename QR-code-module get-module)
|
||||
make-segment
|
||||
make-segment-alpha-numeric
|
||||
make-segment-bytes
|
||||
make-segment-numeric
|
||||
make-segment-text
|
||||
make-segment-eci
|
||||
MAX-VERSION
|
||||
MIN-VERSION
|
||||
mode?
|
||||
mode-ALPHANUMERIC
|
||||
mode-BYTE
|
||||
mode-ECI
|
||||
mode-KANJI
|
||||
mode-NUMERIC
|
||||
)
|
||||
|
||||
(include "qrcodegen.scm"))
|
||||
|
@ -0,0 +1,85 @@
|
||||
;
|
||||
; QR Code generator library (Scheme R7RS)
|
||||
;
|
||||
; Copyright (c) José Bollo. (MIT License)
|
||||
; 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.
|
||||
;
|
||||
|
||||
(define (reed-solomon-multiply x y)
|
||||
; Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
|
||||
; are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
|
||||
|
||||
(unless (and (<= 0 x 255) (<= 0 y 255))
|
||||
(error "Byte out of range"))
|
||||
|
||||
; Russian peasant multiplication
|
||||
(do ((i 7 (- i 1))
|
||||
(z 0 (let ((nz (bit-xor (* z 2) (* (quotient z 128) #x11D))))
|
||||
(if (bit-set? i y) (bit-xor nz x) nz))))
|
||||
((negative? i) z)))
|
||||
|
||||
(define (reed-solomon-compute-divisor degree)
|
||||
; Returns a Reed-Solomon ECC generator polynomial for the given degree. This could be
|
||||
; implemented as a lookup table over all possible parameter values, instead of as an algorithm.
|
||||
|
||||
(unless (<= 1 degree 255)
|
||||
(error "Degree out of range"))
|
||||
|
||||
; Polynomial coefficients are stored from highest to lowest power, excluding the leading term which is always 1.
|
||||
; For example the polynomial x^3 + 255x^2 + 8x + 93 is stored as the uint8 array [255, 8, 93].
|
||||
; Start off with the monomial x^0
|
||||
(define result (make-bytevector degree 0))
|
||||
(bytevector-u8-set! result (- degree 1) 1)
|
||||
|
||||
; Compute the product polynomial (x - r^0) * (x - r^1) * (x - r^2) * ... * (x - r^{degree-1}),
|
||||
; and drop the highest monomial term which is always 1x^degree.
|
||||
; Note that r = 0x02, which is a generator element of this field GF(2^8/0x11D).
|
||||
(do ((root 1 (reed-solomon-multiply root 2))
|
||||
(i 0 (+ i 1)))
|
||||
((= i degree))
|
||||
(do ((j 0 (+ j 1)))
|
||||
((= j degree))
|
||||
(let* ((rj0 (bytevector-u8-ref result j))
|
||||
(rj1 (reed-solomon-multiply rj0 root))
|
||||
(nxj (+ j 1))
|
||||
(rj2 (if (>= nxj degree)
|
||||
rj1
|
||||
(bit-xor rj1 (bytevector-u8-ref result nxj)))))
|
||||
(bytevector-u8-set! result j rj2))))
|
||||
result)
|
||||
|
||||
(define (reed-solomon-compute-remainder data divisor)
|
||||
; Returns the Reed-Solomon error correction codeword for the given data and divisor polynomials.
|
||||
|
||||
(define result (make-bytevector (bytevector-length divisor) 0))
|
||||
(do ((i 0 (+ i 1)))
|
||||
((>= i (bytevector-length data)))
|
||||
(let ((factor (bit-xor (bytevector-u8-ref data i) (bytevector-u8-ref result 0))))
|
||||
(do ((u 0 v)(v 1 (+ v 1)))
|
||||
((>= v (bytevector-length result)) (bytevector-u8-set! result u 0))
|
||||
(bytevector-u8-set! result u (bytevector-u8-ref result v)))
|
||||
(do ((u 0 (+ u 1)))
|
||||
((>= u (bytevector-length divisor)))
|
||||
(bytevector-u8-set! result u
|
||||
(bit-xor
|
||||
(bytevector-u8-ref result u)
|
||||
(reed-solomon-multiply factor (bytevector-u8-ref divisor u)))))))
|
||||
result)
|
||||
|
Loading…
Reference in new issue