前言

在 JavaWeb 开发中,一般使用 Zxing 来生成和识别二维码,但是,Zxing 的识别有点差强人意,不少相对模糊的二维码识别率很低。不过就最新版本的测试来说,识别率有了现显著提高。

对比

在没接触 Python 之前,曾使用 Zbar 的客户端进行识别,测了大概几百张相对模糊的图片,Zbar的识别速度要快很多,识别率也比 Zxing 稍微准确那边一丢丢,但是,稍微模糊一点就无法识别。相比之下,微信和支付宝的识别效果就逆天了。

代码案例

# -*- coding:utf-8 -*-

import os

import qrcode

import time

from PIL import Image

from pyzbar import pyzbar

"""

# 升级 pip 并安装第三方库

pip install -U pip

pip install Pillow

pip install pyzbar

pip install qrcode

"""

def make_qr_code_easy(content, save_path=None):

"""

Generate QR Code by default

:param content: The content encoded in QR Codeparams

:param save_path: The path where the generated QR Code image will be saved in.

If the path is not given the image will be opened by default.

"""

img = qrcode.make(data=content)

if save_path:

img.save(save_path)

else:

img.show()

def make_qr_code(content, save_path=None):

"""

Generate QR Code by given params

:param content: The content encoded in QR Code

:param save_path: The path where the generated QR Code image will be saved in.

If the path is not given the image will be opened by default.

"""

qr_code_maker = qrcode.QRCode(version=2,

error_correction=qrcode.constants.ERROR_CORRECT_M,

box_size=8,

border=1,

)

qr_code_maker.add_data(data=content)

qr_code_maker.make(fit=True)

img = qr_code_maker.make_image(fill_color="black", back_color="white")

if save_path:

img.save(save_path)

else:

img.show()

def make_qr_code_with_icon(content, icon_path, save_path=None):

"""

Generate QR Code with an icon in the center

:param content: The content encoded in QR Code

:param icon_path: The path of icon image

:param save_path: The path where the generated QR Code image will be saved in.

If the path is not given the image will be opened by default.

:exception FileExistsError: If the given icon_path is not exist.

This error will be raised.

:return:

"""

if not os.path.exists(icon_path):

raise FileExistsError(icon_path)

# First, generate an usual QR Code image

qr_code_maker = qrcode.QRCode(version=4,

error_correction=qrcode.constants.ERROR_CORRECT_H,

box_size=8,

border=1,

)

qr_code_maker.add_data(data=content)

qr_code_maker.make(fit=True)

qr_code_img = qr_code_maker.make_image(fill_color="black", back_color="white").convert('RGBA')

# Second, load icon image and resize it

icon_img = Image.open(icon_path)

code_width, code_height = qr_code_img.size

icon_img = icon_img.resize((code_width // 4, code_height // 4), Image.ANTIALIAS)

# Last, add the icon to original QR Code

qr_code_img.paste(icon_img, (code_width * 3 // 8, code_width * 3 // 8))

if save_path:

qr_code_img.save(save_path)

else:

qr_code_img.show()

def decode_qr_code(code_img_path):

"""

Decode the given QR Code image, and return the content

:param code_img_path: The path of QR Code image.

:exception FileExistsError: If the given code_img_path is not exist.

This error will be raised.

:return: The list of decoded objects

"""

if not os.path.exists(code_img_path):

raise FileExistsError(code_img_path)

# Here, set only recognize QR Code and ignore other type of code

return pyzbar.decode(Image.open(code_img_path), symbols=[pyzbar.ZBarSymbol.QRCODE], scan_locations=True)

if __name__ == "__main__":

# # 简易版

# make_qr_code_easy("make_qr_code_easy", "make_qr_code_easy.png")

# results = decode_qr_code("make_qr_code_easy.png")

# if len(results):

# print(results[0].data.decode("utf-8"))

# else:

# print("Can not recognize.")

#

# # 参数版

# make_qr_code("make_qr_code", "make_qr_code.png")

# results = decode_qr_code("make_qr_code.png")

# if len(results):

# print(results[0].data.decode("utf-8"))

# else:

# print("Can not recognize.")

#

# 带中间 logo 的

# make_qr_code_with_icon("https://blog.52itstyle.vip", "icon.jpg", "make_qr_code_with_icon.png")

# results = decode_qr_code("make_qr_code_with_icon.png")

# if len(results):

# print(results[0].data.decode("utf-8"))

# else:

# print("Can not recognize.")

# 识别答题卡二维码 16 识别失败

t1 = time.time()

count = 0

for i in range(1, 33):

results = decode_qr_code(os.getcwd()+"\\img\\"+str(i)+".png")

if len(results):

print(results[0].data.decode("utf-8"))

else:

print("Can not recognize.")

count += 1

t2 = time.time()

print("识别失败数量:" + str(count))

print("测试时间:" + str(int(round(t2 * 1000))-int(round(t1 * 1000))))

测试了32张精挑细选的模糊二维码:

识别失败数量:1

测试时间:130

使用最新版的 Zxing 识别失败了三张。

源码

python二维码生成识别代码_Python学习案例之二维码生成识别相关推荐

  1. python调用摄像头人脸识别代码_Python使用 opencv调用笔记本摄像头进行人脸识别...

    首先需要导入opencv库pip install  -i https://pypi.tuna.tsinghua.edu.cn/simple/   opencv-python 上代码#万码学堂Pytho ...

  2. 爬虫python下载文献代码_Python爬虫案例:爬取微信公众号文章

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 文章转载于公众号:早起Python 作者:陈熹 大家好,今天我们来讲点Selenium自动化,你是 ...

  3. python segy格式地震数据读写包segyio学习笔记(二)

    python segy格式地震数据读写包segyio学习笔记(二) 最近大致搞明白了segyio读取叠后和叠前segy数据的方法,以及内部存储结构,以两段代码为例: 叠后数据读取.这是一个从给定时窗内 ...

  4. python灰度图生成g代码_Python打造一个在线G代码生成器

    用tornado框架做后端--,用bootstrap做前端 先上效果图: 生成出来的G代码: g.py源码: #coding=utf-8 # -*- coding: UTF-8 -*- #!/usr/ ...

  5. 手写体识别代码_Python识别图片中的文字

    一.前言 不知道大家有没有遇到过这样的问题,就是在某个软件或者某个网页里面有一篇文章,你非常喜欢,但是不能复制.或者像百度文档一样,只能复制一部分,这个时候我们就会选择截图保存.但是当我们想用到里面的 ...

  6. python建立文件数据库_python学习-- Django根据现有数据库,自动生成models模型文件...

    Django引入外部数据库还是比较方便的,步骤如下 : 创建一个项目,修改seting文件,在setting里面设置你要连接的数据库类型和连接名称,地址之类,和创建新项目的时候一致 运行下面代码可以自 ...

  7. python点餐系统代码_Python学习手册(第4版).1

    装饰器就是一个给对象添加额外功能的函数,其本质是函数.它的基本构造:高阶函数+函数嵌套+闭包. 装饰器 即在代码运行期间动态增加功能的方式. 3大条件: 1. 函数可作为对象,赋值给变量,也就是函数可 ...

  8. python视频人脸检测_Python学习案例之视频人脸检测识别

    前言 上一篇博文与大家分享了简单的图片人脸识别技术,其实在实际应用中,很多是通过视频流的方式进行识别,比如人脸识别通道门禁考勤系统.人脸动态跟踪识别系统等等. 案例 这里我们还是使用 opencv 中 ...

  9. python视频人脸识别教程_Python学习笔记之视频人脸检测识别实例教程

    前言 上一篇博文与大家分享了简单的图片人脸识别技术,其实在实际应用中,很多是通过视频流的方式进行识别,比如人脸识别通道门禁考勤系统.人脸动态跟踪识别系统等等. 下面话不多说了,来一起看看详细的介绍吧 ...

最新文章

  1. 量子科技概念大火,国内现状如何?国盾量子撑起量子通信,华为BAT均入局量子计算...
  2. 最大化平均值 (二分搜索法)
  3. micropython lcd_MicroPython动手做(05)——零基础学MaixPy之LCD液晶屏
  4. spring—AOP 的动态代理技术
  5. 开源合同管理系统_「物联网架构」最适合物联网的开源数据库
  6. 作者:王亮(1975-),男,中国科学院自动化研究所研究员,博士生导师
  7. 服务端访问Linux的DNS出现DNS request timed out..
  8. 观察:家乡的、身边的真实互联网
  9. go语法 — 多路选择操作符 select的用法
  10. p2p与反p2p的激战--资料搜集
  11. python3中将'\xb2\xbb\xca\xc7\xc4\xda\xb2\xbf\xbb\xf2\xcd\xe2\xb2\xbf\xc3\xfc\xc1\xee'转成中文
  12. grafana对接zabbix
  13. Python:游戏:测试打字速度
  14. 【JY】2B青年欢乐多之Matlab篇
  15. 一个失败创业者的告白
  16. go依赖包下载加速方法及github加速
  17. Linux ARP 代理专题
  18. 秦嘉哲:11.11黄金短线空单布局,黄金解τ策略(黄金早盘最新分析)
  19. 头脑风暴问题:如何将一只全新品种的龙虾卖给养虾人?
  20. Android常用命令行——gradlew,adb,adb shell

热门文章

  1. CIO圈子里的“老行家”:太平绅士赖锡璋
  2. Python编程:Python2和Python3环境下re正则匹配中文
  3. CSS浮动定位与背景样式
  4. JPA实体中数据库生成ID的最终指南1
  5. linux如何更改密钥环密码,Linux系统教程:Ubuntu桌面上禁用默认的密钥环解锁提示...
  6. 格兰杰因果( Granger causality test)在神经科学中脑区功能连接上的应用
  7. 计算机考研408真题(全国统考2009--2020)、985高校计算机考研资料(清北+北理+北邮+武大+华科+浙大+复旦+哈工大+西安交大+华南理工)、王道四件套、天勤四件套---百度网盘免费下载
  8. [ linux ] vim 编辑器的三种模式介绍
  9. 出现 Unexpected token T in JSON at position 0 ,at JSON.parse (<anonymous>) 的解决方法
  10. 汇编INT中断和I/O指令【获取主板时钟】