Documentation
¶
Overview ¶
Package qr encodes data as QR codes and renders them as SVG or PNG.
It has no dependencies outside the standard library, and the whole encoder — bitstream, Reed-Solomon parity over GF(256), matrix layout and mask selection — is implemented here rather than bound to a C library.
Scope is deliberately narrow: byte mode, error correction level M, versions 1-20 (up to 666 bytes). Byte mode encodes any payload, so the alphanumeric and kanji modes only buy density for restricted character sets; leaving them out, along with versions 21-40, keeps the surface small enough to test exhaustively. Every version this package claims to support is verified at capacity by its tests.
Rendering is black on white and ignores any surrounding theme. A QR code is read by a camera, not a person, and light-on-dark defeats most scanners.
Index ¶
Constants ¶
const QuietZone = 4
QuietZone is the light margin the specification requires around a symbol. Without it many scanners will not lock on.
Variables ¶
var ErrTooLong = errors.New("qr: data too long")
ErrTooLong means the data does not fit in a version-20 symbol at level M.
Functions ¶
func Render ¶
Render encodes data and returns an <img> element carrying the QR code as an inline PNG, or an empty string if the data cannot be encoded. Callers that can proceed without a QR code use this to avoid failing a whole page over one element.
The image is embedded as a data URI rather than referenced by URL: nothing is fetched at render time, so the payload is never disclosed to a third party and the markup works offline. A test asserts the src is nothing but base64.
PNG rather than SVG for clients where SVG is unavailable or disabled. See Code.SVG, which is the better choice when it is.
Types ¶
type Code ¶
type Code struct {
// Version is 1-20.
Version int
// Size is the side length in modules, 4*Version + 17.
Size int
// contains filtered or unexported fields
}
Code is an encoded QR symbol.
func (*Code) Image ¶
Image renders the code as a paletted bitmap at scale pixels per module, including the quiet zone the specification requires around the symbol.
func (*Code) Module ¶
Module reports whether the module at (x, y) is dark. Out-of-range coordinates are light, which is what the surrounding quiet zone is.
func (*Code) SVG ¶
SVG renders the code as a standalone <svg> element for inlining into a page.
The markup is self-contained: no <script>, no <use>, no external href or url() reference, so it renders identically with scripting and remote content disabled, and it cannot phone home. A test asserts this.
Resolution-independent and typically smaller than the equivalent PNG, so this is the better choice wherever SVG is available. Where it is not — some email clients, and browsers configured with svg.disabled — use Render, which emits a PNG.
Colors are hardcoded black on white rather than inheriting the page theme. A QR code is read by a camera, not a person, and light-on-dark defeats most scanners — so this is one element that must ignore dark mode.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
qrdump
command
Command qrdump writes QR codes as PNGs so the encoder's output can be checked against an independent decoder — a phone camera, or any third-party library.
|
Command qrdump writes QR codes as PNGs so the encoder's output can be checked against an independent decoder — a phone camera, or any third-party library. |