文本大小写会改变二维码的大小。
Text case changes the size of QR codes

原始链接: https://www.johndcook.com/blog/2025/10/31/smaller-qr-codes/

这段文字展示了大小写对使用Python的`qrcode`库生成二维码的影响。从相同句子生成二维码,混合大小写和全部大写会产生不同的像素密度。 这种差异源于二维码处理字符数据的方式。大写字母和数字字符属于一个44个字符的字母数字集合,允许更高效的二进制编码(每个字符约5.5位)。混合大小写,包括小写字母,默认使用效率较低的8位编码。 在示例中,大写二维码(29x29像素)所需的像素比混合大小写版本(33x33像素)少约30%。这个原理适用于加密货币地址:Bech32使用单大小写字母表,生成比使用混合大小写字母表的Base58更小的二维码,用于相同的数据。最终,有限且一致的字符集可以优化二维码的大小。

## QR码尺寸与编码技巧 这次Hacker News讨论的核心是优化QR码尺寸,尤其是在手工制作杯垫或品牌推广等场景下。一个关键点是,使用字母数字编码(使用大写字母)可以生成比标准ASCII编码更小的QR码,因为其字符集更有效率。 用户分享了最大化这种效果的经验——有人雕刻了一个用于rickroll的QR码,利用不区分大小写的DNS来适应更长的URL。 还有人指出潜在的陷阱,例如“免费”的QR码生成器会控制你的链接,以及LinkedIn拒绝扫描包含大写URL的QR码(可能是一种品牌决策)。 对话还深入到技术细节:正确的字母数字字符集(包括`/`的45个字符)、URL的大小写敏感性,以及在单个QR码内混合编码方案的可能性。 许多评论者强调理解QR码纠错和编码的重要性,以便获得最佳结果,并分享了进一步探索的资源。 最终,这个帖子展示了对“破解”QR码的迷人细节和奉献精神,既出于实用目的,也出于美观考虑。
相关文章

原文

Let’s make a QR code out of a sentence two ways: mixed case and upper case. We’ll use Python with the qrcode library.

>>> import qrcode
>>> s = "The quick brown fox jumps over the lazy dog."
>>> qrcode.make(s).save("mixed.png")
>>> qrcode.make(s.upper()).save("upper.png")

Here are the mixed case and upper case QR codes.

The QR code creation algorithm interprets the mixed case sentence as binary data but it interprets the upper case sentence as alphanumeric data.

Alphanumeric data, in the context of QR codes, comes from the following alphabet of 44 characters:

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-.:

Since 44² = 1936 < 2048 = 211 two alphanumeric characters can be encoded in 11 bits. If text contains a single character outside this alphabet, such as a lower case letter, then the text is encoded as ISO/IEC 8859-1 using 8 bits per character.

Switching from mixed-case text to upper case text reduces the bits per character from 8 to 5.5, and so we should expect the resulting QR code to require about 30% fewer pixels. In the example above we go from a 33 × 33 grid down to a 29 × 29 grid, from 1089 pixels to 841.

Application to Bitcoin addressess

Bech32 encoding uses an alphabet of 32 characters while Base58 encoding uses an alphabet of 58 characters, and so the former needs about 17% more characters to represent the same data. But Bech32 uses a monocase alphabet, and base 58 does not, and so Bech32 encoding requires fewer QR code pixels to represent the same data as Base58 encoding.

(Bech32 encoding uses a lower case alphabet, but the letters are converted to upper case before creating QR codes.)

Related posts

联系我们 contact @ memedata.com