赞
踩
利用python中的zxing包实现二维码,条形码的识别。zxing使用0.12版本,python版本为3.7,先看效果:
再看代码:
- # -*- coding: utf-8 -*-import logging
- import osimport zxing # 导入解析包logger = logging.getLogger(__name__)
- if not logger.handlers:
- logging.basicConfig(level=logging.INFO)
- DEBUG = (logging.getLevelName(logger.getEffectiveLevel()) == 'DEBUG')class QRCode:
- @staticmethod
- def ocr_qr_code(path):
- """
- 检测图片中是否包含二维码
- :param path: 图片路径
- :return: BarCode(raw='', parsed='', uri=None, format=None,
- type=None, points=[])
- """
- zx = zxing.BarCodeReader()
- data = ''
- # 条形码或者二维码中有中文会出现异常
- try:
- zx_data = zx.decode(path) if zx_data:
- logger.debug(u'识别二维码:%s,内容: %s' % (filename, zx_data))
- data = zx_data
- else:
- logger.error(u'识别二维码出错:%s' % filename)
- return data
- except Exception as e:
- print(e)
- return Noneif __name__ == '__main__':
- filepath = 'D:/xxxx/'
- for parent, dirnames, filenames in os.walk(filepath):
- for filename in filenames:
- image_path = filepath + filename
- # 将图片文件里的信息转码放到ltext里面
- ltext = QRCode.ocr_qr_code(image_path)
- logger.info(u'[%s]二维码识别:[%s]!!!' % (filename, ltext))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。