Python 中的 QR 码

2025年1月12日 | 阅读 19 分钟

二维码是一种条形码,易于数字设备读取,并以像素串的形式承载数据,这些像素以方形网格排列。由于许多智能手机都内置了二维码扫描仪,因此二维码常用于营销和推广活动。它们被广泛用于跟踪供应链中产品的相关数据。在当前监控新冠病毒暴露和阻止病毒传播的倡议中,它们被证明是必不可少的。

1994年,日本丰田子公司Denso Wave公司创建了第一项二维码技术。当时,他们需要一个更精确的系统来跟踪汽车和零部件的生产过程。为此,他们开发了一种能够读取日语汉字、假名和字母数字字符的特殊条形码。

传统条形码只能从上到下读取。因此,它们只能存储有限的数据,通常是字母数字格式。然而,二维码有两种读取方式:从上到下和从右到左。因此,它可以存储更多信息。二维码的设计团队旨在使其易于读取,以便工作人员不必浪费时间调整到正确的角度。他们还希望它具有独特的外观,使其脱颖而出。因此,他们最终选择了经久不衰的方形设计。

Denso Wave 公司宣布将放弃行使其专利权,使二维码免费可用。因此,任何人都可以创建和使用二维码。尽管这一概念花了一些时间才被接受,但第一批支持二维码的手机于2002年在日本推出。智能手机的普及促使使用二维码的企业数量不断增加。

传统上,产品包装上或宣传其优势的广告中可用的空间量限制了可以传输的关于商品或服务的信息量。如果客户想要更多关于产品的信息,例如其可用性、价格或功能,他们需要找到销售人员或获取额外的文件。

条形码经常出现在产品包装的背面,并使用不同长度的平行线组合来传输数据。这些代码可以通过配备光学扫描仪的设备读取。美国铁路公司在20世纪60年代首次使用条形码来跟踪货物和设备,彻底改变了企业处理库存和价格的方式。1974年,二维传统条形码在美国零售商店中得到了广泛应用。

如今,从集装箱到医院腕带和员工身份证,各种物品上都可以看到条形码。

静态二维码(如上图所示)经常用于分发公开信息。它们经常出现在报纸和杂志的广告中,以及海报和广告牌上。代码的创建者可以跟踪其代码被扫描的频率以及代码中包含的行动号召是否被遵循。动态二维码(也称为唯一二维码)具有更多功能。它们可以随时编辑,甚至可以定制以销售给特定个人。这些代码包含更多元数据以帮助跟踪。

与商店的条形码类似,二维码的功能也类似。每个二维码都由代表各种数据的黑方块和点组成。当条形码被扫描时,其独特的设计会转换为人类可读的数据。这个过程瞬间完成。

虽然现在大多数人使用智能手机扫描二维码,但用户必须有一个二维码阅读器或扫描仪才能获取信息。如果您的手机缺少此功能,有许多免费的二维码扫描应用程序,如NeoReader和QuickMark Barcode Scanner。动态二维码的一个重要优势是能够收集扫描指标。用户个人信息无法访问,但您可以查看每次扫描的日期、地点和使用的扫描设备。作为营销人员,我们深知这些数据对于衡量营销活动成功与否的重要性。

代码

在主函数中,用户会看到一个菜单,其中提供了多种选项供用户选择。在菜单中呈现给用户的各种选项包括:输入将用于生成二维码的文本数据、输入将保存已生成二维码的文件名、生成带有输入数据并存储在指定文件名的二维码、输入需要读取的二维码文件名、从指定的二维码文件中读取数据、打印从二维码文件中读取的数据,以及最后一个选项是停止打印此菜单并退出代码执行。根据用户选择的选项类型,会通过在主函数中提前创建的对象来调用特定类的相应函数。如果用户调用的函数需要用户输入,它会要求用户提供适当的输入,然后对用户提供的输入执行各种必要的操作。

代码

输出

Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
1
Enter the text from which you want to generate the QR code:
Save this data in a QR code file
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
2
Enter the name of the file in which the generated QR code needs to be stored.
The file needs to be in .png format.
qrcodefile1.png
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
3
QR code generated successfully and saved in qrcodefile1.png file.
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
4
Enter the name of the file in which the QR code is saved.
The file should be a .png file.
qrcodefile1.png
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
5
QR code in the file named qrcodefile1.png read successfully.
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
6
The data which is read from the QR code file name [qrcodefile1.png] is [Save this data in a QR code file]
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
1
Enter the text from which you want to generate the QR code:
Data with speci@l ch@r$
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
2
Enter the name of the file in which the generated QR code needs to be stored.
The file needs to be in .png format.
qrfilewithspeicalchars.png
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
3
QR code generated successfully and saved in qrfilewithspeicalchars.png file.
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
4
Enter the name of the file in which the QR code is saved.
The file should be a .png file.
qrfilewithspeicalchars.png
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
5
QR code in the file named qrfilewithspeicalchars.png read successfully.
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
6
The data which is read from the QR code file name [qrfilewithspeicalchars.png] is [Data with speci@l ch@r$]
To proceed with the printing of the menu which was getting displayed enter [Y] else [N]
y


Do select any of these options:
To enter the text data from which the QR code will be generated.
To enter the name of the file in which the genrated QR code will be saved.
To generate a QR with the entered data which will be stored in the specified filename.
To enter the name of the QR code file which needs to be read.
To read the data from the specified QR code file.
To print the data which has been read from the QR code file.
To exit from the code execution.
7

在上面编写的代码中,主函数会呈现一个菜单,其中提供了多种选项供用户选择。在菜单中呈现给用户的各种选项包括:输入将用于生成二维码的文本数据、输入将保存已生成二维码的文件名、生成带有输入数据并存储在指定文件名的二维码、输入需要读取的二维码文件名、从指定的二维码文件中读取数据、打印从二维码文件中读取的数据,以及最后一个选项是停止打印此菜单并退出代码执行。根据用户选择的选项类型,会通过在主函数中提前创建的对象来调用特定类的相应函数。如果用户调用的函数需要用户输入,它会要求用户提供适当的输入,然后对用户提供的输入执行各种必要的操作。

二维码的优点

  • 快速且无差错:如果没有二维码,消费者只能通过URL访问在线信息。然而,在智能手机上输入URL既耗时又麻烦。客户还容易出现输入错误。相比之下,扫描二维码的过程更快、更无差错。2015年,总部位于芝加哥的公司Wintrust Financial和技术公司FIS推出了约200台ATM机。这些ATM机配备了使用二维码处理无卡交易的功能。通过Wintrust应用程序,用户可以通过扫描ATM屏幕上的二维码轻松进行取款。据Wintrust和FIS称,使用二维码进行无卡现金取款仅需8秒,而传统取款需要47秒。这表明二维码的可用性有多么快捷方便。客户只需使用Wintrust智能手机扫描ATM屏幕上的二维码即可进行现金取款。据Wintrust和FIS称,使用二维码进行无卡现金取款仅需8秒,而传统取款需要47秒。这表明二维码对用户来说更快捷、更实用。这是二维码在中国流行的主要原因。
  • 信息量更大:与在线营销相比,印刷营销存在缺点。尤其是在房地产行业。您在报纸广告、产品包装和宣传册中只能说这么多,对吧?错。通过使用二维码,您可以为他们提供访问无限信息的方式。Maggi面条是印度雀巢公司的一款受欢迎的产品,于2015年被禁售。原因是当局声称其含铅量非法。尽管禁令很快被撤销,但雀巢知道人们的信任度已经下降。为了赢回客户的信任,雀巢采取了多种行动来证明其遵守法律和客户健康。为此,他们向客户提供了详细的成分列表以及其他关键信息。这可能与健康、环境、社会或烹饪有关。当然,不可能在包装上发布任何内容。因此,该公司添加了一个二维码,将客户引导至包含所有这些信息的移动网站。
  • 与丰富内容互动:文字很有用。一张图片值千言。然而,一个视频包含成千上万张图片。此外,音频更实用。不幸的是,除非包含二维码,否则您无法通过印刷广告分发这些类型的丰富内容。营销人员可以通过使用二维码在印刷广告中传达丰富内容。因此,他们将看到更多的受众参与。例如,Chef's Basket的一款意大利面产品盒上贴有二维码。扫描二维码后,客户会进入一个包含该意大利面食谱的视频。
  • 可操作:即使印刷媒体在品牌推广方面仍然有效,与户外或数字营销相比,它也缺乏互动功能。但使用二维码可以实现这一点。您可以通过使用二维码使您的促销活动更具互动性,并获得目标市场的反馈。您可以举办竞赛,鼓励人们注册某项服务或活动,收集反馈,或者让您的客户直接从报纸广告或传单上购买。一个很好的例子是2010年在纽约市中央公园举行的“世界公园”二维码活动。公园内的各个地点都有带有二维码的展板。扫描二维码后,游客会参与关于公园历史、流行文化、艺术和音乐的测验。
  • 可追踪:如果您从事营销工作,您就知道分析的重要性。它使您能够了解很多关于您的受众的信息。因此,帮助您识别您的优势和劣势。然而,与数字媒体不同,印刷媒体本身没有跟踪功能。这时二维码就派上用场了。通过监控扫描活动的能力,二维码允许您评估印刷媒体营销活动的有效性。例如,有多少人扫描了它,以及何时何地扫描了它。它还提供了一个称为事件跟踪的附加功能。它描述了用户扫描二维码后对其中包含的信息的响应。例如,可能是点击
  • 在线和离线媒体之间的桥梁:传单、名片、广告牌、海报、宣传册和标牌等离线媒体都可以通过二维码轻松更新。它们更快、更简单,并且不太可能导致最终用户出错。潜在客户可以扫描您传单或消息上的独特代码,并响应您的网站或号召性用语。
  • 客户友好:首先,这无疑使他们的生活更简单。人们现在可以通过快速扫描二维码来致电您的公司或填写您网站上的联系表单,而无需记住以后访问网站或记下电话号码和在线地址。对于 B2B 公司来说,这一点尤其重要,因为您正在与可能时间紧迫的潜在客户打交道。人们越能快速轻松地联系您以了解您销售的产品,他们转化的可能性就越大。这些只是二维码为您的 B2B 网站提供的众多好处中的一部分。它们可以以多种方式使用。您对此非常有把握。
  • 新技术:每个人都喜欢在线互动,因为世界已经数字化。如果您的公司拥有数字形象,那么它显然是具有前瞻性并且先进的。二维码的另一个好处是,它们使您能够成功地传播您的信息并增强您公司的互动品牌形象。
  • 节省纸张:通过使用二维码,可以完全数字化。您有机会通过减少纸张和其他材料的消耗来保护环境。借助自定义二维码,您可以减少在用于营销或宣传的纸张和其他材料上的投资。通过提供数字二维码,您可以让他们充分利用手持数字屏幕。您只需为他们提供最佳信息,而无需在纸质物品上花费金钱。

二维码的缺点

  • 隐私问题:使用二维码,您可以访问额外的跟踪和分析功能,还可以节省纸张。根据《纽约时报》的一篇最新文章,一些公司利用二维码创建客户订单历史记录和联系信息的数据库。此外,二维码使公司所有者能够查看每次扫描的频率、地点和时间。虽然这些技术可能对您的业务有所帮助,但其他人应该评估使用它们是否值得冒客户隐私的风险。近年来,消费者越来越关注公司如何使用他们的个人数据,如果他们感到被监视,他们愿意去别处购物。根据2019年Cisco的一项调查,32%的客户因数据共享协议而更换了公司。二维码存在安全风险,因为它们很容易被利用来将用户导向恶意网站或触发有害软件的下载。攻击者可以将恶意URL隐藏在二维码中,用户会不知情地扫描,从而导致网络钓鱼尝试或在设备上安装恶意软件。由于二维码的内容不可见,用户可能直到为时已晚才意识到危险。此外,公共场所或数字平台上的篡改代码很难检测,从而在用户不知情的情况下增加了网络攻击的脆弱性。
  • 网络欺诈:使用二维码的最大优势之一是能够接受无现金支付。客户可以通过扫描二维码使用智能手机付款,从而无需员工手动处理信用卡和支付设备。然而,网络罪犯正在制造虚假二维码并冒充合法公司,以欺骗毫无戒心的客户的钱财或用恶意软件感染他们的设备。虚假二维码比网络钓鱼电子邮件或其他冒充合法公司的方法更难识别。假冒二维码几乎与真实条形码完全相同。通过简单地将一个假冒的二维码覆盖在真实的二维码上,网络罪犯就可以将用户导向虚假的支付网站。

所以,在这篇文章中,我们了解了如何使用 Python 读取和创建二维码。